C++ Basics Tutorial - Lesson 9
Related Blog Items
- C++ Basics - Tutorial
- C++ Tutorial Part 2 - Advanced
- C++ Advanced Tutorial - Lesson 11
- C++ Advanced Tutorial - Lesson 8
- C++ Advanced Tutorial - Lesson 3
Please refer lesson 8..
9. Polymorphism
9.1 Virtual Methods
9.2 Polymorphism
9.3 Dynamic and Static Binding
9.4 Abstract Base Class (ABC)
9.5 Virtual Destructor
9.6 Hierarchy for Interface and Implementation
9.7 Base Class Idiom
9.8 Apply Polymorphism on Operator << and >>
9. Polymorphism
9.1 Virtual Methods
When a method is declared virtual, it remains “virtual” all the way down the inheritance hierarchy even if the overridden method in the derived class is not declared virtual. However, it is a good practice to explicitly declare all the overriding methods down in the hierarchy to be virtual to promote program clarity.
If a derived class doesn’t provide an overriding method, it will simply inherits its base class’s virtual method.
9.2 Polymorphism
When different classes inherits from the same base class, we can use a base-class pointer to point to any derived-class objects, and access their virtual methods. Objects of different classes related by inheritance from the same base class can response differently to the same method call.
9.3 Dynamic and Static Binding
=>Dynamic Binding
When a pointer is used to refer to a derived-class virtual method:
basePtr1 = &DeriveObj1; basePtr1->print();
the correct method is chosen at run time dynamically. This is called “dynamic binding”.
Because the correct method is not chosen at compile time, a program can be written to receive and make use of an object of a class which has not be developed yet. It can simply put a base-class pointer in the parameter list, and perform all the operations through this pointer.
=>Static Binding
If the object name is used to call its method:
DerivedObj1.print();
the correct method is chosen at compile time. This is called “static binding”.
9.4 Abstract Base Class (ABC)
We declare the base-class virtual method “pure” by putting “= 0″ at the end of the method prototype. We needn’t but we are allowed to provide method definition for pure methods.
A base class with a pure method is called abstract base class (ABC). It is designed purely to be inherited, and is not allowed to have any instance. The only way to make a class abstract is to have a pure method. In Java, you can simply put abstract in front of a class header to make it abstract.
If a class is derived from an abstract class and does not provide an overriding method for the pure virtual method, it will inherit the pure virtual method and thus become an ABC too.
A hierarchy does not need to contain any abstract classes, but many good OO systems have class hierarchies headed by one or even several levels of abstract classes. To prevent partial assignment, it is a good practice to always inherit from ABC.
Pure virtual methods do not need to have any implementation, but it can be implemented although it is very misleading. When all the methods of the base class need to do something it is always better to put as much as common behaviors of different derived classes into the base class but still we want this class to be ABC, we make the destructor pure virtual.
9.5 Virtual Destructor
Although a constructor can not be virtual, a destructor can be, so that you can delete a derived-class object though a base-class pointer. Otherwise when deleting this object only the base-class destructor will be called.
9.6 Hierarchy for Interface and Implementation
Hierarchies designed for implementation tend to have their methodality high in the hierarchy. Derived classes inherit these implementations.
Hierarchies designed for interface tend to have their methodality lower in the hierarchy. The base class only provide an interface (e.g. with pure virtual methods), and the derived class provide their own implementations. Like Java s interfaces.
When a base class is purely designed for providing an interface, it may only contain pure virtual methods and no data members.
9.7 Base Class Idiom
Virtual destructor and protected assignment operator is called the base class idiom.
9.8 Apply Polymorphism on Operator << and >>
As discussed before, stream insertion and extraction operators ( >> and << ) can not be member methods. But we can still make them applicable to polymorphism:
ostream & operator<<(ostream & os, const Base & obj) { return obj.output(os); }
Then we write a virtual output method for all derived classes:
virtual ostream & output(ostream & os)
Popularity: 15%
You need to log on to convert this article into PDF
Related Blog Items - C++ Basics - Tutorial
- C++ Tutorial Part 2 - Advanced
- C++ Advanced Tutorial - Lesson 11
- C++ Advanced Tutorial - Lesson 8
- C++ Advanced Tutorial - Lesson 3
Related Blog Items
- C++ Basics - Tutorial
- C++ Tutorial Part 2 - Advanced
- C++ Advanced Tutorial - Lesson 11
- C++ Advanced Tutorial - Lesson 8
- C++ Advanced Tutorial - Lesson 3
No Comments
No comments yet.