Summary: In this post, we will learn about inheritance in C++ with examples.
Introduction to Inheritance in C++
Inheritance is one of the properties of any object-oriented programming language.
It allows a class to reuse the properties of another class i.e. inheritance promotes code reusability.
The properties of the class include member variables and member functions.
Consider the following example.
An Account in a bank can be of different types, depending on the interest rate or deposit and withdrawal limit or any other factor.
Such types of bank account could be:
- Savings Account
- Current Account
- Recurring Deposit Account
All the account types listed above has few things in common and some of them are:
- Balance
- Deposit
- Withdraw
So if you are asked to create a class for each of the 3 accounts listed above and provide deposit and withdraw function for all accounts, What you will do?
If you don’t know inheritance than you would end up creating three separate classes for each account type and write the deposit and withdraw function for each of them.
It is not efficient to write the same code for deposit and withdraw for every class.
Writing the same code, again and again, is a great demerit in the programming world. Here is why we use Inheritance.
With Inheritance, you only need to specify the withdraw and deposit function for a single class (account), the rest of the class will derive the deposit and withdraw functionality from the parent class.
It is possible because inheritance allows one class to reuse the code of another class.
Example of Inheritance in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include <iostream> using namespace std; class Account{ public: double balance; Account(){ balance = 0.0; } void deposit(double amt){ balance += amt; cout << "Amount Deposited" << endl; } void withdraw(double amt){ balance -= amt; cout << "Amount Withdrawn" << endl; } }; class SavingsAccount :public Account{ public: double interestRate; SavingsAccount(){ balance = 0.0; interestRate = 5.1; } }; int main() { SavingsAccount sa; sa.deposit(1000); cout << "Savings Account Balnce: "<< sa.balance << endl; } |
Output
The program has two class Account & SavingAccount. Both the account type/class has 3 things in common:
- Balance
- Deposit
- withdraw
Therefore in spite of writing the same methods for SavingAccount class, with inheritance, we are using the methods of Account class.
SavingAccount class is extending to Account class i.e SavingAccount is deriving resource from Account class. That’s why we have not declared balance, deposit & withdraw in SavingAccount. Therefore
- SavingAccount: is Derive Class
- Account: is Base Class
By seeing the output we can say that SavingAccount class is using Account class resources. In short, SavingAccount inherits Account.
Types of Inheritance in C++
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
Hence we have successfully implemented inheritance in C++ using an example. If you have any doubts regarding inheritance than do comment below.