12

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;
}
A
Compiler Dependent

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

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

D
Compiler Error
Inheritance    
Discuss it


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

A
Base1's destructor
Base2's destructor
Derived's destructor
B
Derived's destructor
Base2's destructor
Base1's destructor
C
Derived's destructor
D
Compiler Dependent
Inheritance    
Discuss it


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;
}
A
40
B
80
C
0
D
4
Inheritance    
Discuss it


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;
}
A
Inside P
B
Inside Q
C
Compiler Error: Ambiguous call to print()
Inheritance    
Discuss it


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;
}
A
i = 0 j = 0
B
Compiler Error: i and j are private in Base
C
Compiler Error: Could not call constructor of Base
Inheritance    
Discuss it


Question 6
#include<iostream>
using namespace std;

class Base {};

class Derived: public Base {};

int main()
{
    Base *bp = new Derived;
    Derived *dp = new Base;
}
A
No Compiler Error
B
Compiler Error in line "Base *bp = new Derived;"
C
Compiler Error in line " Derived *dp = new Base;"
D
Runtime Error
Inheritance    
Discuss it


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

A
Compiler Error in line " bp->show()"
B
Compiler Error in line " cout << bp->x"
C
In Base 10
D
In Derived 10
Inheritance    
Discuss it


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
#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;
}
A
Base::fun(int i) called
B
Derived::fun() called
C
Base::fun() called
D
Compiler Error
Inheritance    
Discuss it


Question 8 Explanation: 
If a derived class writes its own method, then all functions of base class with same name become hidden, even if signaures 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:
    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.Base::fun(5);
    return 0;
}
A
Compiler Error
B
Base::fun(int i) called
Inheritance    
Discuss it


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?
#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;
}
A
This is Derived class
This is Base class
B
This is Base class
This is Derived class
C
This is Base class
This is Base class
D
Compiler Error
Inheritance    
Discuss it


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 ovoided 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 14 questions to complete.
12

Company Wise Coding Practice    Topic Wise Coding Practice