7. Inheritance

In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class.Inheritance is one of the most important feature of Object Oriented Programming.

In C++, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class.          


Modes of Inheritance

  1. Public mode: If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class.
  2. Protected mode: If we derive a sub class from a Protected base class. Then both public member and protected members of the base class will become protected in derived class.
  3. Private mode: If we derive a sub class from a Private base class. Then both public member and protected members of the base class will become Private in derived class.

Note : The private members in the base class cannot be directly accessed in the derived class, while protected members can be directly accessed. For example, Classes B, C and D all contain the variables x, y and z in below example. It is just question of access.

Example:-
class A  
public: 
 int x; 
protected: 
 int y; 
private: 
 int z; 
}; 

class B : public A 
    // x is public 
   // y is protected 
   // z is not accessible from B 
}; 

class C : protected A 
   // x is protected 
   // y is protected 
  // z is not accessible from C 
}; 

class D : private A    // 'private' is default for classes 
   // x is private 
   // y is private 
  // z is not accessible from D 
}; 

The below table summarizes the above three modes and shows the access specifier of the members of base class in the sub class when derived in public, protected and private modes:

Types Of Inheritance:-

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multilevel inheritance
  • Hybrid inheritance
 1.Single Inheritance: In single inheritance, a class is allowed to inherit from only one class. i.e. one sub class is inherited by one base class only.


 
Syntax:-
class subclass_name : access_mode base_class
{
  //body of subclass
};

Example:-
#include <iostream>  
using namespace std;  
 class Account {  
   public:  
   float salary = 60000;   
 };  
   class Programmer: public Account {  
   public:  
   float bonus = 5000;    
   };       
int main(void) {  
     Programmer p1;  
     cout<<"Salary: "<<p1.salary<<endl;    
     cout<<"Bonus: "<<p1.bonus<<endl;    
    return 0;  
 
Output:-
         Salary: 60000
         Bonus: 5000

2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one sub class is inherited from more than one base classes.
Syntax:-
class subclass_name : access_mode base_class1, access_mode base_class2, ....
{
  //body of subclass
};

Example:-
#include <iostream>
using namespace std;
class A {
public A()
{
     cout<<"Constructor of A class"<<endl;
 }
};

class B {
public B()
{
     cout<<"Constructor of B class"<<endl;
 }
};

class C: public A, public B {
public C()
{
     cout<<"Constructor of C class"<<endl;
 }
};

int main() {
   //Creating object of class C
   C obj;
   return 0;
}

Output:-
           Constructor of A class
           Constructor of B class
           Constructor of C class

3. Multilevel Inheritance: In this type of inheritance, a derived class is created from another derived class.
 Syntax:-
class subclass1_name : access_mode base_class2
{
  //body of subclass
};
class derivedclass_name : access_mode base_class1
{
  //body of subclass
};

Example:-
#include <iostream>
using namespace std;
class A 
{
   public A(){
     cout<<"Constructor of A class"<<endl;
 }
};
class B: public A 
{
   public B(){
     cout<<"Constructor of B class"<<endl;
  }
};
class C: public B 
{
   public C(){
     cout<<"Constructor of C class"<<endl;
  }
};
int main() {
  //Creating object of class C
  C obj;
  return 0;
}

Output:-
        Constructor of A class
        Constructor of B class
        Constructor of C class

4. Hierarchical Inheritance: In this type of inheritance, more than one sub class is inherited from a single base class. i.e. more than one derived class is created from a single base class.

Example:-
#include <iostream> 
using namespace std; 
  
// base class 
class Vehicle  
  public Vehicle() 
    { 
      cout << "This is a Vehicle" << endl; 
    } 
}; 
  
 // first sub class  
class Car: public Vehicle 
  
}; 
  
// second sub class 
class Bus: public Vehicle 
      
}; 
  
int main() 
{    
    // creating object of sub class will 
    // invoke the constructor of base class 
    Car obj1; 
    Bus obj2; 
    return 0; 

Output:-
        This is a Vehicle
        This is a Vehicle

 5. Hybrid Inheritance:- Hybrid inheritance is a combination of more than one type of inheritance.A derived class with two base classes and these two base classes have one common base class is called multipath inheritance.

 

Example:-
#include <iostream> 
using namespace std; 
  // base class  
class Vehicle  
  public Vehicle() 
    { 
      cout << "This is a Vehicle" << endl; 
    } 
}; 
  
//base class 
class Fare 
    public Fare() 
    { 
        cout<<"Fare of Vehicle\n"; 
    } 
}; 
  
// first sub class  
class Car: public Vehicle 
  }; 
  
// second sub class 
class Bus: public Vehicle, public Fare 
 }; 
  
int main() 
{    
    // creating object of sub class will 
    // invoke the constructor of base class 
    Bus obj2; 
    return 0; 

Output:-
      This is a Vehicle
      Fare of Vehicle


What is protected inheritance?

When a class is being derived from another class, we can make use of access specifiers. This is essentially useful to control the access the derived class members have to the base class.
When inheritance is protected :

i. Private members of base class are not accessible to derived class.
ii. Protected members of base class remain protected in derived class.
iii. Public members of base class become protected in derived class.

Post a Comment

0 Comments