Two versions of Actor 5 demonstrate polymorphism. The first version begins with the Actor 3, and makes the display() function virtual to demonstrate basic polymorphism. The second version is fleshed out, suggesting a purpose for polymorphism. Code fragments for each version are presented in the discussion, and links to the complete programs are given at the end of the section.
The completed Actor 3 example consists of five classes connected by three class relationships: inheritance, aggregation, and composition. The last two relationships don't participate in polymorphism, so the demonstration includes the composed and aggregated classes without modification. All the changes made here center around the three classes related by inheritance.
Basic Polymorphism
The chapter began with a list of five elements that programs must provide to enable polymorphism:
Inheritance
Function overriding
A pointer or reference variable (polymorphism cannot operate through an automatic or local variable)
Upcasting
A virtual function
As a practicing computer scientist, no one will ask you to list these elements. However, reading and understanding the behavior of a program is an expected skill. In the case of polymorphism, you must recognize the presence or absence in a program of the five elements. As you study the following changes to the Actor program, compare the changes to the list of requirements. A program must implement all five features to activate polymorphism.
The Actor 3 example has three classes, Person, Actor, and Star, related by inheritance. Each class defines a display function that has a void return type and an empty parameter list. So, the display function satisfies the requirements for function overriding. So, Actor 3 has the first two "ingredients" for polymorphism, but we must modify the example to get the last three.
The last step that we must take is to switch on polymorphism by making one of the functions in the Person class virtual (requirement 5). For this example, we will make the display function virtual (i.e., polymorphic) and contrast its behavior to that of the inserter function, which, because it is a non-member friend function, cannot be polymorphic.
An Extended Polymorphism Example
The extended example relies on a dynamic data structure called a circularly linked list named CList. A data structure stores data (often objects), provides a set of (often standardized) operations that access the data, and organizes that data to optimize some operations. A linked list (see Figure 1) is a data structure that organizes the stored data linearly in a list that is formed or "held together" by pointers. A circularly linked list is a special kind of linked list where the pointer in the last data node points to the beginning of the list.
Although library-grade data structures can generally store any data type, the list used in this demonstration is limited for simplicity. It can only store instances of the Person class. There are many variations on linked lists, and this variation organizes the data alphabetically based on the person's name. This organization implies that the list can reorder the stored objects, so the storage order is not necessarily the same as the insertion order. We'll see that this organization makes a more interesting demonstration of polymorphism.
The CList source code may be difficult to understand at this point in your studies, but it will become quite clear after your data structures and algorithms class. Following standard object-oriented programming practices, we can use the CList class by understanding its public interface. Following common practice, we explore the CList public interface through a set of function prototypes:
Three of the CList functions (insert, search, and remove) require access to the name stored in each instance of Person. As the member variable storing the name is private, the Person class must define an appropriate getter function:
The getName function is the last part necessary to complete the extended polymorphism example. The following main creates a CList, adds three different kinds of Person objects to the list, and then prints all of the information for each object: