Download presentation
Presentation is loading. Please wait.
Published bySilas Butler Modified over 9 years ago
2
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting and downcasting Virtual member functions Early binding and late binding Abstract base classes Pure virtual function Public inheritance
3
Inheriting classes Base class Derived class Program tabtenn0.h, tabtenn0.cpp, usett0.cpp
4
Inheritance
5
Derived class class RatedPlayer : public TableTennisPlayer { private: int rating; public: … };
6
Base and derived classes
7
Access specify without inheritance
8
Initializer list RatedPlayer::RatedPlayer(unsigned int r, const char *fn, const char*ln, bool ht): TableTennisPlayer(fn, ln, ht) { rating = r; } … RatedPlayer rplayer1(1140, “Mallory”, “Duck”, true);
9
Initializer list RatedPlayer::RatedPlayer(unsigned int r, const char *fn, const char*ln, bool ht): TableTennisPlayer(fn, ln, ht), rating(r) {} … RatedPlayer rplayer1(1140, “Mallory”, “Duck”, true);
10
Constructor for derived class The base-class object is constructed first The derived-class constructor should pass base-class information to a base-class constructor via a member initializer list The derived-class constructor shoud initialize the data members that were added to the derived class
11
Using a derived class Program tabtenn1.h, tabtenn1.cpp, usett1.cpp
12
Access specify with inheritance
13
Special relationship between base and derived class Derived class uses base class methods Upward casting is allowed Downward casting is not safe
14
Implicit upward casting void Show(const TableTennisPlayer &rt) { cout << “Name: ”; rt.Name(); cout << “\nTable: ”; if (rt.HasTable()) cout << “yes\n”; else cout << “no\n”; } TableTennisPlayer player1(“Tara”, “Boomdea”, false); RatedPlayer rplayer1(1140, “Mallory”, “Duck”, true); Show(player1); Show(rplayer1);
15
Implicit copy constructor RatedPlayer olaf1(1190, “Olaf”, “Loaf”, true); TableTennisPlayer olaf2(olaf1), olaf3; // invoke implicit copy constructor and upward casting // TableTennisPlayer(const TableTennisPlayer&); TableTennisPlayer winer = olaf1; TableTennisPlayer winer = TableTennisPlayer(olaf1); olaf3 = olaf1;//assignment op and upward casting
16
Is-a & has-a relationships
17
Inheritance model Public –An is-a relationship –Illegal relationships Is-like-a, has-a, is-implemented-as, uses-a Private Protected
18
Polymorphic public inheritance Redefining base-class methods in a derived class Using virtual methods Program brass.h, brass.cpp, usebrass1.cpp Virtual method behavior and the need for virtual destructors in base class Program usebrass2.cpp
19
Bindings Static binding (early binding) during compiling Dynamic binding (late binding) during execution
20
Pointer and reference type compatibility double x = 2.5; int *pi = &x;// invalid, mismatch pointer type long & r = x;// invalid, mismatch reference type BrassPlus dilly(“Annie Dill”, 34854, 3000); Brass *pd = &dilly;// ok Brass &rd = dilly;// ok
21
Upcasting & downcasting Upcasting –Converting a derived-class reference or pointer to a base-class reference or pointer –Implicit type cast is allowed –Is–a relationship Downcasting –Converting a base-class pointer or reference to a derived-class pointer or reference –Only allow explicit type cast
22
Upcasting downcasting
23
Virtual member functions and dynamic binding Virtual function uses dynamic binding, whereas, non-virtual function employs static binding
24
Virtual or non-virtual? Virtual function –Base class member function is allowed to be redefined in derived class Non-virtual function –Base class member function is not allowed to be redefined in derived class
25
Why two kinds of binding? Why static is the default? Two reasons –Efficiency – static binding is a little efficient than dynamic binding –The conceptual model – if you don’t want to redefine member functions in derived classes
26
How virtual function work? Virtual function table (vtbl) Scientist sophie(“Sophie Fant”);
27
Costs of using virtual function Each object has its size increased by the amount needed to hold an address For each class, the compiler creates a table (an array) of addresses of virtural functions For each function call, there’s an extra step of going to a table to look up an address
28
Rules of virtual methods Begin a class method declaration with the keyword virtual in a base class Constructor cannot be virtual Destructor should be virtual unless a class isn’t to be used as a base class Friends cannot be virtual functions because friends are not class members Statics cannot be virtual If a derived class fails to redefine a function, the class will use the base version of the function If base class declaration is overloaded, one needs to redefine all the base class version in derived class
29
Return type is allowed to vary
30
Redefinition hides methods
31
Hidden methods
32
Inheritance is-a relationship Sometimes applying the is-a rule is not as simple as it might appear For example, A circle is a special case of an ellipse. Problems arise when one is tempting to derive a circle class from an ellipse class.
33
Abstract base classes (ABC) Solution: One can abstract from the Ellipse and Circle classes what they have in common and place those features in an ABC Program acctabc.h, acctabc.cpp, usebrass3.cpp
34
Inheritance & dynamic memory allocation How does inheritance interact with dynamic memory allocation? Derived class doesn’t use new Derived class does use new
35
Derived class without DMA It is not necessary to self-defined constructors, copy constructor, destructor, assignment by using new and delete
36
Derived class with DMA It needs to self-defined constructors, copy constructor, destructor, assignment by using new and delete
37
An inheritance example with DMA and Friends Program dma.h, dma.cpp, usedma.cpp
38
Class design Member functions that the compiler generates for you –Default constructors –Copy constructors –Assignment operators –Destructors Class method considerations
39
Constructor –Constructors aren’t inherited) Destructor –Should provide virtual destructor in base class Conversion constructors
40
Passing by value or reference Passing an object by value or passing by reference –Passing by value involves a temporary copy constructor and then destructor –Efficiency sake –Inheritance using virtual function to accept a base-class reference argument
41
Returning an object or returning a reference
42
Public inheritance considerations Is-a relationship consideration Constructors are not inherited Down casting is not safe Assignment operator // Implicit upcast // Explicit downcast // Save upcast // Unsafe downcast Or use conversion constructor
43
Public/private/protected members private protected public Class D: protected A D ObjD private protected public Class DD: public D DD ObjDD private protected public Class CC: public C CC ObjCC
44
Virtual method considerations Inappropriate code In the Inadequate() function Brass version ViewAcct() was called Brace object constructed by Brass copy constructor Automatic upcasting allows the constructor argument to refer to a BrassPlus object
45
Other methods considerations A base class destructor should be virtual Friend function is not actually a class member, it is not inherited
46
The base class methods in public inheritance
47
Member functions in class
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.