1 / 25

Relation Between Derived-Class and Base-Class

Relation Between Derived-Class and Base-Class. Derived class object can use base class methods, if they are not private. Base class pointer can point to a derived class object A base class reference can refer to a derived class object

feoras
Download Presentation

Relation Between Derived-Class and Base-Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Relation Between Derived-Class and Base-Class • Derived class object can use base class methods, if they are not private. • Base class pointer can point to a derived class object • A base class reference can refer to a derived class object • A derived class reference or pointer can NOT refer to base class object.

  2. Public Inheritance is an Is-a relation. An object of derived class is an object of base class. Consider Fruit and Banana, Banana is a Fruit.

  3. public inheritance doesn’t model a has-a relationship. Consider Lunch and Fruit. In general fruit is not lunch, but lunch can have fruit. public inheritance doesn’t model a is-like-a relationship. Lawyers are like sharks, (some times) Don’t try to derived a Lawyer class from Shark. Otherwise lawyers must live underwater!

  4. public inheritance doesn’t model an is-implemented-as-a relationship. Stack and Array. Not proper to derive a Stack from Array class public inheritance doesn’t model a uses-a relationship. Computer can use printer, but we don’t derived printer class from computer class.

  5. Method with different form for derived class Sometimes we want a method to behave differently for the derived class. We can have multiple behaviors for a method which is called polymorphic Polymorphic Public Inheritance: Redefining base-class methods in derived class using virtual methods

  6. Example HSBC bank Account Information: client name Account Number Current Balance Required method: Creating an account Deposit money Withdraw money Display

  7. For HSBCPlus bank account, we want to add the following : An upper limit to the overdraft protection Interest rates charged for loans Overdraft currently owed to bank

  8. Withdraw money operation has to incorporate overdraft protection Display operation has to show additional information for HSBCPlus.

  9. #ifndef HSBC_H_ #define HSBC_H_ class HSBC{ private : enum {MAX=35}; char fullName[MAX]; long accNum; double balance; public : HSBC(const char *s="NULL",long an=-1, double bal =0.0);

  10. void Deposit(double amt); virtual void Withdraw (double amt); double get_balance(); virtual void ViewAcct(); virtual ~HSBC(){ } }; class HSBCPlus : public HSBC { private : double maxLoan; double rate; double owesBank;

  11. public: HSBCPlus(const char *s="NULL", long an=-1, double bal =0.0, double ml=500,double r=0.10); HSBCPlus (const HSBC &hs, double ml=500, double r=0.10); virtual void ViewAcct(); virtual void Withdraw(double amt); void ResetMax(double m) { maxLoan =m;} void ResetRate(double r) { rate =r; } void ResetOwes() { owesBank =0; } }; #endif

  12. An object from HSBC uses the ViewAcc in HSBS class and object from HSBCPlus uses the ViewAcc in HSBCPlus. HSBC dom(“Dave Cohen”,1122,4343.45); HSBCPlus dot(“Gregory Guin”,1238, 2689.00); dom.ViewAcct(); // HSBS dot.ViewAcct(); //HSBCPlus

  13. If we DONT use keyword virtual : HSBC dom(“Dave Cohen”,1122,4343.45); HSBCPlus dot(“Gregory Guin”,1238, 2689.00); HSBC &b1_ref=dom; HSBC &b2_ref=dot; b1_ref.ViewAcct(); // HSBC b2_ref.ViewAcct(); // HSBC

  14. If we use keyword virtual : HSBC dom(“Dave Cohen”,1122,4343.45); HSBCPlus dot(“Gregory Guin”,1238, 2689.00); HSBC &b1_ref=dom; HSBC &b2_ref=dot; b1_ref.ViewAcct(); // HSBC b2_ref.ViewAcct(); // HSBCPlus HSBCPlus &b3_ref=dot; b3_ref.ViewAcct(); //which method is called when there is no virtual?

  15. #include<iostream.h> #include<cstring> #include “hsbc.h” HSBC::HSBC(const char *s,long an, double bal ) { strncpy(fullNmae,s,MAX-1); fullName[MAX-1]=‘\0’; acctNum=an; balance=bal; }

  16. void HSBC::Deposit(double amt) { if( amt<0) cout<<"No negative deposit"; else balance+=amt; } void HSBC::Withdraw(double amt) { if( amt < 0) cout<< "No negative withdraw"; else if( amt <= balance) balance -=amt; else cout<<"Not enough balance"; }

  17. void HSBC::ViewAcct(double amt) { cout<<“client: “ <<fullName<<endl; cout<<“Account Num:” <<accNum<<endl; cout<<“Balance: $“<<balance<<endl; }

  18. HSBCPlus::HSBCPlus(const char *s,long an, double bal, double ml, double r ) : HSBC(s,an,bal) // member initialize list { maxLoan=ml; owesBank=0.0; rate=r; } HSBCPlus::HSBCPlus(const HSBC &hs , double ml, double r ) : HSBC(hs) { maxLoan=ml; owesBank=0.0; rate=r; }

  19. void HSBCPlus::ViewAcct() { HSBC::ViewAcct(); cout<<"Maxloan: $" <<maxLoan<<endl; cout<<"OwesBank: $" <<owesBank<<endl; cout<<"Loan Rate: $"<<100*rate<<"%\n"; }

  20. void HSBCPlus::Withdraw(double amt) { double bal=get_balance(); if( amt <= bal) HSBC::Withdraw(amt); else if (amt <=bal+maxLoan-owesBank) { double advance=amt-bal; owesBank+=advance*(1.0+rate); cout<< "Bank Advance: $ " <<advance<<endl; cout<<"Finace Charge: $ "<<advance*rate<<endl; Deposit(advance); HSBC::Withdraw(amt); } else cout<<"Credit Limit exceeded"; }

  21. #include<iostream.h> #include "hsbc.h" int main() { HSBC coh("Dave Cohen",34523,3000.0); HSBCPlus greg("Greory Gutin",32144,2000.0); coh.ViewAcct(); cout<<endl; greg.ViewAcct(); cout<<endl; greg.Deposit(1000.00); coh.Withdraw(4200.00); greg.Withdraw(3200.00); greg.ViewAcct(); return 0; }

  22. #include<iostream> #include “hsbc.h” const int CLIENT=4; const int LEN=40; int main(){ HSBC *p_clients[CLIENTS]; int i; for( i=0;i<CLIENTS; i++) { char temp[LEN]; long tempnum; double tempbal; char kind; cout<<“enter client name :”; cin.getline(temp,LEN);

  23. cout<<“enter client’s name: “; cin>>tempbal; cout<<“ enter 1 for HSBS, 2 for HSBCPlus”; if ( kind==‘1’) { p_clients[i]=new HSBC(temp, tempnum,tembal); } else { double tmax, trate; cout<<“enter the overdraft limit: $”; cin>>tmax; cout<<“enter enter the interest rate ”; cin>>trate; p_clients[i]=new HSBCPlus(temp,tempnum,tempbal,tmax,trate); }

  24. while (cin.get() !=‘\n’) continue; } cout<<endl; for( i=0; i<CLIENTS;i++) { p_clients[i]->ViewAcct(); cout<<endl; } for(i=0; i<CLIENTS;i++) delete p_clients[i]; } reurn 0; }

  25. we use polymorphic in the this loop: for( i=0; i<CLIENTS;i++) { p_clients[i]->ViewAcct(); cout<<endl; }

More Related