#include<iostream> using namespace std; class Base { public: void show() { cout<<" In Base "; } }; class Derived: public Base { public: int x; void show() { cout<<"In Derived "; } Derived() { x = 10; } }; int main(void) { Base *bp, b; Derived d; bp = &d; bp->show(); cout << bp->x; return 0; } |
chevron_right
filter_none
(A) Compiler Error in line ” bp->show()”
(B) Compiler Error in line ” cout <x”
(C) In Base 10
(D) In Derived 10
Answer: (B)
Explanation: A base class pointer can point to a derived class object, but we can only access base class member or virtual functions using the base class pointer because object slicing happens when a derived class object is assigned to a base class object. Additional attributes of a derived class object are sliced off to form the base class object.
Quiz of this Question
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- C++ | Inheritance | Question 1
- C++ | Inheritance | Question 2
- C++ | Inheritance | Question 3
- C++ | Inheritance | Question 4
- C++ | Inheritance | Question 5
- C++ | Inheritance | Question 6
- C++ | Inheritance | Question 8
- C++ | Inheritance | Question 9
- C++ | Inheritance | Question 10
- C++ | Inheritance | Question 11
- C++ | Inheritance | Question 12
- C++ | Inheritance | Question 13
- C++ | Inheritance | Question 14
- Java | Inheritance | Question 1
- Java | Inheritance | Question 2
- Java | Inheritance | Question 3
- Java | Inheritance | Question 4
- Java | Inheritance | Question 5
- Java | Inheritance | Question 9
- Java | Inheritance | Question 7
Improved By : Pallabi Mondal

