Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an...
Transcript of Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an...
![Page 1: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/1.jpg)
68
Inheritance
A mechanism for specialization A mechanism for reuse
Fundamental to supporting polymorphism
![Page 2: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/2.jpg)
69
BankAccount
class Account{protected: double fBalance;
public: Account( double aBalance );
void deposit( double aAmount ); void withdraw( double aAmount ); double getBalance();};
class BankAccount: public Account{private: double fInterestRate;
public: BackAccount( double aRate );
void addInterest(); void chargeFee( double aAmount );};
![Page 3: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/3.jpg)
70
Constructors and Inheritance
Whenever an object of a derived class is instantiated,multiple constructors are called so that each class inthe inheritance chain can initialize itself.
The constructor for each class in the inheritancechain is called beginning with the base class at thetop of the inheritance chain and ending with the mostrecent derived class.
![Page 4: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/4.jpg)
71
Base Class Initializer
class BankAccount : public Accout{public: BackAccount( double aRate ) : Account( 0.0 ),
fInterestRate( aRate ) {}};
![Page 5: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/5.jpg)
72
Facts About Base Class Initializer
If a base class does not have a default constructor, thederived class must provide a base class initializer for it.
Base class initializers frequently appear alongside memberinitializers, which use similar syntax.
If more than one argument is required by a base classconstructor, the arguments are separated by comma.
Reference members need to be initialized using a memberinitializer.
![Page 6: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/6.jpg)
73
Destructors and Inheritance
Whenever an object of a derived class is destroyed,the destructor for each class in the inheritancechain, if defined, is called.
The destructor for each class in the inheritance chainis called beginning with the most recent derived classand ending with the base class at the top of theinheritance chain.
![Page 7: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/7.jpg)
74
Virtual Destructors
When deleting an object using a base class pointerof reference, it is essential that the destructors foreach of the classes in the inheritance chain get achance to run:
BackAccount *BAptr;Account *Aptr;
BAptr = new BackAccount(2.25);Aptr = BAptr;…delete Aptr;
![Page 8: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/8.jpg)
75
Virtual Account Destructor
class Account{public: virtual ~Account() { … } …};
Not declaring a destructorvirtual is a common source
for memory leaks!
![Page 9: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/9.jpg)
76
Virtual Member Functions
To give a member function from a base class newbehavior in a derived class, one overrides it.
To allow a member function in a base class to beoverridden, one must declare the member functionvirtual.
Note, in Java all member functions are virtual.
![Page 10: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/10.jpg)
77
Virtual withdraw Method
class Account{public:… virtual void withdraw( double aAmount ) { fBalance -= aAmount; }…};
![Page 11: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/11.jpg)
78
Overriding the withdraw Method
class BackAccount : public Account{public:… virtual void withdraw( double aAmount ) { if ( !(fBalance - aAmount < 0.0) ) Account::withdraw( aAmount ); }…};
never use ==
base call
![Page 12: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/12.jpg)
79
Calling a Virtual Method
BankAccount lBankAccount(2.25);Account* Aptr = &lBackAccount;
Aptr->withdraw( 50.0 );
invokes BankAccount::withdraw
![Page 13: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/13.jpg)
80
Facts About Virtual Members Constructors cannot be virtual. Declaring a member function virtual does not require that
this function must be overridden in derived classes, exceptthe member function is declared pure virtual.
Once a member function has been declared virtual, it remainsvirtual.
Parameter and result types must match to properly overridea virtual member function.
If one declares a non-virtual member function virtual in aderived class, the new member function hides the inheritedmember function.
You can declare a private member function virtual to enablepolymorphism within the scope of the declaring class.
![Page 14: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/14.jpg)
81
Abstract Base Classes
A class is abstract if it contains one or more purevirtual member functions. An abstract class cannot be instantiated. Derived classes must provide definitions for the pure virtual
member functions, or declare them as pure virtual itself. A pure virtual member function can be defined in the class
that declares it pure virtual. If one declares a pure virtual destructor, a definition for it
must be given in the class that declares the destructorpure virtual.
Abstract classes required a virtual destructor.
![Page 15: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/15.jpg)
82
Pure Virtual Member Function
class Account{public: virtual double estimateReturn() = 0;};
Now Account is abstract!
![Page 16: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/16.jpg)
83
Abstract Classes as Interfaces
An interface is a set of member functions. Interfaces never define data types. Interfaces never provide a default implementation
of methods. A class implements an interface by defining all
member functions declared by the interface.
An abstract class can be viewed as an interface ifit only contains public pure virtual memberfunctions.
![Page 17: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/17.jpg)
84
Interface TicTacToeView
class TicTacToeView{public: virtual ~TicTacToeView() {}; // empty required destructor
virtual void registerGame( TicTacToe* aGame ) = 0; virtual void printBoard() = 0; virtual void draw() = 0; virtual void declareWinner( int aPlayer ) = 0;};
![Page 18: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/18.jpg)
85
Access Level for Inheritance
public: Public members in the base class remain public. Protected members in the base class remain protected. Yields a “is a” relationship.
protected: Public and protected members in the base class are
protected in the derived class. Yields a “implemented in terms of” relationship.
private: Public and protected members in the base class become
private in the derived class. Yields a stricter “implemented in terms of” relationship.
![Page 19: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/19.jpg)
86
Multiple Inheritance
C++ allows a class to inherit fromseveral base classes at once.
Multiple inheritance is a mechanism tocompose orthogonal behavior.
![Page 20: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/20.jpg)
87
CheckingAccount
class Account { … };
class BackAccount : public Account { … };
class WireAccount : public Account { … };
class CheckingAccount : public BackAccount, public WireAccount { … };
![Page 21: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/21.jpg)
88
Diamond Problem
class Account
class BankAccount class WireAccount
class CheckingAccount
CheckingAccount inherits Account twice!
fBalancefBalance
BankAccount::fBalance WireAccount::fBalance
![Page 22: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/22.jpg)
89
Virtual Base Classes
A virtual base class allows multipleinstances of a base to be included inobject of a derived class.
Members of virtual base classes aredisambiguated by merging them intoone representative.
![Page 23: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/23.jpg)
90
Virtual Account Class
class Account { … };
class BackAccount : virtual public Account { … };
class WireAccount : virtual public Account { … };
class CheckingAccount : public BackAccount, public WireAccount { … };
![Page 24: Inheritance - cs.iastate.edulumpe/CS229/Inheritance.pdf70 Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that](https://reader036.fdocuments.in/reader036/viewer/2022081401/5ad04ede7f8b9a4e7a8dc3df/html5/thumbnails/24.jpg)
91
How Virtual Base Classes Work
class Account
class BankAccount class WireAccount
class CheckingAccount
fBalancefBalance
(Account::)fBalance (Account::)fBalance
fBalance