C++ | Inheritance | Question 7

3
#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;
}

(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.


Quiz of this Question

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 1 vote(s)