OOP Using C++ – C++ Programming Inheritance based Questions and Answers

Lists of Questions and Answers

  • 1. Explain types of inheritance with example
  • 2. List out visibility of inherited members in various categories of inheritance.
  • 3. Explain protected access modifier for class members
  • 4. What is overriding in C++. Explain with suitable example.
  • 5. Explain virtual base class with example
  • 6. List out various situations for the execution of base class constructor in inheritance.

 

Question – 1. Explain types of inheritance with example.

  • Inheritance is the process, by which class can acquire the properties and methods of another class.
  • The mechanism of deriving a new class from an old class is called inheritance.
  • The new class is called derived class and old class is called base class.
  • The derived class may have all the features of the base class and the programmer can add new features to the derived class.

 

Types of Inheritance:

Single Inheritance

  • If a class is derived from a single class then it is called single inheritance.
  • Class B is derived from class A

 

Multilevel Inheritance

  • A class is derived from a class which is derived from another class then it is called multilevel inheritance
  • Here, class C is derived from class B and class B is derived from class
  • A, so it is called multilevel inheritance.

 

Multiple Inheritance

  • If a class is derived from more than one class then it is called multiple inheritance.
  • Here, class C is derived from two classes, class A and class B

 

Hierarchical Inheritance

  • If one or more classes are derived from one class then it is called hierarchical inheritance.
  • Here, class B, class C and class D are derived from class A.

 

Hybrid Inheritance

  • It is a combination of any above inheritance types. That is either multiple or multilevel or hierarchical or any other combination.
  • Here, class B and class C are derived from class A and class D is derived from class B and class C.
  • class A, class B and class C is example of Hierarchical Inheritance and class B, class C and class D is example of Multiple Inheritance so

this hybrid inheritance is combination of Hierarchical and Multiple Inheritance

 

Example:

#include<iostream>

using namespace std;

class A

{

 public:

void dispA()

{

cout<<"class A method"<<endl;

}

};

class B : public A // Single Inheritance - class B is derived from class A

{

 public:

void dispB()

{

cout<<"class B method";

}

};

class C : public B // Multilevel Inheritance - class C is derived from class B

{

 public:

void dispC()

{

cout<<"class C method"<<endl;

}

};

class D

{

 public:

void dispD()

{

cout<<"class D method"<<endl;

}

};

class E: public A, public D //Multiple Inheritance:class E is derived from class A

{ // and D

 public:

void dispE()

{

cout<<"class E method";

}

};

class F: public B, public C //Hybrid Inheritance:class F is derived from class B

{ // and C

 public:

void dispF()

{

cout<<"class F method";

}

};

int main()

{

A a;

B b;

C c;

D d;

E e;

F f;

b.displayA();

f.displayF();

f.displayA();

}

 

Output:

class A method

class A method

class D method

class C method

 

 

  • Class B and class E are derived from class A so it is example of Hierarchal Inheritance.
  • Class F is derived from class B and class C, class B is derived from class A so displayA() is not a member of class F then also we can access it using object of class F.

 

Question – 2. List out visibility of inherited members in various categories of inheritance.

Base class visibility Derived class visibility
Public derivation Private derivation Protected derivation
Private Not inherited Not inherited Not inherited
Protected Protected Private Protected
Public Public Private Protected

 

Question – 3. Explain protected access modifier for class members.

Protected:

  • This access modifier plays a key role in inheritance.
  • Protected members of the class can be accessed within the class and from derived class but cannot be accessed from any other class or program.
  • It works like public for derived class and private for other programs

 

Example:

#include<iostream>

using namespace std;

class A

{

protected:

int a;

public:

void getdata()

{

cout<<"Enter value of a:";

cin>>a;

}

};

class B:public A

{

public:

void show()

{

cout<<"Value of a is:"<<a;

}

};

int main()

{

B x;

x.getdata();

x.show();

}

Output:

Enter value of a:5

Value of a is:5

 

Question – 4. What is overriding in C++. Explain with suitable example.

 

  • If base class and derived class have member functions with same name and arguments.
  • If you create an object of derived class and write code to access that member function then, the member function in derived class is only invokes.
  • Means the member function of derived class overrides the member function of base class.
  • This is called function overriding or method overriding in C++.

 

Example:

#include<iostream>

using namespace std;

class A

{

public:

void display()

{

cout<<"This is parent class";

}

};

class B:public A

{

public:

void display() // overrides the display() function of class A

{

cout<<"\nThis is child class";

}

};

int main()

{

B x;

x.display(); // method of class B invokes, instead of class A

}

Output:

This is child class

 

Question – 5. Explain virtual base class with example.

  • It is used to prevent the duplication/ambiguity.
  • In hybrid inheritance child class has two direct parents which themselves have a common base class.
  • So, the child class inherits the grandparent via two seperate paths. it is also called as indirect parent class.
  • All the public and protected member of grandparent are inherited twice into child.

 

FIGURE COMES HERE

 

We can stop this duplication by making base class virtual.

For example:

class A

{

 public:

 int i;

};

class B : virtual public A

{

 public:

 int j;

};

class C: virtual public A

{

 public:

 int k;

};

class D: public B, public C

{

 public:

 int sum;

};

 

The keywords virtual and public may be used in either order.

If we use virtual base class, then it will inherit only single copy of member of base class to child class.

 

Question – 6. List out various situations for the execution of base class constructor in inheritance.

 

Method of Inheritance Order of Execution
Class B:public A

{

};

A(); base constructor

B(); derived constructor

Class A:public B, public C

{

};

B(); base(first)

C(); base(second)

A(); derived

Class A:public B, virtual public C

{

};

C(); virtual base

B(); ordinary base

A(); derived