Inheritance in C++

inheritance in c plus plus

The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object Oriented Programming. 

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

We will also see what are advantages and disadvantages of inheritance in c++

Types of Inheritance in C++

  1. Single inheritance
  2. Multiple inheritance
  3. Hierarchical inheritance
  4. Multilevel inheritance
  5. Hybrid inheritance

Sub Class: The class that inherits properties from another class is called Sub class or Derived Class. 
Super Class:The class whose properties are inherited by sub class is called Base Class or Super class. 

What is visibility mode or access specifier ?

he visibility mode specifies whether the features of the base class are publicly inherited or privately inherited. It can be public or private.

Note: In C++, the default mode of visibility is private.

Note: The private members of the base class are never inherited.

Types of visibility mode

  1. Public
  2. Private
  3. Protected

Public :  members are accessible from outside the class

Private : members cannot be accessed (or viewed) from outside the class

Protected: members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

What is single inheritance?

Single inheritance is defined as the inheritance in which a derived class is inherited from the only one base class.

for example:

Where ‘A’ is the base class, and ‘B’ is the derived class.

Example of single inheritance ?

When one class inherits another class, it is known as single level inheritance. Let’s see the example of single level inheritance which inherits the fields only.

#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

Note: In the above example, Employee is the base class and Programmer is the derived class.

What is multiple inheritance?

Multiple inheritance is the process of deriving a new class that inherits the attributes from two or more classes.

Syntax of the Derived class:

class D : visibility B-1, visibility B-2, ?  
{  
    // Body of the class;  
}   

Let’s see a simple example of multiple inheritance.

#include <iostream>  
using namespace std;  
class A  
{  
    protected:  
     int a;  
    public:  
    void get_a(int n)  
    {  
        a = n;  
    }  
};  
  
class B  
{  
    protected:  
    int b;  
    public:  
    void get_b(int n)  
    {  
        b = n;  
    }  
};  
class C : public A,public B  
{  
   public:  
    void display()  
    {  
        std::cout << "The value of a is : " <<a<< std::endl;  
        std::cout << "The value of b is : " <<b<< std::endl;  
        cout<<"Addition of a and b is : "<<a+b;  
    }  
};  
int main()  
{  
   C c;  
   c.get_a(10);  
   c.get_b(20);  
   c.display();  
  
    return 0;  
}  

Output:

The value of a is : 10
The value of b is : 20
Addition of a and b is : 30

Note: In the above example, class ‘C’ inherits two base classes ‘A’ and ‘B’ in a public mode.

Ambiquity Resolution in Inheritance

Ambiguity can be occurred in using the multiple inheritance when a function with the same name occurs in more than one base class.

Let’s understand this through an example:

#include <iostream>  
using namespace std;  
class A  
{  
    public:  
    void display()  
    {  
        std::cout << "Class A" << std::endl;  
    }  
};  
class B  
{  
    public:  
    void display()  
    {  
        std::cout << "Class B" << std::endl;  
    }  
};  
class C : public A, public B  
{  
    void view()  
    {  
        display();  
    }  
};  
int main()  
{  
    C c;  
    c.display();  
    return 0;  
}  

Output:

error: reference to 'display' is ambiguous
        display();

The above issue can be resolved by using the class resolution operator with the function. In the above example, the derived class code can be rewritten as:

class C : public A, public B  
{  
    void view()  
    {  
        A :: display();         // Calling the display() function of class A.  
        B :: display();         // Calling the display() function of class B.  
  
    }  
};  

An ambiguity can also occur in single inheritance.

Consider the following situation:

class A  
{  
   public:  
void display()  
{  
   cout<<?Class A?;  
}   
} ;  
class B  
{   
  public:  
 void display()  
{  
 cout<<?Class B?;  
}  
} ;  

In the above case, the function of the derived class overrides the method of the base class. Therefore, call to the display() function will simply call the function defined in the derived class. If we want to invoke the base class function, we can use the class resolution operator.

int main()  
{  
    B b;  
   b.display();               // Calling the display() function of B class.  
   b.B :: display();       // Calling the display() function defined in B class.  
}   

What is Hierarchical Inheritance?

Hierarchical inheritance is defined as the process of deriving more than one class from a base class.

Syntax of Hierarchical inheritance:

class A  
{  
    // body of the class A.  
}    
class B : public A   
{  
    // body of class B.  
}  
class C : public A  
{  
    // body of class C.  
}   
class D : public A  
{  
    // body of class D.  
}   

Let’s see a simple example:

#include <iostream>  
using namespace std;  
class Shape                 // Declaration of base class.  
{  
    public:  
    int a;  
    int b;  
    void get_data(int n,int m)  
    {  
        a= n;  
        b = m;  
    }  
};  
class Rectangle : public Shape  // inheriting Shape class  
{  
    public:  
    int rect_area()  
    {  
        int result = a*b;  
        return result;  
    }  
};  
class Triangle : public Shape    // inheriting Shape class  
{  
    public:  
    int triangle_area()  
    {  
        float result = 0.5*a*b;  
        return result;  
    }  
};  
int main()  
{  
    Rectangle r;  
    Triangle t;  
    int length,breadth,base,height;  
    std::cout << "Enter the length and breadth of a rectangle: " << std::endl;  
    cin>>length>>breadth;  
    r.get_data(length,breadth);  
    int m = r.rect_area();  
    std::cout << "Area of the rectangle is : " <<m<< std::endl;  
    std::cout << "Enter the base and height of the triangle: " << std::endl;  
    cin>>base>>height;  
    t.get_data(base,height);  
    float n = t.triangle_area();  
    std::cout <<"Area of the triangle is : "  << n<<std::endl;  
    return 0;  
}  

Output:

Enter the length and breadth of a rectangle:
23  
20  
Area of the rectangle is : 460          
Enter the base and height of the triangle:  
2   
5
Area of the triangle is : 5 

What is multilevel inheritance?

When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all its base classes.

Let’s see the example of multi level inheritance in C++.

#include <iostream>  
using namespace std;  
 class Animal {  
   public:  
 void eat() {   
    cout<<"Eating..."<<endl;   
 }    
   };  
   class Dog: public Animal   
   {    
       public:  
     void bark(){  
    cout<<"Barking..."<<endl;   
     }    
   };   
   class BabyDog: public Dog   
   {    
       public:  
     void weep() {  
    cout<<"Weeping...";   
     }    
   };   
int main(void) {  
    BabyDog d1;  
    d1.eat();  
    d1.bark();  
     d1.weep();  
     return 0;  
}  

Output:

Eating...
Barking...
Weeping...

What is hybrid inheritance?

Hybrid inheritance is a combination of more than one type of inheritance.

#include <iostream>  
using namespace std;  
class A  
{  
    protected:  
    int a;  
    public:  
    void get_a()  
    {  
       std::cout << "Enter the value of 'a' : " << std::endl;  
       cin>>a;  
    }  
};  
  
class B : public A   
{  
    protected:  
    int b;  
    public:  
    void get_b()  
    {  
        std::cout << "Enter the value of 'b' : " << std::endl;  
       cin>>b;  
    }  
};  
class C   
{  
    protected:  
    int c;  
    public:  
    void get_c()  
    {  
        std::cout << "Enter the value of c is : " << std::endl;  
        cin>>c;  
    }  
};  
  
class D : public B, public C  
{  
    protected:  
    int d;  
    public:  
    void mul()  
    {  
         get_a();  
         get_b();  
         get_c();  
         std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;  
    }  
};  
int main()  
{  
    D d;  
    d.mul();  
    return 0;  
}  

Output:

Enter the value of 'a' :
10              
Enter the value of 'b' :    
20      
Enter the value of c is :   
30  
Multiplication of a,b,c is : 6000

Advantages of inheritance in c++

  1. Inheritance promotes reusability. When a class inherits or derives another class, it can access all the functionality of inherited class.
  2. Reusability enhanced reliability. The base class code will be already tested and debugged.
  3. As the existing code is reused, it leads to less development and maintenance costs.
  4. Inheritance makes the sub classes follow a standard interface.
  5. Inheritance helps to reduce code redundancy and supports code extensibility.
  6. Inheritance facilitates creation of class libraries.

Disadvantages of inheritance in c++

  1. Inherited functions work slower than normal functions as there is indirection.
  2. Improper use of inheritance may lead to wrong solutions.
  3. Often, data members in the base class are left unused which may lead to memory wastage.
  4. Inheritance increases the coupling between the base class and derived class. A change in the base class will affect all the child classes.

0 0 votes
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x