More%20about%20Inheritance - PowerPoint PPT Presentation

About This Presentation
Title:

More%20about%20Inheritance

Description:

class HSBC: public AcctABC. public : ... HSBCPlus(const HSBC & hs, double ml=500, double r=0.10); virtual void Withdraw(double amt) ... – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 31
Provided by: Ara8
Category:

less

Transcript and Presenter's Notes

Title: More%20about%20Inheritance


1
More about Inheritance
2
Static and Dynamic Binding
  • Compiler finds which part of the code should be
    executed when a function calls.
  • Interpreting a function invoking is called
    binding .
  • This task is simple in C , each name is for one
    function.
  • In C binding is a complex task because of
    function overloading.

3
  • In some cases binding can be done in compilation
    time, static binding .
  • For virtual function we cant decide which
    function should be executed.
  • This task should be done in running time, dynamic
    binding ( late binding) .
  • Dynamic binding in C associated with methods
    invoked by pointers and references.

4
  • HSBCplus op
  • HSBC bp
  • bpop
  • bp-gtViewAcct()
  • The compiler uses static binding for nonvirtual
    function.
  • The compiler uses dynamic binding for virtual
    function.

5
Things to Know About Virtual
  • Beginning a class method declaration with
  • virtual in the base class makes the function
  • for the base class and all classes derived from
    the base class virtual.
  • If a virtual method is invoked by reference or
    pointer to an object, the program uses the method
    defined for the object type rather than the
    method defined for the reference or pointer type.
    (dynamic binding)
  • If youre defining a class that will be used as a
    base class for inheritance, you should declare as
    virtual functions the class methods that may have
    to redefined in the derived classes.

6
  • Constructors cant be virtual. Derived class
    doesnt inherit the base class constructors.
  • Destructors should be virtual unless a class is
    not a base class.
  • Example
  • employee pe new singer
  • delete pe // employee() or singer() ?
  • If we dont use virtual, delete invokes the
    employee() destructor. Frees only memory pointed
    by employee components of the singer object.
  • If destructor is virtual first singer() is
    invoked and then employee()

7
  • Friends can not be virtual functions, because
    they are not member function.
  • If we have a chain of derivation, the most resent
    version of the function is used.
  • (function hiding )

8
  • class Base
  • public
  • virtual void show(int a)
  • .
  • class Derived public Base
  • public
  • virtual void show()
  • Derived temp
  • temp.show() // valid
  • temp.show(5) // invalid.

9
  • The new definition hides the base class version .
    Redefining inherited methods is not a variation
    of overloading.
  • If we redefine a function in a derived class
  • It doesnt just override the base class
    declaration
  • with the same function signature.
  • It hides all base-class methods of the same name.

10
  • If you redefined an inherited method you need
    to make sure you match the original prototype.
  • Exception
  • Return type that is a reference or pointer to a
    base class can be replaced by reference
  • or pointer to the derived class.

11
  • class Base
  • public
  • virtual Base build(int n)
  • .
  • class Derived public Base
  • public
  • virtual Derived build(int n)

12
  • We need to redefine all the base class versions
    in the derived class.
  • class Base
  • public
  • virtual void show(int a)
  • virtual void show(double x)
  • virtual void show( )
  • .
  • class Derived public Base
  • public
  • virtual void show(int a)
  • virtual void show(double x)
  • virtual void show()

13
Access Control protected
  • Protected access Like private access the
    outside class can have access to them through the
    public member.
  • Member of the derived class can access protected
    member of the base class directly, but no direct
    access to the private member.

14
  • class HSBC
  • protected
  • double balance
  • void HSBCplus withdraw (double amt)
  • if( amt lt balance)
  • HSBCWithdraw()
  • else if (amt ltbalancemaxLoan-owesBank)
  • double advanceamt-balance
  • owesBankadvance(1.0rate)
  • coutltlt Bank Advance ltltadvanceltltendl
  • coutltltFinace Charge ltltadvancerateltltendl
  • Deposit(advance)
  • HSBCWithdraw(amt)
  • else
  • coutltlt Credit Limit exceeded

15
Abstract Base Classes
  • Consider ellipse and circle in a graphic
    program
  • Suppose we have the following fields for ellipse
    class
  • class Ellipse
  • private
  • double x
  • double y
  • double a, b // short axis and long axis
  • double angle // orientation angle in degree
  • public
  • virtual void Move(int x, int y) xnx yny
  • virtual double Area() return 3.1459ab

16
  • virtual void Rotate( double nang)
    anglenang
  • virtual void Scale( double sa, double sb)
    asa bsb
  • ..
  • Since circle is an ellipse we can derived circle
    class from base class.
  • But we should be careful since ab, and rotate
    doesnt make sense for circle. Angle is not
    needed for circle.

17
  • Better to write a separate class for circle
  • class Circle
  • private
  • double x
  • double y
  • double r // radius
  • public
  • void Move(int x, int y) xnx yny
  • double Area() return 3.1459rr
  • void Scale( double sr) rsr

18
Another Solution
  • Abstract the common features from Ellipse and
    Circle class and place them in an ABC.
  • Derived both ellipse and circle classes from the
    ABC.

19
  • class BaseEllipse
  • private
  • double x,y
  • public
  • BaseEllipse ( double x00 double y00)
    x(x0) , y(y0)
  • virtual BaseEllipse()
  • void Move ( int nx, int ny) xnx yny
  • virtual double Area() 0 // make it pure
    virtual
  • .

20
  • 0 indicates that the class is an ABC and class
    doesnt have to define a function.
  • But we still can define the function in cpp file
  • An ABC describes an interface that uses at least
    one pure virtual function.
  • Example

21
  • ifndef ACCTABC_H_
  • define ACCTABC_H_
  • class AcctABC
  • private
  • enum MAX 35
  • char fullNameMAX
  • long acctNum
  • double balance
  • protected
  • const char FullName() return fullName
  • long AcctNum() return acctNum

22
  • public
  • AcctABC( const char s"Nullbody", long an-1,
    double bal 0.0)
  • void Deposit(double amt)
  • virtual void Withdraw(double amt) 0
  • double Balance () return balance
  • virtual void ViewAcct() 0
  • virtual AcctABC()

23
  • class HSBC public AcctABC
  • public
  • HSBC(const char s"Nullbody", long an-1,
    double bal 0.0) AcctABC(s,an,bal)
  • virtual void Withdraw(double amt)
  • virtual void ViewAcct()
  • virtual HSBC()

24
  • class HSBCPlus public AcctABC
  • private
  • double maxLoan
  • double rate
  • double owesBank
  • public
  • HSBCPlus(const char s"Nullbody", long
    an-1, double bal 0.0, double ml500, double
    r0.10)
  • HSBCPlus(const HSBC hs, double ml500, double
    r0.10)
  • virtual void Withdraw(double amt)
  • virtual void ViewAcct()
  • void ResetMax(double m) maxLoan m
  • void ResetRate(double r) rate r
  • void ResetOwes() owesBank 0
  • endif

25
  • includeltiostream.hgt
  • includeltcstringgt
  • include "acctabc.h"
  • AcctABC AcctABC (const char s, long an,
    double bal)
  • strncpy(fullName,s,MAX-1)
  • fullNameMAX-1'\0'
  • acctNuman
  • balancebal

26
  • void AcctABCDeposit(double amt)
  • if( amtlt0)
  • coutltlt"No negative deposit"
  • else
  • balanceamt
  • void AcctABCWithdraw(double amt)
  • balance-amt

27
  • void HSBCWithdraw(double amt)
  • if( amt lt 0)
  • coutltlt "No negative withdraw"
  • else if( amt lt Balance())
  • AcctABC Withdraw(amt)
  • else
  • coutltlt"Not enough balance"
  • void HSBCViewAcct()
  • coutltlt"client " ltltFullName()ltltendl
  • coutltlt"Account Num" ltltAcctNum()ltltendl
  • coutltlt"Balance "ltltBalance()ltltendl

28
  • HSBCPlusHSBCPlus(const char s,long an, double
    bal, double ml, double r ) AcctABC(s,an,bal)
  • // member initialize list
  • maxLoanml
  • owesBank0.0
  • rater
  • HSBCPlusHSBCPlus(const HSBC hs , double ml,
    double r ) AcctABC(hs)
  • maxLoanml
  • owesBank0.0
  • rater

29
  • void HSBCPlus ViewAcct()
  • coutltlt"HSBCPlus client " ltltFullName()ltltendl
  • coutltlt"Acct Number" ltltAcctNum()ltltendl
  • coutltlt"Balance "ltltBalance()ltltendl
  • coutltlt"maxloan " ltltmaxLoanltltendl
  • coutltlt"OwesBank " ltltowesBankltltendl
  • coutltlt"Loan Rate "ltlt100rateltlt"\n"

30
  • void HSBCPlusWithdraw(double amt)
  • double balBalance()
  • if( amt lt bal)
  • AcctABCWithdraw(amt)
  • else if (amt ltbalmaxLoan-owesBank)
  • double advanceamt-bal
  • owesBankadvance(1.0rate)
  • coutltlt "Bank Advance "ltltadvanceltltendl
  • coutltlt"Finace Charge "ltltadvancerateltltendl
  • Deposit(advance)
  • AcctABCWithdraw(amt)
  • else
  • coutltlt"Credit Limit exceeded"
Write a Comment
User Comments (0)
About PowerShow.com