Summary: In this post, we will learn what inheritance is and how can we inherit a class to another in C++.

Introduction to Inheritance

Inheritance is one of the important features of an object-oriented programming language that promotes code reusability.

This allows one class to reuse the properties of another class i.e. we can reuse one class’s methods or variables in another.

For example, if there is a method in class A that we want to reuse in class B, we can inherit Class A in Class B to gain access to that method.

In inheritance, the class that inherits is called a sub or derived class and the class that is inherited is called super or base class.

Syntax to Inherit a class in C++

class A{
    //code
};

class B: <public|protected|private> class B{
    //code
};  

The access specifier determines the mode of inheritance.

PublicThe access level of the properties in class A remains the same in class B.
ProtectedThe access level of the public properties in class A reduces to protected in class B.
PrivateThe access level of the public and protected properties in class A reduces to private in class B (i.e. all properties of class A become private in class B).
Different modes of Inheritance.

When to Use Inheritance in C++?

Inheritance implies IS A relationship between class.

We should check if such a relationship could exist between the classes, if so, then we should inherit accordingly.

For example, the dog is an animal. It would be best to inherit the Animal class in the Dog class.

C++ Example: Inheritance

Let see an example to understand the actual significance of inheritance in C++.

#include <iostream>
using namespace std;
 
class Account{
public:
    double balance;
    Account(){
        this->balance = 0.0;
    }
 
    void deposit(double amt){
        this->balance += amt;
        cout << "Amount Deposited" << endl;
    }
 
    void withdraw(double amt){
        this->balance -= amt;
        cout << "Amount Withdrawn" << endl;
    }
};
 
//inherting Account class 
class SavingsAccount: public Account{
public:
    double interestRate;
 
    SavingsAccount(){
        this->balance = 0.0;
        this->interestRate = 5.1;
    }
};
 
int main()
{
   SavingsAccount sa;
   sa.deposit(1000);
 
   cout << "Savings Account Balnce: "<< sa.balance << endl;
}

Output:

inheritance example in c++

Since a saving account is a type of bank account, in the above example, we have inherited the Account in the SavingAccount class to derive balance, deposit & withdraw from it.

Here, SavingAccount is a derive class and Account is a base class.

In the above example, we have leveraged the inheritance feature to reuse the method and properties of the Account class.

Since we have used inherited the Account class in public mode, the private and public properties of the Account class maintains the same access level in the SavingAccount class.

Types of Inheritance in C++

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance
Types of inheritance in C++

Every kind of heritage has its own importance. We must apply them when applicable to reuse the code and reduce the complexity of the program.

Leave a Reply

5 × one =