Inheritance in C++ programming

21 Feb 2023
Intermediate
327 Views

Inheritance in C++ programming

Introduction

Are you a computer programmer who is trying to get your bearings in the world of C++ programming? Or maybe a novice techie trying to pick up the fundamentals of language-based coding? Either way, once you've mastered some basic programming concepts, one of the key areas that can take your skills to the next level is understanding inheritance in C++. By mastering inheritance principles in C++ programming language and applying them correctly, any coder will soon find programs working smoother, and faster and would have made life much easier for even more complex projects. We'll be taking an in-depth look at this particular topic today - so settle down with us as we discuss all there is about Inheritance in C++!

What is Inheritance in C++

Inheritance in C++ programming language is a powerful tool for developers to take advantage of while coding. It allows classes to be related to each other so that a child class can inherit from a parent class and access the same methods, functions, and variables that the parent class has. This inheritance structure makes it easier for developers to structure any large projects and create code reuse. The inheritance model provides a simple and effective way for a development team to manage complexity in their project by abstracting away details from the child classes. In terms of coding, inheritance allows developers to build programs in c++ programming language around abstractions rather than data objects, further, it simplifies their projects while increasing speed, performance, maintainability and robustness.

Advantages of Inheritance in C++

There are some advantages of inheritance in c++ programming language
1. Code Reusability: Inheritance allows the programmer to reuse the code which is already written in the base class. This helps to reduce the amount of code a programmer needs to write and makes the process of development of the program faster and easier.
2. Easier Maintenance: By using inheritance, programmers of the c++ programming language can make changes to the base class and all the derived classes will automatically get the changes. This makes maintenance of the code much easier.
3. Polymorphism: Inheritance allows the developer of the program to create different classes that share the same properties, features, and methods. This particular tool allows the developer to create a single interface for multiple classes and then they can use them interchangeably.
4. Extensibility: the programmer of the c++ programming language can easily extend the functionality of the existing classes by adding new properties and methods to the derived classes. This makes it easy to add new features to the application without any need to rewrite the existing code.
5. Readability: Inheritance makes the code more readable and understandable. By looking at the base class, the programmer as well as the user can easily understand the structure of the derived classes.

Types of Inheritance in c++

There are 5 types of inheritance in C++ programming language. Those are
  • Single Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Multilevel Inheritance
  • Hybrid Inheritance

Single Inheritance

Single Inheritance in c++ programming language is defined as a type of Inheritance in which a derived class is generally inherited from one and only base class.

Example

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

Output

Salary: 75000
Bonus: 10000
Multiple Inheritance
Multiple Inheritance in c++ programming language is a process of deriving a new class that inherits some particular attributes from two or more classes.

Example

#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<<"The addition of a and b is : "<<a+b;
    }
};
int main()
{
   C c;
   c.get_a(120);
   c.get_b(80);
   c.display();
    return 0;
}

Output

The value of a is : 120
The value of b is : 80
The addition of a and b is : 200

Hierarchical Inheritance

Hierarchical Inheritance in c++ programming language can be defined as the process of deriving one class from the parent class.

Example

#include <iostream>
using namespace std;
class Shape // Declaration of base class.
{
    public:
    int X;
    int Y;
    void get_data(int n,int m)
    {
        X= n;
        Y= m;
    }
};
class Rectangle : public Shape // inheriting Shape class
{
    public:
    int rect_area()
    {
        int result = X*Y;
        return result;
    }
};
class Triangle : public Shape // inheriting Shape class
{
    public:
    int triangle_area()
    {
        float result = 0.5*X*Y;
        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:
24
20
Area of the rectangle is : 480
Enter the base and height of the triangle:
2
5
Area of the triangle is : 5

Multilevel Inheritance

Multilevel Inheritance in c++ programming language assists to derive a class from another derived class.

Example

#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 sleep()
     {
    cout<<"Sleeping...";
     }
   };
int main(void)
{
    BabyDog d1;
    d1.eat();
    d1.bark();
    d1.sleep();
     return 0;
}

Output

Eating...
Barking...
Sleeping...

Hybrid Inheritance

Hybrid Inheritance in c++ programming language is a combination of more than one type of inheritance

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

Output

Enter the value of 'a' :
120
Enter the value of 'b' :
20
Enter the value of c is :
310
Multiplication of a,b,c is : 744000

Visibility mode in Inheritance in C++

In c++ programming language when a base class is derived by a derived class with the help of the functionality of the inheritance then the accessibility of the base class is controlled by the visibility modes. There are three types of visibility modes present in c++ programming language. Those are
  • Public- if the member is declared as a public member then it is accessible to all the functions of c++ program
  • Private- if the member is declared as a private member the it is only accessible by some particular class
  • Protected- if the member is declared as a protected member then only its own class and the derived class can only access them

Visibility of the inherited member

Base class visibilityDerived class visibility
PublicPrivateProtected
PrivateNot InheritedNot InheritedNot Inherited
ProtectedProtectedPrivateProtected
PublicPublicPrivateProtected

Summary

Learning about inheritance in C++ programming can help you better understand how objects are related to one another. This can be a helpful tool when working with large programs. If you want to learn more about inheritance in C++ programming this article can give you a vast idea about inheritance, other than this there are many resources available online and in libraries. Do some research to find the resources that will work best for you and your learning style. You can also find helpful resources on our website. inheritance in c++ including visibility mode in c++ provides a lot of benefits for programmers. It helps them to reuse code and make their programs more efficient. Thanks for reading!

Accept cookies & close this