Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance in C++ Content adapted from a lecture by Dr Soo Yuen Jien.

Similar presentations


Presentation on theme: "Inheritance in C++ Content adapted from a lecture by Dr Soo Yuen Jien."— Presentation transcript:

1

2 Inheritance in C++ Content adapted from a lecture by Dr Soo Yuen Jien

3 class BankAccount { private: int _num; int _bal; public: BankAccount(int num, int bal){ _num = num; _bal = bal; } //withdraw method //deposit method //print method }; Bank AccountSaving Account class SavingAccount { private: int _num; int _bal; int _rate; public: SavingAccount(int num, int bal, int rate){ _num = num; _bal = bal; _rate = rate; } //withdraw method //deposit method //print method //pay_interest method }; Let’s say we wrote a BankAccount class to represent a bank account.It has some private attributes,a constructor,and some public methods. Now, we want to implement a SavingAccount that also has the same attributes and methods, but in addition, has an extra attribute to store the interest rate and an extra method to pay interest.

4 class SavingAccount { private: int _num; int _bal; int _rate; public: SavingAccount(int num, int bal, int rate){ _num = num; _bal = bal; _rate = rate; } //withdraw method //deposit method //print method //pay_interest method }; Bank AccountSaving Account class BankAccount { private: int _num; int _bal; public: BankAccount(int num, int bal){ _num = num; _bal = bal; } //withdraw method //deposit method //print method }; class BankAccount { private: int _num; int _bal; public: BankAccount(int num, int bal){ _num = num; _bal = bal; } //withdraw method //deposit method //print method }; class BankAccount { private: int _num; int _bal; public: BankAccount(int num, int bal){ _num = num; _bal = bal; } //withdraw method //deposit method //print method }; One solution is to write the SavingAccount class by copy pasting the BankAccount code,

5 Bank AccountSaving Account class SavingAccount { private: int _num; int _bal; int _rate; public: SavingAccount(int num, int bal, int rate){ _num = num; _bal = bal; _rate = rate; } //withdraw method //deposit method //print method //pay_interest method }; class BankAccount { private: int _num; int _bal; public: BankAccount(int num, int bal){ _num = num; _bal = bal; } //withdraw method //deposit method //print method }; and modifying as necessary. That however results in a high level of code duplication. Duplicated code is hard to maintain because any change to one copy needs to be replicated in all copies of the code.

6 Bank AccountSaving Account class SavingAccount { private: int _num; int _bal; int _rate; public: SavingAccount(int num, int bal, int rate){ _num = num; _bal = bal; _rate = rate; } //withdraw method //deposit method //print method //pay_interest method }; class BankAccount { private: int _num; int _bal; public: BankAccount(int num, int bal){ _num = num; _bal = bal; } //withdraw method //deposit method //print method };

7 class BankAccount { private: int _num; int _bal; public: BankAccount(int num, int bal){ _num = num; _bal = bal; } //withdraw method //deposit method //print method }; class SavingAccount : public BankAccount { private: int _rate; public: SavingAccount(int num, int bal, int rate) :BankAccount(num, bal){ _rate = rate; } //pay_interest method }; BankAccount SavingAccount (parent class, super class) (child class, sub-class) protected: inherited Bank AccountSaving Account Instead, we can use inheritance to define the SavingAccount based on the existing BankAccount class.This is the syntax for declaring SavingAccount as a sub-class of BankAccount class. That way, all attributes and methods of the BankAccount class are inherited by the SavingAccount class, without needing to define them again. But note that we need to change the visibility of these attributes for them to be visible to the SavingAccount class. Private members are not visible to sub classes. The sub class needs to contain extra attributes and methods only. There is no need to repeat the inherited attributes and methods. Because the sub-class is defined based on the parent class, the constructor of the sub-class needs to call the constructor of the super class. This is the syntax for that.

8 class BankAccount { private: int _num; int _bal; public: BankAccount(int num, int bal){ _num = num; _bal = bal; } //withdraw method //deposit method //print method }; class SavingAccount : public BankAccount { private: int _rate; public: SavingAccount(int num, int bal, int rate) :BankAccount(num, bal){ _rate = rate; } //pay_interest method }; BankAccountSavingAccount (parent class, super class) (child class, sub-class) protected: inherited Bank AccountSaving Account

9 int main( ){ SavingAccount sa( 8888, 999, 2 ); sa.deposit(1000); sa.payInterest(); } Inherited method from super class BankAccount New method in SavingAccount class class BankAccount { protected: int _num; int _bal; public: BankAccount(int num, int bal){ _num = num; _bal = bal; } //withdraw method //deposit method //print method }; class SavingAccount : public BankAccount { private: int _rate; public: SavingAccount(int num, int bal, int rate) :BankAccount(num, bal){ _rate = rate; } //pay_interest method }; Now, let’s see how we use the inherited classes.First, we create a SavingAccount object.Then, we can call methods it inherited from the super class,and methods defined in the sub class too.

10 class BankAccount { protected: int _num; int _bal; public: BankAccount(int num, int bal){ _num = num; _bal = bal; } //withdraw method //deposit method //print method }; class SavingAccount : public BankAccount { private: int _rate; public: SavingAccount(int num, int bal, int rate) :BankAccount(num, bal){ _rate = rate; } //pay_interest method }; void transfer(BankAccount& fromAcct, BankAccount& toAcct, int amt){ fromAcct.withdraw( amt ); toAcct.deposit( amt ); } int main( ) { BankAccount ba( 1, 234 ); SavingAccount sa( 2, 2000, 3 ); transfer( ba, sa, 234 ); } void transfer(BankAccount& fromAcct, BankAccount& toAcct, int amt){ fromAcct.withdraw( amt ); toAcct.deposit( amt ); } int main( ) { BankAccount ba( 1, 234 ); SavingAccount sa( 2, 2000, 3 ); transfer( ba, sa, 234 ); } Super class expected Sub-class given Let’s say we created this method to transfer money from one BankAccount to another BankAccount.Here, we are passing a SavingAccount object to that method although it expects a BankAccount object. It still works because of the substitutability principle: If the code expects an object of the super class, it will also work with an object of the sub class.

11 class BankAccount { protected: int _num; int _bal; public: BankAccount(int num, int bal){ _num = num; _bal = bal; } //withdraw method //deposit method void print() { cout << "Number:" << _num ; cout << "\nBalance:", << _bal ; } }; class SavingAccount : public BankAccount { private: int _rate; public: SavingAccount(int num, int bal, int rate) :BankAccount(num, bal){ _rate = rate; } //pay_interest method void print() { cout << "Number:" << _num ; cout << "\nBalance:", << _bal ; cout << "\nRate:" << _rate ; } }; overrides Rate:2 e.g. Number:1234 Balance:100 e.g. Number:1234 Balance:100 Suppose this is the print method in the BankAccount class.Currently, it is inherited by the SavingAccount class too.What if we want SavingAccount objects to print in a different way?In that case, we can override the print method in the SavingAccount class by simply re-implementing it.

12 class BankAccount { protected: int _num; int _bal; public: BankAccount(int num, int bal){ _num = num; _bal = bal; } //withdraw method //deposit method void print() { cout << "Number:" << _num ; cout << "\nBalance:", << _bal ; } }; class SavingAccount : public BankAccount { private: int _rate; public: SavingAccount(int num, int bal, int rate) :BankAccount(num, bal){ _rate = rate; } //pay_interest method void print() { cout << "Number:" << _num ; cout << "\nBalance:", << _bal ; cout << "\nRate:" << _rate ; } }; BankAccount::print(); Notice how these lines are duplicated.To avoid this duplication, we can simply call the method of the super class from the sub class.

13 class BankAccount { protected: int _num; int _bal; public: BankAccount(int num, int bal){ _num = num; _bal = bal; } //withdraw method //deposit method void print() { cout << "Number:" << _num ; cout << "\nBalance:", << _bal ; } }; class SavingAccount : public BankAccount { private: int _rate; public: SavingAccount(int num, int bal, int rate) :BankAccount(num, bal){ _rate = rate; } //pay_interest method void print() { cout << "Number:" << _num ; cout << "\nBalance:", << _bal ; cout << "\nRate:" << _rate ; } }; BankAccount::print(); BankAccount SavingAccount (parent class, super class) (child class, sub-class) Let’s summarize. When a class inherits from another class,it inherits attributes,and methods.The constructor of the child class should call one of the constructors of the super class.The sub class can override methods from the super class.It can call methods from the super class too.

14 Inheritance in C++ Content adapted from a lecture by Dr Soo Yuen Jien http://PowerPointLabs.info Created using

15 14 --- 12 3 [ CG1103 AY1011S2 Lecture 2a ]


Download ppt "Inheritance in C++ Content adapted from a lecture by Dr Soo Yuen Jien."

Similar presentations


Ads by Google