Description
Objectives:
This lab will cover the following topics:
Polymorphism
Persistence
Run Time Type Identification
The concepts in this lab will be discussed using the following classes:
The Shape Classes
Consider the following class diagram.
Shape is an abstract class because it contains a pure virtual function CalcArea. Note how the virtual and pure virtual functions are marked in the class diagram.
Rectangle and Circle inherit from the Shape class. In these classes, ReadData, WriteData, and CalcArea all are written to over-ride the corresponding methods in the base class.
Download the header file for the shape classes here and the .cpp file here. You should spend some time studying this code to better understand how the classes are related and the classes are set up to use polymorphism and persistence.
Persistence
If an object has persistence it means that the object can exist outside of the program. We make an object persistent by having the object write itself to a file. In the Shapes example we make Shape objects persistent by implementing the readData and writeData functions in such a way that the data can be identified and read in properly. Study how this is done in the sample code.
Run Time Type Identification and Down-Casting
Occasionally it is necessary to downcast a base class pointer to be a derived class pointer. For example, this is required when we need to access data or a function that is defined in a derived class, but we have are give a base class pointer to the object. Before doing a downcast, you have to test the pointer to see if it in fact, points to a derived class object. This uses a concept in C++ known as run time type identification, or rtti. Once we have discovered that the pointer points to a derived class object, the pointer can be downcast to give access to the data or function declared in the derived class. To better understand the use of rtti and down casting, study the driver code for this program, located here.
The Driver
Download the driver code here. Study the code carefully to see how the driver makes use of Polymorphism to simplify the task of dealing with objects in an inheritance tree. The Driver does the following operations:
It Creates an Array of Shape pointers.
It creates some Rectangle objects and some Circle objects and stores the pointers to these objects in the array.
The driver then displays the name and area of each object in the array. Because we have an array of base class pointers that point to objects of the derived classes, polymorphism kicks in and makes this step really easy.
Then the program displays the name in each object and its radius if it is a circle, or its height and width if it is a rectangle. since the getters for radius, height, and width are unique to the circle and the rectangle class respectively, the base Shape class pointer that we have will not allow us to access that data. Therefore, we have to downcast the pointer to get to that data.
The program now writes each object’s data to a file. Notice how polymorphism makes this step so easy. We simply call each object’s WriteData( ) method. Because of polymorphism, the correct methods are called for each object. Also note that the first piece of data writen to the file is a string that tells us what kind of data follows, rectangle data or circle data.
The file is now closed.
The four objects that we created are now deleted, and the elements of the array are all set to null.
The data is now read back in from the file. First the program reads a string to see what kind of data follows. Then an object of the appropriate type is created. The object is told to read its data from the file and the reference to the object is added to the array.
Finally, the program displays the name value stored in each object.
Lab Assignment
For this assignment you need to modify the classes in your Employee class hierarchy to make them ready for the next programming project. Carefully reflect on the Shapes example as you think about the changes that need to be made.
You need to make all of the classes in the Employee class hierarchy persistent.
You need to define the calcPay( ) method in the Employee class as an abstract method and override calcPay in each of the derived classes so that you can use polymorphism to calculate the payroll.
Every class needs to implement readData( ) and writeData( ). Make sure that your classes all have strong cohesion, that is, each class is responsible for managing the data defined in that class. You will also use polymorphism to read objects from a file and write objects to a file, so the readData( ) and writeData( ) methods in your derived classes will override those methods in the Employee class.
After carefully studying this code, create a class diagram for the Employee classes, illustrating these changes.
Submitting Your Assignment
After you are satisfied with your class diagram create a PDF file of your diagram and submit it to Canvas as Lab #10.
Grading Criteria
Description
Points possible
Your points
Assignment meets the submission guidelines.
o Your class diagram is in a .pdf file
o Your class diagram was properly submitted to Canvas
2
The class diagram is correct.
3
Total
5