Inheritance in C++

                                                         

                              C++ Inheritance


  • In C++, inheritance may be a process during which one object acquires all the properties and behaviors of its parent object automatically. In such way, you'll reuse, extend or modify the attributes and behaviors which are defined in other class.
  • In C++, the category which inherits the members of another class is named the derived class and therefore the class whose members are inherited is named the bottom class. The derived class is that the specialized class for the bottom class.
  • When creating a category rather than writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is named the bottom class, and therefore the new class is mentioned because the derived class.

  • Base and Derived Classes

A class is often derived from quite one class, which suggests it can inherit data and functions from multiple base classes. To define a derived class, we use a category derivation list to specify the bottom class(es). a category derivation list names one or more base classes and has the shape −class derived-class: 
class derived-class: access-specifier base-class
access-specifier base-class where access-specifier is one among public, protected, or private, and base-class is that the name of a previously defined class. If the access-specifier isn't used, then it's private by default.

Consider a base class Shape and its derived class Rectangle as follows −

#include <iostream>

using namespace std;

class Shape

{

   public:

      void setWidth(int w)

      {

         width = w;

      }

      void setHeight(int h)

      {

         height = h;

      }

   protected:

      int width;

      int height;

};

class Rectangle: public Shape

{

   public:

      int getArea()

      {

         return (width * height);

      }

};

int main(void)

{

   Rectangle Rect;

   Rect.setWidth(6);

   Rect.setHeight(9);

   cout << "Total area: " << Rect.getArea() << endl;

   return 0;

}

The output of this program is like this,: - 
Fig (1).Output of Program

  • Types Of Inheritance: -
  • Single inheritance
  • Multiple inheritances
  • Hierarchical inheritance
  • Multilevel inheritance
  • Hybrid inheritance

  1. C++ Single Inheritance: -
Single inheritance is defined as the inheritance in which a derived class is inherited from only one base class.
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 Software = 100000;  

 }; 

   class Programmer: public Account

   { 

   public: 

   float Mangement = 50000;   

   };      

int main(void)

     Programmer p1; 

     cout<<"Software: "<<p1.Software<<endl;   

     cout<<"Mangement: "<<p1.Mangement<<endl;   

    return 0; 

}  

The output of this program is like this,: - 
Fig(2).Output of Program 

  • How to make a Private Member Inheritable

The private member is not inheritable. If we modify the visibility mode by making it public, but this takes away the advantage of data hiding.

C++ introduces a third visibility modifier, i.e., protected. The member which is declared as protected will be accessible to all the member functions within the class as well as the class immediately derived from it.

Visibility modes can be classified into three categories:

  • Public: When the member is declared as public, it is accessible to all the functions of the program.
  • Private: When the member is declared as private, it is accessible within the class only.
  • Protected: When the member is declared as protected, it is accessible within its own class as well as the class immediately derived from it.

  • C++ Multilevel Inheritance

Multilevel inheritance is the process of deriving a class from another derived class.

When one class inherits another class which is further inherited by another class, it is known as a 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; 

}   



The output of this program is like this,: - 
Fig(3). The output of this program



  • C++ Multiple Inheritance: -
Multiple inheritances are 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 inheritances.

#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(40); 

   c.get_b(80); 

   c.display(); 

    return 0; 

}  



The output of this program is like this,: - 

Fig (4).The output of this program




  • C++ Hybrid Inheritance: -
Hybrid inheritance is a combination of more than one type of inheritance.

Let's see a simple example:


#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; 

}  



The output of this program is like this,: - 
Fig(5). Output of program



  • C++ 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                 

    public: 

    int a; 

    int b; 

    void get_data(int n,int m) 

    { 

        a= n; 

        b = m; 

    } 

}; 

class Rectangle : public Shape 

    public: 

    int rect_area() 

    { 

        int result = a*b; 

        return result; 

    } 

}; 

class Triangle : public Shape

    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; 

}  


The output of this program is like this: -

Fig(6).The output of this program




  • Conclusion: -
  • Inheritance promotes reusability. When a category inherits or derives another class, it can access all the functionality of the inherited class. Reusability enhanced reliability.
  • The bottom class code is going to be already tested and debugged.
  • As the existing code is reused, it results in less development and maintenance costs. Inheritance makes the subclasses follow a typical interface. Inheritance helps to scale back code redundancy and supports code extensibility. Inheritance facilitates the creation of sophisticated libraries.

-Sanjyot Mankar

Comments