C++
Chapter 10 - Polymorphism
and Virtual Functions
The goal in this chapter is to create a "Late Binding" for the behavior of a list of objects. The term "Early Binding" refers to compile time. A non-polymorphic program has all of its containers, like lists and arrays, populated with a single data type. The behavior derived from each container is defined at compile time. However, in a polymorphic system, we do not know what data types will be in the container data structure. Therefore, the behavior of the list or array is determined at runtime or "Late Binding".
There are three required elements to creating a polymorphic system.
For Example: In the Gencad system, we do not know what types of objects the user will select for the parts list. To display the parts list we simply use a "for" loop on the list executing the show function for each type of object. The base class, Baseobj, has the functional nature of the objects identified as virtual and has a virtual destructor. Each derived class implements or over-rides the virtual function of the base class with it's own functional behavior. If a derived class does not implement the virtual functions defined in the base class then the virtual functions of the base class will be used.
Specifying Polymorphism Virtual Functions
Syntax: Use inheritance to create a base class, derived class hierarchy.
Identify the functional behavior which will be virtual for all objects in the polymorphic
list and place "virtual" in front of each function prototype. Each class
must have a virtual destructor to properly cleanup the polymorphic objects.
If you want to keep an instance of the base class from being created. The base class can be made an abstract base class by creating a pure virtual function.
class Abstract
{
virtual void Show ( void ) = 0;
};
Here every derived class must implement it's own show function and since the body of
the show function is 0 or null then no instance can be made from this data type.
In Class Work Shop
Use the following code to create a working example of polymorphism. Perform the
following
modifications. Click here, polym.cpp to access the file or
copy it from the page.
Save the code to polym.cpp
compile: gcc -Wall -o polym polym.cpp -lgpp
execute: polym.exe
1. Observe the order of construction and destruction
2. Create a new animal called a Dog, derived from base class Animal
and implement the virtual functions.
3. Trying creating an instance of just an Animal, What happened?
Note the use of the constructors and passing parameters into the base class.
Example Output
Overloaded Constructor Base Class Animal Overloaded Constructor Elephant Overloaded Constructor Base Class Animal Overloaded Constructor Canary Overloaded Constructor Base Class Animal Overloaded Constructor Elephant Overloaded Constructor Base Class Animal Overloaded Constructor Canary Overloaded Constructor Base Class Animal Overloaded Constructor Elephant Overloaded Constructor Base Class Animal Overloaded Constructor Canary Overloaded Constructor Base Class Animal Overloaded Constructor Elephant Overloaded Constructor Base Class Animal Overloaded Constructor Canary Overloaded Constructor Base Class Animal Overloaded Constructor Elephant Overloaded Constructor Base Class Animal Overloaded Constructor Canary Object = Elephant Height = 10 Weight = 100 State = Big, Slow and Hungry Object = Canary Height = 0.2 Weight = 0.8 State = Teeny, Tiny and PEEP! Object = Elephant Height = 30 Weight = 300 State = Big, Slow and Hungry Object = Canary Height = 0.4 Weight = 1.6 State = Teeny, Tiny and PEEP! Object = Elephant Height = 50 Weight = 500 State = Big, Slow and Hungry Object = Canary Height = 0.6 Weight = 2.4 State = Teeny, Tiny and PEEP! Object = Elephant Height = 70 Weight = 700 State = Big, Slow and Hungry Object = Canary Height = 0.8 Weight = 3.2 State = Teeny, Tiny and PEEP! Object = Elephant Height = 90 Weight = 900 State = Big, Slow and Hungry Object = Canary Height = 1 Weight = 4 State = Teeny, Tiny and PEEP!
04/12/00 03:51 PM