Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism

Types of Inheritance

Data Structures using C++ Program

Questions: 1. Explain with examples, the types of inheritance in C++. 2. Explain the types of inheritance with example. 3. What is multiple inheritance? Discuss the syntax and rules of multiple inheritance in C++. How can you pass parameters to the constructors of base classes in multiple inheritance? Explain with suitable example. 4. What is inheritance ? List out the advantages of inheritance. 5. Write a C++ program to illustrate the concept of hierarchical inheritance. 6. Write C++ program to implement multiple inheritance.

Types of Inheritance

• Various types of inheritances are as shown by following figures ‒


1. Single Inheritance

2. Multiple Inheritance

3. Multilevel Inheritance

4. Hierarchical Inheritance

5. Hybrid Inheritance






1. Single Inheritance

 

• In single inheritance there is one parent per derived class. This is the most common form of inheritance.


The simple program for such inheritance is ‒

C++ Program

#include <iostream>

using namespace std;

class Base

{

public:

   int x

   void set _x(int n)

   {

      x=n;

   }

   void show_ x()

   {

      cout<<"\n\t Base class ...";

      cout<<"\n\t x= "<<x;

   }

};

class derived : public Base

{

   int y;

public:

   void set_ y(int n)

   {

      y = n;

   }

   void show_xy()

   {

      cout<<"\n\n\t Derived class...";

      cout<<"\n\t x = "<<x;

      cout<<"\n\t y= "<<y;

   }

};

int main()

{

   derived obj;

   int x, y;

   cout<<"\n Enter the value of x";

   cin>>x;

   cout<<"\n Enter the value of y";

   cin>>y;

   obj.set x(x);//inherits base class

   obj.set y(y); // access member of derived class

   obj.show x();//inherits base class

   obj.show xy(); // access member of derived class

   return 0;

}

Output

Enter the value of x 10

Enter the value of y 20

Base class

x= 10

Derived class ...

x = 10

y  = 20

Example: 1

Define a class to represent a string with operations string length, compare and reverse. Show its use of base and derived classes.

Solution :

#include <iostream>

using namespace std;

class Base

{

   public:

   char str1[10],str2[10];

   void getstr()

   {

      cout<<"\n Enter String1: ";

      cin>>str1;

      cout<<"\n Enter String2: ";

      cin>>str2;

   }

};

class Derived:public Base

{

public:

   int getlength(char s[10]).

   {

      for(int i=0;s[i]!='\0';i++);

      return i‒1;

   }

   void reverse()

   {

      int n=getlength(str1);

      for(int i=n;i>=0;i− −)

      cout<<str1[i];

      cout<<endl;

   }

void compare()

int i,j,flag=0;

int n1=getlength(str1);

int n2=getlength(str2);

for(i=0,j=0;i<n1,j<n2;i++j++)

{

   if(str1[i]!=str2[j])

   flag=1;

}

if(flag= =1)

   cout<<"\n Two strings are not equal";

else

   cout<<"\n Two strings are equal";

}

};

int main()

{

   Derived obj;

   obj.getstr();

   cout<<"\n Reversing first string: ";

   obj.reverse();

   obj.compare();

   return 0;

}

Output

Enter String1: hello

Enter String2: hello

Reversing first string: olleh

Two strings are equal

 

2. Multiple Inheritance

 

• In multiple inheritance the derived class is derived from more than one base class.


• The implementation of multiple inheritance is as shown below ‒

C++ Programs

#include <iostream>

using namespace std;

class Operation

{

protected:

   int x, y,

public:

   void set_ values (int a, int b)

   {

      x=a;

      y=b;

   }

};

class Coutput

{

public:

   void display (int i);

};

void Coutput::display (int i);

{

   cout << i << endl;

}

//product class inherits two base classes ‒

//Operation and Coutput

class product: public Operation, public Coutput

{

public:

   int function ()

   {

      return (x * y);

   }

};

//sum class inherits two base classes ‒

//Operation and Coutput

class sum: public Operation, public Coutput

{

public:

   int function ()

   {

      return (x + y);

   }

};

int main()

{

   product obj_pr;//object of product class

   sum obj_sum;//object of sum class

   obj_pr.set_values (10,20);

   obj_sum.set_values (10,20);

   cout<<"\n The product of 10 and 20 is "<<endl;

   obj_pr.output (obj_pr.function());

   cout<<"\n The sum of 10 and 20 is "<<endl;

   obj_sum.output (obj_sum.function());

   return 0;

}

Output

The product of 10 and 20 is

200

The sum of 10 and 20 is

30

• In above program there are two classes Operation and Coutput. The derived class product is derived from both Operation and Coutput classes. Similarly the derived class sum is derived from two classes: Operation and Coutput.

• Then in main function obj_pr is an object created for class product and obj_sum is an object created for class sum. Thus multiple inheritance is achieved.

 

3. Multilevel Inheritance

 

• When a derived class is derived from a base class which itself is a derived class then that type of inheritance is called multilevel inheritance.


• For example ‒ If class A is a base class and class B is another class which is derived from A, similarly there is another class C being derived from class B then such a derivation leads to multilevel inheritance.

• The implementation of multilevel inheritance is as given below ‒

C++ Program

#include<iostream>

using namespace std;

class A

{

protected:

   int x;

public:

   void get_a(int);

   void put_a();

};

void A:get_a(int a)

{

   x=a;

}

void A::put_a()

{

   cout<<"\n The value of x is "<<x;

}

class B: public A

{

protected:

   int y;

public:

   void get_b(int);

   void put_b();

};

void B::get_b(int b)

{

   y=b;

}

void B::put_b()

{

   cout<<"\n The value of y is "<<y;

}

class C::public B

{

   int z;

   public:

   void display();

};

void C::display()

{

   z=y+10;

   put_a();//member of class A

   put_b();//member of class B

   cout<<"\n The value of z is "<<z;

}

int main()

{

   C obj://object of class C

   //accessing class A member via object of class C

   obj.get_a(10);

   //accessing class B member via object of class C

   obj.get_b(20);

   //accessing class C member via object of class C

   obj.display();

   cout<<endl;

   return 0;

}

Output

The value of x is 10

The value of y is 20

The value of z is 30

•  In above program we have declared 3 classes namely A, B and C. In these classes values to variables x, y and z are assigned.

• In class C, which is actually derived from a derived class B(derived from A) a display() function is written. Note that the members of class A and B are accessible in class C as it is a derived class.

z=y+10;

put a();//member of class A

put_b();//member of class B

cout<<"\n The value of z is "<<z;

Similarly in main() function we have created an object of class C.

C obj;

And now using this obj we can access the member of any class.

Thus the multilevel inheritance is achieved.


4. Hierarchical Inheritance

 

• Hierarchical inheritance is a kind of inheritance in which one or more classes are derived from the common base class. For example ‒

• In this type of inheritance the subclass can inherit the properties of its parent classes and at the same time it can add its new features. The subclass can serve as a base class for the lower level classes.


• The C++ program demonstrating this type of inheritance is as shown below ‒

#include<iostream>

using namespace std;

class Passenger

{

   int code:

   char name[20];

public:

   void getPassenger()

   {

      cout<<"\nEnter the code and name";

      cin>>code>>name;

   }

   void ShowDetails()

   {

      cout<<"\nCode: "<<code;

      cout<<"\nName: "<<name;

   }

};

class Seats:public Passenger

{

   int NoOfSeats;

   public:

   void getSeats()

   {

      cout<<"\nEnter the Number of Seats";

      cin>>NoOfSeats;

   }

   void Display _Reservation()

   {

      cout<<"\nNumber of Seats: "<<NoOfSeats;

   }

};

class AC_ Class:public Seats

{

   double fare;

   public:

   void getFare()

}

cout<<"\nEnter the fare";

cin>>fare;

}

void DisplayFare()

{

   cout<<"\nType: AC Class Reservation";

   cout<<"\nFare Amount: "<<fare;

}

};

class NonAC_Class:public Seats

{

   public:

   void DisplayClass()

   {

      cout<<"\nType: Non AC Class Reservation";

   }

};

using namespace std;

int main()

{                                 

   int i,m,n,choice;

   AC_Class a[10];

   NonAC Class na[10];

   cout<<"\nEnter the number of AC Class Passengers ";

   cin>>m;

   for(i=0;i<m;i++)

   {

      cout<<"\nEnter Details of Passenger "<<<i+1;

      a[i].getPassenger();

      a[i].getSeats();

      a[i].getFare();

      return 0;

   }

   cout<<"\nEnter the number of Non‒AC Class Passengers ";

   cin>>n;

   for(i=0;i<n;i++)

   {

      cout<<"\nEnter the Details of Passenger "<<i+1;

      na[i].getPassenger();

      na[i].getSeats();

   }

   while(1)

   {

      cout<<"\n Displaying Details";

      cout<<"\n 1. AC Class \n 2. Non AC Class\n 3. Exit\n";

      cout<<"\n Enter Choice";

      cin>>choice;

      switch(choice)

      {

      case 1:for(i=0;i<m;i++)

       {

         a[i].ShowDetails();

         a[i].Display Reservation();

         a[i].DisplayFare();

       }

     break;

   case 2:for(i=0;i<n;i++)

    {

       na[i].ShowDetails();

       na[i].DisplayClass();

       nafil Display Reservation();

   }

  break;

case 3:exit(0);

 }

 }

}

 

5. Hybrid Inheritance

 

• When two or more types of inheritances are combined together then it forms the hybrid inheritance. The following Fig. 2.4.5 represents the typical scenario of hybrid inheritance.


• The following implementation shows that multiple and multilevel inheritance is combined together to form a hybrid inheritance.

C++ Program

#include<iostream>

using namespace std;

class A

{

protected:

   int x,

public:

   void get_a(int);

   void put a();

   void A:get_a(int a)

   {

      x = a;

   }

   void A::put a()

   {

      cout<<"\n The value of x is "<<x;

   }

   class:B public A

   {

   protected:

   int y:

   public:

   void get_b(int);

   void put_b();

};

void B::get_b(int b)

{

   y=b;

}

void B::put_b()

{

   cout<<"\n The value of y is "<<y;

}

class D

{

   protected:

   int t;

   public:

   void get_d(int);

   void put _d();

};

void D::get_d(int d)

{

   t=d;

}

void D::put d()

{

   cout<<"\n The value of t is "<<t;

}

//multiple inheritance added in the multilevel inheritance

class C:public B,public D

{

   int z:

   public:

   void display();

};

void C::display()

{

   z=y+t+10;

   put_a();//member of class A

   put_b();//member of class B

   put_d();//member of class D

   cout<<"\n The value of z is "<<z;

}

int main()

{

   C obj://object of class C

   //accessing class A member via object of class C

   obj.get_a(10);

   //accessing class B member via object of class C

   obj.get_b(20);

   ////accessing class C member via object of class C

   obj.get_d(30);

   obj.display();

   cout<<endl;

   return 0;

}

Output

The value of x is 10

The value of y is 20

The value of t is 30

The value of z is 60

 

Example: 2

Write a complete C++ program to do the following:

i) 'Student' is a base class, having two data members: entryno and name; entryno is integer and name of 20 characters long. The value of entryno is 1 for Science student and 2 for Arts student, otherwise it is an error.

ii) 'Science' and 'Arts' are two derived classes, having respectively data items marks for Physics, Chemistry, Mathematics and marks for English, History, Economics.

iii) Read appropriate data from the screen for 3 Science and 2 Arts Students.

iv) Display entryno, name, marks for Science students first and then for Arts students.

Solution:

#include<iostream>

using namespace std;

class student

{

protected:

   int entryno;

  char name[20];

public:

void Input()

{

   cout<<"Enter name of the student"<<endl;

   cin>>name;

}

void display()

{

   cout<<"Student Name: "<<name<<endl;

}

};

class science:public student

{

   float physics;

   float chemistry;

   float maths;

   public:

void Input()

{

   student::Input();

   cout<<"Enter marks for Physics: ";

   cin>>physics;

   cout<<"Enter marks for Chemistry: ";

   cin>>chemistry;

   cout<<"Enter marks for Mathematics: ";

   cin>>maths;

}

void display()

{

   entryno=1;

   cout<<"\n\t Entry Number for Science student is: "<<entryno<<endl;

   student::display();

   cout<<"\nMarks in Physics: ";

   cout<<physics;

   cout<<"\nMarks in Chemistry: ";

   cout<<chemistry;

   cout<<"\nMarks in Mathematics: ";

   cout<<maths;

}

};

class arts:public student

{

   float english;

   float history:

   float economics;

   public:

   void Input()

   {

       student::Input();

      cout<<"\nEnter marks for English: ";

      cin>>english;

      cout<<"\nEnter marks for history: ";

      cin>>history;

      cout<<"\nEnter marks for Economics: ";

      cin>>economics;

   }

   void display()

   {

      entryno=2;

      cout<<"\n\t Entry number for Arts student is: "<<entryno<<endl;

      student::display();

      cout<<"\nMarks in English: ";

      cout<<english;

      cout<<"\nMarks in History: ";

      cout<<history;

      cout<<"\nMarks in Economics: ";

      cout<<economics;

}

};

void main()

{

   science s1[3];

   arts a1[3];

   int i,j,k,l;

   cout<<"\nEntry for Science students "<<endl;

   for(i=0;i<3;i++)

}

s1[j].Input();

}

cout<<"Details of three Science students are "<<endl;

for(j=0;j<3;j++)

{

   s1[j].display();

}

cout<<"\n\nEntry for Arts students "<<endl;

for(k=0;k<3;k++)

{

   a1[k].Input();

}

cout<<"Details of three Arts students are "<<endl;

for(1=0;1<3;1++)

{

   a1[1].display();

}

}

 

Example :3

How can you pass parameters to the constructors of base classes in multiple inheritance? Explain with suitable example.

Solution :

#include<iostream>

using namespace std;

class A

{

public:

A(int a)

{

   cout<<"\n Constructor for class A is called, value1= "<<a;

}

};

class B

{

public:

B(float b)

{

   cout<<"\n Constructor for class B is called, value2= "<<b;

}

};

class C:public A,public B

{

public:

C(int a,float b):A(a),B(b)

{

   cout<<"\n Constructor for child class C is called value1= "<<a<<" value2= "<<b;

}

};

void main()

{

   cout<<"\n\t Creating class objects....";

   C obj(10,20.1);

   cout<<endl;

}

Output

Creating class objects....

Constructor for class A is called, value1= 10

Constructor for class B is called, value2= 20.1

Constructor for child class C is called value1= 10 value2 = 20.1

 

 

Review Questions

1. Explain with examples, the types of inheritance in C++.

2. Explain the types of inheritance with example.

3. What is multiple inheritance? Discuss the syntax and rules of multiple inheritance in C++. How can you pass parameters to the constructors of base classes in multiple inheritance? Explain with suitable example.

4. What is inheritance ? List out the advantages of inheritance.

5. Write a C++ program to illustrate the concept of hierarchical inheritance.

6. Write C++ program to implement multiple inheritance.

 

Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism : Tag: Data Structure, C++, C Programing : Data Structures using C++ Program - Types of Inheritance


Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation