C++ Inheritance

Question 1
#include<iostream>
 
using namespace std;
class Base1 {
 public:
     Base1()
     { cout << " Base1's constructor called" << endl;  }
};
 
class Base2 {
 public:
     Base2()
     { cout << "Base2's constructor called" << endl;  }
};
 
class Derived: public Base1, public Base2 {
   public:
     Derived()
     {  cout << "Derived's constructor called" << endl;  }
};
 
int main()
{
   Derived d;
   return 0;
}
Cross
Compiler Dependent

Tick
Base1′s constructor called
Base2′s constructor called
Derived’s constructor called

Cross
Base2′s constructor called
Base1′s constructor called
Derived’s constructor called

Cross
Compiler Error


Question 1-Explanation: 
When a class inherits from multiple classes, constructors of base classes are called in the same order as they are specified in inheritance.
Question 2
Output?
#include <iostream>  
using namespace std;

class Base1 {
 public:
     ~Base1()  { cout << " Base1's destructor" << endl; }
};
  
class Base2 {
 public:
     ~Base2()  { cout << " Base2's destructor" << endl; }
};
  
class Derived: public Base1, public Base2 {
   public:
     ~Derived()  { cout << " Derived's destructor" << endl; }
};
  
int main()
{
   Derived d;
   return 0;
}

Cross
Base1\'s destructor
Base2\'s destructor
Derived\'s destructor
Tick
Derived\'s destructor
Base2\'s destructor
Base1\'s destructor
Cross
Derived\'s destructor
Cross
Compiler Dependent


Question 2-Explanation: 
Destructors are always called in reverse order of constructors.
Question 3
Assume that an integer takes 4 bytes and there is no alignment in following classes, predict the output.
#include<iostream>
using namespace std;

class base {
    int arr[10];
};

class b1: public base { };

class b2: public base { };

class derived: public b1, public b2 {};

int main(void)
{
  cout << sizeof(derived);
  return 0;
}
Cross
40
Tick
80
Cross
0
Cross
4


Question 3-Explanation: 
Since b1 and b2 both inherit from class base, two copies of class base are there in class derived. This kind of inheritance without virtual causes wastage of space and ambiguities. virtual base classes are used to save space and avoid ambiguities in such cases. For example, following program prints 48. 8 extra bytes are for bookkeeping information stored by the compiler (See this for details)
#include<iostream>
using namespace std;
 
class base {
  int arr[10];     
};
 
class b1: virtual public base { };
 
class b2: virtual public base { };
 
class derived: public b1, public b2 {};
 
int main(void)
{ 
  cout << sizeof(derived);
  return 0;
} 
Question 4
#include<iostream>
 
using namespace std;
class P {
public:
   void print()  { cout <<" Inside P"; }
};
 
class Q : public P {
public:
   void print() { cout <<" Inside Q"; }
};
 
class R: public Q { };
 
int main(void)
{
  R r; 
  r.print();
  return 0;
}
Cross
Inside P
Tick
Inside Q
Cross
Compiler Error: Ambiguous call to print()


Question 4-Explanation: 
The print function is not present in class R. So it is looked up in the inheritance hierarchy. print() is present in both classes P and Q, which of them should be called? The idea is, if there is multilevel inheritance, then function is linearly searched up in the inheritance hierarchy until a matching function is found.
Question 5
Output?
#include<iostream>
using namespace std;

class Base {
private:
     int i, j;
public:
    Base(int _i = 0, int _j = 0): i(_i), j(_j) { }
};
class Derived: public Base {
public:
     void show(){
        cout<<" i = "<<i<<"  j = "<<j;
     }
};
int main(void) {
  Derived d;
  d.show();
  return 0;
}
Cross
i = 0 j = 0
Tick
Compiler Error: i and j are private in Base
Cross
Compiler Error: Could not call constructor of Base


Question 6
#include<iostream>
using namespace std;

class Base {};

class Derived: public Base {};

int main()
{
    Base *bp = new Derived;
    Derived *dp = new Base;
}
Cross
No Compiler Error
Cross
Compiler Error in line \"Base *bp = new Derived;\"
Tick
Compiler Error in line \" Derived *dp = new Base;\"
Cross
Runtime Error


Question 6-Explanation: 
A Base class pointer/reference can point/refer to a derived class object, but the other way is not possible.
Question 7
#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;
}

Cross
Compiler Error in line \" bp->show()\"
Tick
Compiler Error in line \" cout << bp->x\"
Cross
In Base 10
Cross
In Derived 10


Question 7-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.
Question 8

C

#include<iostream>
using namespace std;

class Base
{
public:
    int fun()  { cout << "Base::fun() called"; }
    int fun(int i)  { cout << "Base::fun(int i) called"; }
};

class Derived: public Base
{
public:
    int fun() {  cout << "Derived::fun() called"; }
};

int main()
{
    Derived d;
    d.fun(5);
    return 0;
}
Cross

Base::fun(int i) called

Cross

Derived::fun() called

Cross

Base::fun() called

Tick

Compiler Error



Question 8-Explanation: 

If a derived class writes its own method, then all functions of base class with same name become hidden, even if signatures of base class functions are different. In the above question, when fun() is rewritten in Derived, it hides both fun() and fun(int) of base class.

Question 9
#include<iostream>
using namespace std;

class Base {
public:
    void fun()          {    cout << "Base::fun() called";     }
    void fun(int i)     {   cout << "Base::fun(int i) called";  }
};

class Derived: public Base  {
public:
    void fun()   {     cout << "Derived::fun() called";   }
};

int main()  {
    Derived d;
    d.Base::fun(5);
    return 0;
}
Cross
Compiler Error
Tick
Base::fun(int i) called


Question 9-Explanation: 
We can access base class functions using scope resolution operator even if they are made hidden by a derived class function.
Question 10

Output of following program? 

CPP

#include <iostream>
#include<string>
using namespace std;

class Base
{
public:
    virtual string print() const
    {
        return "This is Base class";
    }
};

class Derived : public Base
{
public:
    virtual string print() const
    {
        return "This is Derived class";
    }
};

void describe(Base p)
{
    cout << p.print() << endl;
}

int main()
{
    Base b;
    Derived d;
    describe(b);
    describe(d);
    return 0;
}
Cross
This is Derived class
This is Base class
Cross
This is Base class
This is Derived class
Tick
This is Base class
This is Base class
Cross

Compiler Error



Question 10-Explanation: 

Note that an object of Derived is passed in describe(d), but print of Base is called. The describe function accepts a parameter of Base type. This is a typical example of object slicing, when we assign an object of derived class to an object of base type, the derived class object is sliced off and all the data members inherited from base class are copied. Object slicing should be avoided as there may be surprising results like above. As a side note, object slicing is not possible in Java. In Java, every non-primitive variable is actually a reference.

There are 15 questions to complete.

  • Last Updated : 28 Sep, 2023

Share your thoughts in the comments
Similar Reads