Reference: I Horton, Ivor Horton's Beginning Visual C++ 2013 (Wiley 2014).
9.2 Inheritance in Classes
∎ Deriving classes form a base class
Ex9_01.cpp (main function; #include "CandyBox.h")
CandyBox.h (derived class; #include "Box.h"; class CCandyBox : CBox)
Box.h (base class; class CBox)
9.3 Access Control under Inheritance
∎ Accessing private members of the base classclass
CCandyBox : public CBox vs. CBox (or private CBox)
∎ The function members defined in the derived class cannot access the private date members of a base class. To access the private members of the base class, one has to move the function "double volume() const" from the derived class to the base class.
CCandyBox(double lv, double wv, double hv, const char* str = "Candy") : CBox {lv, wv, hv} // 1
explicit CCandyBox(const char* str = "Candy") // 2
∎ The access level of inherited class member
9.4 The Copy Constructor in a Derived Class
∎ The copy constructor in a derived class (Ex9_05)
CCandyBox(const CCandyBox& initCB) vs. CCandyBox(const CCandyBox& initCB) : CBox{initCB} // All copy constructors in both header files have to be called
9.5 Preventing Class Derivation
∎ Modifier "final"
9.6 Class Members as Friends
9.7 Virtual Functions
∎ Using an inherited function: early binding (Ex9_06)
myBox.showVolume(); // Call the volume() function in Box.h
myGlassBox.showVolume(); // Still call the volume() function in Box.h, not the one in GlassBox.h
virtual double volume() const // Box.h
virtual double volume() const // GlassBox.h; don't forget to specify volume() as const, otherwise you won't override the base function
∎ Ensuring correct virtual function operation (Modifier "override")
virtual double volume() const override // The keyword "const" still cannot be omitted, however
∎ Preventing function overriding (Modifier "final)
virtual double volume() const final // If you don't want this volume() function to be overridden
∎ Points to base and derived classes (Ex9_08)
CBox myBox{ 2.0, 3.0, 4.0 }; // Define a base box
CGlassBox myGlassBox{ 2.0, 3.0, 4.0 }; // Define derived box of same size
CBox* pBox{}; // A pointer to base class objects
pBox = &myBox; // Set pointer to address of base object
pBox->showVolume(); // Display volume of base box
pBox = &myGlassBox; // Set pointer to derived class object
pBox->showVolume(); // Display volume of derived box
∎ Using references with virtual functions (Ex9_09)
output(myBox); // Output volume of base class object
output(myGlassBox); // Output volume of derived class object
void output(const CBox& aBox) // Accept an object of any class derived from CBox as an argument
{
aBox.showVolume();
}
∎ An abstract class (Ex9_10)
// Container.h
virtual double volume() const = 0; // No content; pure virtual function in the abstract base class
// Main.cpp
// Pointers to abstract base class, initialized with address of CBox object or CCan object
CContainer* pC1{ new CBox{ 2.0, 3.0, 4.0 } };
CContainer* pC2{ new CCan{ 6.5, 3.0 } };
pC1->showVolume(); // Output the volumes of the two objects ...
pC2->showVolume(); // ... pointed to
delete pC1; // Clean up ...
delete pC2; // ... the free space
∎ More than one level of inheritance (Ex9_11)
// Class CGlassBox is derived from its direct base class CBox and indirect base class CContainer
// Inheritance & Constructor (GlassBox.h)
class CGlassBox : public CBox
CGlassBox(double lv, double wv, double hv) : CBox{ lv, wv, hv } {}
9.8 Casting between Class Types
∎
9.9 Nested Classes
∎
9.10 Summary
∎
沒有留言:
張貼留言