C++ Inheritance and Types


C++ Inheritance

Inheritance means access the properties and features of one class into another class. The class who is going to provide its features to another class will be called base class and the class who is using the properties and features of another class will be called derived class.

Example of inheritance


       class Employee
              int Id;
              char Name[25];
              int Age;
              long Salary;

               void GetData()
                      cout<<"\n\tEnter Employee Id : ";

                      cout<<"\n\tEnter Employee Name : ";

                      cout<<"\n\tEnter Employee Age : ";

                      cout<<"\n\tEnter Employee Salary : ";

               void PutData()
                      cout<<"\n\nEmployee Id : "<<Id;
                      cout<<"\nEmployee Name : "<<Name;
                      cout<<"\nEmployee Age : "<<Age;
                      cout<<"\nEmployee Salary : "<<Salary;


       class Company : public Employee   //Statement 1
              int RegNo;
              char CName[25];

               void ReadData()
                      cout<<"\n\nEnter Registration No. : ";

                      cout<<"\nEnter Company Name : ";


               void WriteData()
                      cout<<"\n\nRegistration No. : "<<RegNo;
                      cout<<"\nCompany Name : "<<CName;

       void main()

              Company C;            //Statement 2 : Creating Object of Derived Class

              C.GetData();          //Statement 3 : Calling Base Class Method()

              C.PutData();          //Statement 5 : Calling Base Class Method()

   Output :

              Enter Employee Id : 1
              Enter Employee Name : Kumar
              Enter Employee Age : 29
              Enter Employee Salary : 45000

              Enter Registration No. : 715
              Enter Company Name : TutorialDost

              Employee Id : 1
              Employee Name : Kumar
              Employee Age : 29
              Employee Salary : 45000
              Registration No. : 715
              Company Name : TutorialDost

Consider the statement 1, we are publically inheriting an Employee class into Company class using colon(:).

Now, The object of Company class can access the member function GetData() and PutData() of Employee class.

Derived class can inherit Base class as private, protected or public. More info.

Types of Inheritance

C++ supports six types of inheritance as follows:

  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Heirarchical Inheritance
  • Hybrid Inheritance
  • Multipath Inheritance

Single Inheritance

A derived class with only one base class is called single inheritance.

Multilevel Inheritance

A derived class with one base class and that base class is a derived class of another is called multilevel inheritance.

Multiple Inheritance

A derived class with multiple base class is called multiple inheritance.

Heirarchical Inheritance

Multiple derived classes with same base class is called hierarchical inheritance.

Hybrid Inheritance

Combination of multiple and hierarchical inheritance is called hybrid inheritance.

Multipath Inheritance

A derived class with two base classes and these two base classes have one common base class is called multipath inheritance.More info.