relation between derived-class and base-class derived class object can use base class methods, if...

Post on 21-Dec-2015

254 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

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.

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!

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.

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

Example HSBC bank Account

Information:client name Account NumberCurrent Balance

Required method:Creating an accountDeposit moneyWithdraw moneyDisplay

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

Withdraw money operation has to incorporate overdraft protection

Display operation has to show additional information for HSBCPlus.

#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);

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;

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

• 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

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

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(); // HSBCb2_ref.ViewAcct(); // HSBCPlus

HSBCPlus &b3_ref=dot; b3_ref.ViewAcct(); //which method is called when there is no virtual?

#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;}

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"; }

void HSBC::ViewAcct(double amt)

{

cout<<“client: “ <<fullName<<endl;

cout<<“Account Num:” <<accNum<<endl;

cout<<“Balance: $“<<balance<<endl;

}

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; }

void HSBCPlus::ViewAcct()

{

HSBC::ViewAcct();

cout<<"Maxloan: $" <<maxLoan<<endl;

cout<<"OwesBank: $" <<owesBank<<endl;

cout<<"Loan Rate: $"<<100*rate<<"%\n";

}

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";}

#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;}

#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);

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);}

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;}

we use polymorphic in the this loop:

for( i=0; i<CLIENTS;i++)

{

p_clients[i]->ViewAcct();

cout<<endl;

}

top related