Presentation is loading. Please wait.

Presentation is loading. Please wait.

John Byrne Inheritance Bank account examples. John Byrne Example account hierarchy F account relationships: ACCOUNT SAVINGSCHEQUE TIME_ACCT.

Similar presentations


Presentation on theme: "John Byrne Inheritance Bank account examples. John Byrne Example account hierarchy F account relationships: ACCOUNT SAVINGSCHEQUE TIME_ACCT."— Presentation transcript:

1 John Byrne Inheritance Bank account examples

2 John Byrne Example account hierarchy F account relationships: ACCOUNT SAVINGSCHEQUE TIME_ACCT

3 John Byrne About the classes F Account is the most general methods include accept balance, return balance members include balance SAVINGS provides interest and withdrawal CHEQUE provides cheque facilities, there is a charge per cheque if balance is below limit TIME_ACCT only accumulated interest can be withdrawn

4 John Byrne The Declarations F In the header file (.h) class ACCOUNT{ protected: double dbalance; public: ACCOUNT(double bal = 0.0) { balance = bal;} void deposit( double amount) {dbalance += amount;} double get_balance() const {return dbalance;} };

5 John Byrne Main points The functions are so small their definitions can appear in the header protected - means that any function in this class or any function in a derived class can gain access

6 John Byrne Main points contd On construction the ACCOUNT can be sent a value, if no value is supplied then the balance is set to 0.0 - the DEFAULT argument

7 John Byrne SAVINGS Declarations class SAVINGS: public ACCOUNT{ protected: double drate; public: SAVINGS(double bal=0.0,double pct=0.5); double compound(); double withdraw(double amt); };

8 John Byrne Definitions for SAVINGS # include “accounts.h” SAVINGS::SAVINGS(double bal, double pct):ACCOUNT(bal) { drate = pct/100; }

9 John Byrne Definitions Contd double SAVINGS::compound() { double dinterest = drate * dbalance; dbalance += dinterest; return dinterest; }

10 John Byrne More Declarations class TIME_ACCT: public SAVINGS{ protected: double dfunds_avail; public: TIME_ACCT( double bal = 0.0, double pct =0.5); double compound(); //redefinition double withdraw(double amt); //redefinition double get_avail() const {return dfunds_avail;} }; // end class declarations

11 John Byrne Constructor defined TIME_ACCT::TIME_ACCT(double bal, double pct):SAVINGS(bal,pct) { dfunds_avail = 0.0; }

12 John Byrne TIME_ACCT double TIME_ACCT::compound() { double dinterest = SAVINGS::compound(); dfunds_avail += dinterest; return dinterest; }//function keeps track of funds

13 John Byrne MAIN POINTS Constructors - calling the parent class can lead to constructors being automatically called at points higher in the hierarchy eg TIME_ACCT - calls SAVINGS - calls ACCOUNT ! Functions can be overridden see the redefinitions

14 John Byrne Multiple inheritance F multiple inheritance is allowed in C++ F unlike Java and C# -allowed to have multiple inheritance F this can cause problems if two sides of the inheritance family have a common ancestor Account SavingsAccount CurrentAccount CurrentSavingsAccount

15 John Byrne Multiple Inheritance F what if we wanted to create a CurrentSavings account? F we would call the Account constructor twice F and end up with two copies of the balance member variable -need to inherit with keyword virtual F we can also encounter problems if two different super classes have attributes with the same name -qualify them with the class name

16 John Byrne Private inheritance F unlike Java, C++ also allows private inheritance F the derived class can still access the public and protected members of the base class but they are not visible outside the class F useful if you want to adapt an existing class to use a new interface -different signatures for the public methods -use appropriate methods of the superclass F Class Adapter pattern

17 John Byrne Private inheritance class AdaptedAccount : private Account { public: AdaptedAccount(double initialBalance) : Account(initialBalance) {} double getMoney() { return getBalance(); } void putInMoney(double amount) { deposit(amount); } void takeOutMoney(double & amount) { withdraw(amount);} };

18 John Byrne EXTRA TUTORIAL F Complete the inheritance example covered in the lecture F Additional if time permits - Create a function which can take two accounts as parameters: one a CHEQUE account and one a SAVINGS account and return the result of adding the balance from each account


Download ppt "John Byrne Inheritance Bank account examples. John Byrne Example account hierarchy F account relationships: ACCOUNT SAVINGSCHEQUE TIME_ACCT."

Similar presentations


Ads by Google