I-Lab 7 of 7: Polymorphism i LAB O V E R V I E W Scenario and Summary This lab introduces students to the concepts of polymorphism, early binding, late binding, abstract classes, and virtual class functions. This will be done in the context of performing calculations on basic geometrical shapes. Polymorphism is a very powerful extension of inheritance, and by using pointers to the base class, it allows access to derived class objects and their functions based on the context that they are called in. The lab will require the creation of a base geometric class, called Shape, and two sub classes, Circle and Rectangle, that are derived public from the class Shape. From there, objects of both the Circle and the Rectangle classes will be created, as will an array of pointers to the base class Shape. By using the instantiated objects and the object pointers, both static and dynamic binding will be demonstrated. Deliverables 1. Submit a single NotePad file containing the source code for all the lab files to the Week 7 Dropbox. Your source code should use proper indentation and be error free. Be sure that your last name and the lab number are part of the file name: for example, YourLastName_Lab5.txt. Each program should include a comment section that includes, at a minimum, your name, the lab and exercise number, and a description of what the program accomplishes. 2. Submit a lab report (a Word document) containing the following information to the Week 7 Dropbox. o Include your name and the lab or lab exercise number.

Add to the test function a base class array of pointers of the same dimension as the total number of Circle and Rectangle objects that were created in the previous step. Use this pointer array to access the Circle and the Rectangle objects to call a new, nonclass member function that will display all the information about each object. 1. Circle objects should display radius, circumference, and area. 2. Rectangle objects should display dimensions, perimeter. and area. The information-display function should accept as its calling parameter a pointer of the class Shape. Run the test function to demonstrate static (early) binding using the derived class objects calling their member functions, and run the test function to demonstrate dynamic (late) binding using the assigned Shape class pointers to call the nonclass, member-display-information function. There are 8 files required for week 7 ilab. Circle.cpp Circle.h MyRectangle.cpp MyRectangle.h Shape.cpp Shape.h ShapeMain.cpp // Use this file provided by the professor. stdafx.h // This file has been provided by the professor. // ShapeMain.cpp #include â&#x20AC;&#x153;stdafx.hâ&#x20AC;? /****************************************************************** * Course: COMP220 * * Class Name: ShapeTest * Class Description: This program demonstrates polymorphism with the use of abstract classes and virtual functions by using a generalized procedure with a Shape abstract class as an parameter, but then passing in derived classes objects as arguments. The object functions that are invoked are type specific and the derived classes object functions are invoked NOT the abstract base class functions. This demonstrates that you can easily add addition derived classes and not have to change the underlying generic function. ***********************************************************************/ void createShapeObjects(void); void usingObjectArrays(void); void displayArrayObjects(Shape *shapeArray, int size); void printShapeInformation(Shape *shapePtr); void main(void) { char ch; cout << "Program Title: Demonstrating Polymorphism" << endl << endl; //perform lab step 2 test cout << "Step 2 Test: object instantiation" << endl;

cout << "--------------------------------------------" << endl; createShapeObjects(); cout << endl << "--------------------------------------------" << endl; //perform lab step 3 test cout << "Step 3 Test: object arrays" << endl; cout << "--------------------------------------------" << endl; usingObjectArrays(); cout << "--------------------------------------------" << endl; cout << "Press any key to terminate the application..." << endl; cin >> ch; } void usingObjectArrays(void) { //this function implements Step 3 of the lab requirements Shape *shapeList[2]; //test the default constructors Circle myCircle; MyRectangle myRectangle; shapeList[0] = &myCircle; shapeList[1] = &myRectangle; //test the default constructors cout << "Normal operaton test: Object Array, default constructors" << endl; for(int i = 0; i < 2; i++) printShapeInformation(shapeList[i]); //test the mutators cout << "Normal operaton test: Object Array, mutators" << endl; myCircle.setRadius(5); myRectangle.setLength(10); myRectangle.setWidth(30); for(int i = 0; i < 2; i++) printShapeInformation(shapeList[i]); } void createShapeObjects(void) { //this function implements Step 2 of the lab requirements Circle *myCircle; MyRectangle *myRectangle; myCircle = new Circle(2.5); myRectangle = new MyRectangle(4, 10); cout << "Normal operation test: object creation" << endl; //invoke the printShapeInformation and pass in the circle object pointer printShapeInformation(myCircle); //invoke the same printShapeInformation function, but this time pass //in a rectangle shape pointer

printShapeInformation(myRectangle); //access the mutators to ensure they work correctly cout << endl << "Normal operation test: mutators" << endl; myCircle->setRadius(10); printShapeInformation(myCircle); myRectangle->setLength(15); myRectangle->setWidth(20); printShapeInformation(myRectangle); //access the mutators and pass in invalid data to ensure default is set //access the mutators to ensure they work correctly cout << endl << "Robustness operation test: mutators" << endl; myCircle->setRadius(-10); printShapeInformation(myCircle); myRectangle->setLength(0); myRectangle->setWidth(-10); printShapeInformation(myRectangle); } //create a function that accepts an abstract shape object, then operates //on the virtual functions, this will allow us to pass any of the //derived types into this function since all the derived types of Shape must implement //the pure virtual functions and the invariant function â&#x20AC;&#x153;getTypeâ&#x20AC;? is inherited //by all the derived types. void printShapeInformation(Shape *shapePtr) { cout << "--------------------------------------------" << endl; cout << right; cout << shapePtr->getType() << " Information" << endl; shapePtr->displayInformation(); cout << setw(12) << "Area: " << shapePtr->area() << endl; cout << setw(12) << "Perimeter: " << shapePtr->perimeter() << endl; cout << endl; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // stdafx.h // stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently // #pragma once #ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later. #define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows. #endif #include #include

#include #include #include #include using namespace std; #define PI 3.14159 #ifndef SHAPE_OBJ #define SHAPE_OBJ #include “Shape.h” #endif #ifndef CIRCLE_OBJ #define CIRCLE_OBJ #include “Circle.h” #endif #ifndef RECTANGLE_OBJ #define RECTANGLE_OBJ #include “MyRectangle.h” #endif