Exercise: Questions on Inheritance


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

Compiler Dependent
Base1′s constructor called
Base2′s constructor called
Derived’s constructor called
Base2′s constructor called
Base1′s constructor called
Derived’s constructor called
Compiler Error
Show Answer
Questions on 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;
}


Base1's destructor
Base2's destructor
Derived's destructor
Derived's destructor
Base2's destructor
Base1's destructor
Derived's destructor
Compiler Dependent
Show Answer
Questions on Inheritance : 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;
}

40
80
0
4
Show Answer
Questions on Inheritance : 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;
}

Inside P
Inside Q
Compiler Error: Ambiguous call to print()
Show Answer
Questions on Inheritance : 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;
}

i = 0 j = 0
Compiler Error: i and j are private in Base
Compiler Error: Could not call constructor of Base
Show Answer
Questions on Inheritance : Question 6 :
#include<iostream>
using namespace std;

class Base {};

class Derived: public Base {};

int main()
{
    Base *bp = new Derived;
    Derived *dp = new Base;
}

No Compiler Error
Compiler Error in line "Base *bp = new Derived;"
Compiler Error in line " Derived *dp = new Base;"
Runtime Error
Show Answer
Questions on Inheritance : 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;
}


Compiler Error in line " bp->show()"
Compiler Error in line " cout << bp->x"
In Base 10
In Derived 10
Show Answer
Questions on Inheritance : 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;
}

Base::fun(int i) called
Derived::fun() called
Base::fun() called
Compiler Error
Show Answer
Questions on Inheritance : 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;
}

Compiler Error
Base::fun(int i) called
Show Answer
Questions on Inheritance : 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;
}

This is Derived class
This is Base class
This is Base class
This is Derived class
This is Base class
This is Base class
Compiler Error
Show Answer