- Examples
To get a deeper understanding of inheritance, we'll look at a few examples in this lesson.
Example 1: Inheritance #
Press + to interact
#include <iostream>class Account{public:Account(double b): balance(b){}void deposit(double amt){balance += amt;}void withdraw(double amt){balance -= amt;}double getBalance() const {return balance;}private:double balance;};class BankAccount: public Account{public:// using Account::Account;BankAccount(double b): Account(b){}void addInterest(){deposit( getBalance()*0.05 );}};int main(){std::cout << std::endl;BankAccount bankAcc(100.0);bankAcc.deposit(50.0);bankAcc.deposit(25.15);bankAcc.withdraw(30);bankAcc.addInterest();std::cout << "bankAcc.getBalance(): " << bankAcc.getBalance() << std::endl;std::cout << std::endl;}
Explanation #
-
We have created two classes, i.e.,
Account
andBankAccount
. -
The
BankAccount
class inherits theAccount
class publicly in line 25. -
The
public
member functions of theAccount
class are available to theBankAccount
class and we can access them using the.
...