Sie sind auf Seite 1von 6

Download:

http://solutionzip.com/downloads/polymorphism-c/

I-Lab 7 of 7: Polymorphism iLABOVERVIEW 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. o Specification: Include a brief description of what the program accomplishes, including its input, key

processes, and output. o Test Plan: Include a brief description of the method you used to confirm that your program worked properly. If necessary, include a clearly labeled table with test cases, predicted results, and actual results. o Summary and Conclusions: Write a statement summarizing your predicted and actual output, and identify and explain any differences. For conclusions, write at least one nontrivial paragraph that explains, in detail, either a significant problem you had and how you solved it or, if you had no significant problems, something you learned by doing the exercise. o A UML Diagram: This should show all the classes, class members, access specifiers, data types, and function arguments, along with all of the class-to-class relationships. o Answers to Lab Questions: Include the answers to all the lab questions that are asked in the lab steps. Each lab exercise should have a separate section in the lab-report document. Your lab grade is based upon 1. the formatting of your source code; 2. the use of meaningful identifiers; 3. the extent of the internal documentation; 4. the degree to which an exercises specifications are met; and 5. the completeness of your lab report. iLABSTEPS STEP 1: Create a New Multifile Project Create a new multifile project with three classes: the base class Shape, the class Circle (derived public from Shape), and the class Rectangle (derived public from Shape). The classes will have the following requirements. 1. The class Shape should be an abstract class with the following pure virtual functions. a. area() b. perimeter() 2. The class Circle should be derived public from the class Shape and override both the area() and the perimeter() functions. a. The perimeter() function should correctly calculate the circumference of a circle, given a radius. b. The area() function should correctly calculate the area of a circle, given a radius c. Include all the necessary accessor and mutator functions to accomplish the requirements of the class. 3. The class Rectangle should be derived public from the class Shape and override both the area() and the perimeter() functions. a. The perimeter() function should correctly calculate the circumference of a rectangle, given its dimensions. b. The area() function should correctly calculate the area of a rectangle, given its dimensions. c. Include all the necessary accessor and mutator functions to accomplish the requirements of the class. STEP 2: Create the Test Function Instantiate at least one object of each of the Circle and the Rectangle classes. Provide appropriate constructors for both that will accept the necessary initialization arguments to provide the information required for all the class member functions. Exercise and test each member function of both classes for correct calculations and output. STEP 3: Add a Base Class Pointer Array and an Additional Function 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 stdafx.h /****************************************************************** * 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 getType 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

Download:

http://solutionzip.com/downloads/polymorphism-c/

Das könnte Ihnen auch gefallen