inheritance in c++ content adapted from a lecture by dr soo yuen jien

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

Upload: marilynn-carol-hodge

Post on 01-Jan-2016

222 views

Category:

Documents


3 download

TRANSCRIPT

More on C++

Inheritance in C++Content adapted from a lecture by Dr Soo Yuen Jienclass BankAccount {

private: int _num; int _bal;

public: BankAccount(int num, int bal){ _num = num; _bal = bal;

} //withdraw method //deposit method //print method

};Bank AccountSaving Accountclass 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};Lets 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.

Lets say we wrote a BankAccount class to represent a bank account. [afterclick] It has some private attributes,[afterclick] a constructor, [afterclick] and some public methods.[afterclick] 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.2class 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 Accountclass 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,

One solution is to write the SavingAccount class by copy pasting the BankAccount code,3Bank AccountSaving Accountclass 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.

and modifying as necessary. [afterclick] 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.4Bank AccountSaving Accountclass 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

};5class 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: inheritedinheritedBank AccountSaving AccountInstead, 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.

Instead, we can use inheritance to define the SavingAccount based on the existing BankAccount class. [afterclick] This is the syntax for declaring SavingAccount as a sub-class of BankAccount class.[afterclick] That way, all attributes and methods of the BankAccount class are inherited by the SavingAccount class, without needing to define them again. [afterclick] 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.[afterclick] The sub class needs to contain extra attributes and methods only. There is no need to repeat the inherited attributes and methods.[afterclick] 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.6class 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: inheritedinheritedBank AccountSaving Account

int main( ){ SavingAccount sa( 8888, 999, 2 );

sa.deposit(1000);

sa.payInterest();}Inherited method from super class BankAccount New method in SavingAccount classclass 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, lets 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.

Now, lets see how we use the inherited classes.[afterclick] First, we create a SavingAccount object.[afterclick] Then, we can call methods it inherited from the super class,[afterclick] and methods defined in the sub class too.8class 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 ); }Super class expectedSub-class givenLets 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.

Lets say we created this method to transfer money from one BankAccount to another BankAccount.[afterclick] Here, we are passing a SavingAccount object to that method although it expects a BankAccount object. [afterclick] 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.9class BankAccount {

protected: int _num; int _bal;

public: BankAccount(int num, int bal){ _num = num; _bal = bal;

} //withdraw method //deposit method void print() { cout