+ All Categories
Home > Documents > Relation Between Derived-Class and Base-Class Derived class object can use base class methods, if...

Relation Between Derived-Class and Base-Class Derived class object can use base class methods, if...

Date post: 21-Dec-2015
Category:
View: 249 times
Download: 0 times
Share this document with a friend
Popular Tags:
25
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.
Transcript
Page 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.

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.

Page 2: 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.

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.

Page 3: 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.

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!

Page 4: 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.

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.

Page 5: 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.

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

Page 6: 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.

Example HSBC bank Account

Information:client name Account NumberCurrent Balance

Required method:Creating an accountDeposit moneyWithdraw moneyDisplay

Page 7: 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.

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

Page 8: 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.

Withdraw money operation has to incorporate overdraft protection

Display operation has to show additional information for HSBCPlus.

Page 9: 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.

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

Page 10: 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.

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;

Page 11: 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.

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

Page 12: 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.

• 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

Page 13: 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.

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

Page 14: 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.

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?

Page 15: 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.

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

Page 16: 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.

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

Page 17: 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.

void HSBC::ViewAcct(double amt)

{

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

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

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

}

Page 18: 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.

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

Page 19: 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.

void HSBCPlus::ViewAcct()

{

HSBC::ViewAcct();

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

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

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

}

Page 20: 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.

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

Page 21: 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.

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

Page 22: 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.

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

Page 23: 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.

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

Page 24: 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.

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

Page 25: 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.

we use polymorphic in the this loop:

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

{

p_clients[i]->ViewAcct();

cout<<endl;

}


Recommended