Date post: | 26-Dec-2015 |
Category: |
Documents |
Upload: | verity-mcbride |
View: | 217 times |
Download: | 0 times |
CS-1030Dr. Mark L. Hornick
2
Inheritance Syntax class Pet {
protected: string name; public: int speak(); int eat(); }
class Dog : public Pet {// Java uses extendsprivate: bool hasFleas;public: int speak(); // override}
class Cat : public Pet {private: bool isDeclawed;public: int purr();
} Example Program…
CS-1030Dr. Mark L. Hornick
3
C++ Inheritance rules
Protected and public data and methods in the base class are inherited by the derived classWith some exceptions:
Constructors Destructors operator=
Are accessible in the derived class.
CS-1030Dr. Mark L. Hornick
4
Public/protected/private rulesclass Dog : <inheritance specifier> Pet {
private: bool hasFleas;public: int speak();}
<Inheritance specifier> is one of the following public
All public and protected members of Pet will be considered public or protected within Dog Still inherits Pet’s private members, but they’re not accessible from Dog methods Also inherits Pet’s protected members, and they’re accessible from Dog methods
protected All public and protected members of Pet will be considered protected within Dog What was public in Pet is protected within Dog Still inherits Pet’s private members, but they’re not accessible from Dog methods
private All public and protected members of Pet will be considered private within Dog What was public or protected in Pet is private within Dog Still inherits Pet’s private members, but they’re not accessible from Dog methods
CS-1030Dr. Mark L. Hornick
5
C++ Construction Order
1. Base class data members are constructed in order of declaration
2. Base class constructor is called
3. Derived class data members are constructed in order
4. Derived class constructor is executed This is much different from Java, where super()
has to be invoked from the derived class constructor to call the base class constructor
Example Program…
CS-1030Dr. Mark L. Hornick
6
Destruction order
Destructor order (reverse of ctor order)1. Derived class destructor is executed
2. Derived class data members are destroyed
3. The base class destructor is called
4. Base class data members are destroyed
CS-1030Dr. Mark L. Hornick
7
Method overrides class Pet {
protected: string name; public: int eat(); }
class Dog : public Pet {private: bool hasFleas;public: int eat(); // override replaces the inherited version
int speak();}
class Cat : public Pet {private: bool isDeclawed;public: int purr();
} Example Program…
CS-1030Dr. Mark L. Hornick
8
Functional binding
void main() {
Dog spot; spot.speak();
Pet* pet = &spot; pet->speak();
Pet aPet; apet.speak();}
Dog::speak()
Pet::speak()
Pet::speak()
CS-1030Dr. Mark L. Hornick
9
Functional Binding
Variant behavior Derived object – calls derived version Base pointer – calls base version
This is inconsistent and not like Java This is called Early Binding
Functional version is chosen at compile time
CS-1030Dr. Mark L. Hornick
10
Polymorphism
The solution to variant behavior Version is chosen at run time Delay the choice until we know the real type
Even when upcast!
CS-1030Dr. Mark L. Hornick
11
virtual Functions
Declaration in the base class Forces use of late binding for a function Keyword: “virtual”
Implementation Compiler inserts code to ask the object before
calling a virtual function
CS-1030Dr. Mark L. Hornick
12
virtual Example
class Pet {public:
virtual void speak(); …};
class Dog : public Pet {public:
void speak(); …}; virtual is optional here
CS-1030Dr. Mark L. Hornick
13
Calling virtual Functions
void main() {
Dog spot; spot.speak(); //virtual
Pet* pet = &spot; pet->speak();
Pet aPet; apet.speak();}
Dog::speak()
Dog::speak()
Pet::speak()
CS-1030Dr. Mark L. Hornick
14
General Overriding Rules
If you want variant behavior … Don’t declare base class functions virtual Don’t override in derived classes
invariant over specialization Create virtual base class function(s) Overrides expected, but not required in derived
classes
CS-1030Dr. Mark L. Hornick
15
Abstract Base Classes
Remember Java Abstract classes and Interfaces?
Describes a set form/template Never intend to create an object
Must have classes that implement the interface or abstract methods
Compiler will not allow abstact classes/interfaces to be created References are OK (In C++, Pointers are OK too)
CS-1030Dr. Mark L. Hornick
16
The C++ equivalent of Java abstract classes and interfaces
Done in C++ by declaring pure virtual methods
Language syntaxvirtual void speak()=0; // “0=pure virtual”
Not all methods have to be declared pure virtual But having even one pure virtual method makes
the entire class abstract
CS-1030Dr. Mark L. Hornick
17
Upcasting
Inheritance embodies “is a” A derived object is a base object
Derived objects are base objects Dog spot; Pet* pPet = &spot;
Always use pointers or references to do this Otherwise the extra is “sliced” off:
Pet aPet = spot; // ouch!Pet.speak(); // calls Pet::speak()
CS-1030Dr. Mark L. Hornick
18
Object Slicing
Upcasting must be done carefully Always use references or pointers So you have the original object
Base copy constructors and operator= Cannot fully copy a derived object Slices off the “new” and overridden stuff
CS-1030Dr. Mark L. Hornick
19
virtual Destructors
Allows upcast objects to properly destroy Forces use of the most derived destructor All the destructors are still called
Most derived base (in order)
ALWAYS make base class destructors virtual Good habit
Pure virtual destructors must still be defined
CS-1030Dr. Mark L. Hornick
20
Polymorphic methods
Polymorphism is multiple versions of an object that: Change their behavior based on context Adapt to the needs of the situation
Primary example – upcast objects accessed via base class pointers
Secondary example Operators: + addition, concatenation (later) Functions: Multiple constructors