akosiraff

Polymorphism C++

Mar 5th, 2013
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.49 KB | None | 0 0
  1. Download: http://solutionzip.com/downloads/polymorphism-c/
  2.  
  3. I-Lab 7 of 7: Polymorphism
  4. i L A B O V E R V I E W
  5. Scenario and Summary
  6. 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.
  7. 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.
  8. Deliverables
  9. 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.
  10. 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.
  11. 2. Submit a lab report (a Word document) containing the following information to the Week 7 Dropbox.
  12. o Include your name and the lab or lab exercise number.
  13. o Specification: Include a brief description of what the program accomplishes, including its input, key processes, and output.
  14. 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.
  15. 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.
  16. 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.
  17. o Answers to Lab Questions: Include the answers to all the lab questions that are asked in the lab steps.
  18. Each lab exercise should have a separate section in the lab-report document.
  19. Your lab grade is based upon
  20. 1. the formatting of your source code;
  21. 2. the use of meaningful identifiers;
  22. 3. the extent of the internal documentation;
  23. 4. the degree to which an exercises’ specifications are met; and
  24. 5. the completeness of your lab report.
  25. i L A B S T E P S
  26. STEP 1: Create a New Multifile Project
  27. 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.
  28. 1. The class Shape should be an abstract class with the following pure virtual functions.
  29. a. area()
  30. b. perimeter()
  31. 2. The class Circle should be derived public from the class Shape and override both the area() and the perimeter() functions.
  32. a. The perimeter() function should correctly calculate the circumference of a circle, given a radius.
  33. b. The area() function should correctly calculate the area of a circle, given a radius
  34. c. Include all the necessary accessor and mutator functions to accomplish the requirements of the class.
  35. 3. The class Rectangle should be derived public from the class Shape and override both the area() and the perimeter() functions.
  36. a. The perimeter() function should correctly calculate the circumference of a rectangle, given its dimensions.
  37. b. The area() function should correctly calculate the area of a rectangle, given its dimensions.
  38. c. Include all the necessary accessor and mutator functions to accomplish the requirements of the class.
  39. STEP 2: Create the Test Function
  40. 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.
  41. STEP 3: Add a Base Class Pointer Array and an Additional Function
  42. 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.
  43. 1. Circle objects should display radius, circumference, and area.
  44. 2. Rectangle objects should display dimensions, perimeter. and area.
  45. The information-display function should accept as its calling parameter a pointer of the class Shape.
  46. 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.
  47. There are 8 files required for week 7 ilab.
  48. Circle.cpp
  49. Circle.h
  50. MyRectangle.cpp
  51. MyRectangle.h
  52. Shape.cpp
  53. Shape.h
  54. ShapeMain.cpp // Use this file provided by the professor.
  55. stdafx.h // This file has been provided by the professor.
  56. // ShapeMain.cpp
  57. #include “stdafx.h”
  58. /******************************************************************
  59. * Course: COMP220 *
  60. * Class Name: ShapeTest
  61. * Class Description:
  62. This program demonstrates polymorphism with the use of abstract classes
  63. and virtual functions by using a generalized procedure with a Shape
  64. abstract class as an parameter, but then passing in derived classes
  65. objects as arguments. The object functions that are invoked are type
  66. specific and the derived classes object functions are invoked NOT
  67. the abstract base class functions.
  68. This demonstrates that you can easily add addition derived classes
  69. and not have to change the underlying generic function.
  70. ***********************************************************************/
  71. void createShapeObjects(void);
  72. void usingObjectArrays(void);
  73. void displayArrayObjects(Shape *shapeArray, int size);
  74. void printShapeInformation(Shape *shapePtr);
  75. void main(void)
  76. {
  77. char ch;
  78. cout << "Program Title: Demonstrating Polymorphism" << endl << endl;
  79. //perform lab step 2 test
  80. cout << "Step 2 Test: object instantiation" << endl;
  81. cout << "--------------------------------------------" << endl;
  82. createShapeObjects();
  83. cout << endl << "--------------------------------------------" << endl;
  84. //perform lab step 3 test
  85. cout << "Step 3 Test: object arrays" << endl;
  86. cout << "--------------------------------------------" << endl;
  87. usingObjectArrays();
  88. cout << "--------------------------------------------" << endl;
  89. cout << "Press any key to terminate the application..." << endl;
  90. cin >> ch;
  91. }
  92. void usingObjectArrays(void)
  93. {
  94. //this function implements Step 3 of the lab requirements
  95. Shape *shapeList[2];
  96. //test the default constructors
  97. Circle myCircle;
  98. MyRectangle myRectangle;
  99. shapeList[0] = &myCircle;
  100. shapeList[1] = &myRectangle;
  101. //test the default constructors
  102. cout << "Normal operaton test: Object Array, default constructors" << endl;
  103. for(int i = 0; i < 2; i++)
  104. printShapeInformation(shapeList[i]);
  105. //test the mutators
  106. cout << "Normal operaton test: Object Array, mutators" << endl;
  107. myCircle.setRadius(5);
  108. myRectangle.setLength(10);
  109. myRectangle.setWidth(30);
  110. for(int i = 0; i < 2; i++)
  111. printShapeInformation(shapeList[i]);
  112. }
  113. void createShapeObjects(void)
  114. {
  115. //this function implements Step 2 of the lab requirements
  116. Circle *myCircle;
  117. MyRectangle *myRectangle;
  118. myCircle = new Circle(2.5);
  119. myRectangle = new MyRectangle(4, 10);
  120. cout << "Normal operation test: object creation" << endl;
  121. //invoke the printShapeInformation and pass in the circle object pointer
  122. printShapeInformation(myCircle);
  123. //invoke the same printShapeInformation function, but this time pass
  124. //in a rectangle shape pointer
  125. printShapeInformation(myRectangle);
  126. //access the mutators to ensure they work correctly
  127. cout << endl << "Normal operation test: mutators" << endl;
  128. myCircle->setRadius(10);
  129. printShapeInformation(myCircle);
  130. myRectangle->setLength(15);
  131. myRectangle->setWidth(20);
  132. printShapeInformation(myRectangle);
  133. //access the mutators and pass in invalid data to ensure default is set
  134. //access the mutators to ensure they work correctly
  135. cout << endl << "Robustness operation test: mutators" << endl;
  136. myCircle->setRadius(-10);
  137. printShapeInformation(myCircle);
  138. myRectangle->setLength(0);
  139. myRectangle->setWidth(-10);
  140. printShapeInformation(myRectangle);
  141. }
  142. //create a function that accepts an abstract shape object, then operates
  143. //on the virtual functions, this will allow us to pass any of the
  144. //derived types into this function since all the derived types of Shape must implement
  145. //the pure virtual functions and the invariant function “getType” is inherited
  146. //by all the derived types.
  147. void printShapeInformation(Shape *shapePtr)
  148. {
  149. cout << "--------------------------------------------" << endl;
  150. cout << right;
  151. cout << shapePtr->getType() << " Information" << endl;
  152. shapePtr->displayInformation();
  153. cout << setw(12) << "Area: " << shapePtr->area() << endl;
  154. cout << setw(12) << "Perimeter: " << shapePtr->perimeter() << endl;
  155. cout << endl;
  156. }
  157. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  158. // stdafx.h
  159. // stdafx.h : include file for standard system include files,
  160. // or project specific include files that are used frequently, but
  161. // are changed infrequently
  162. //
  163. #pragma once
  164. #ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
  165. #define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
  166. #endif
  167. #include
  168. #include
  169. #include
  170. #include
  171. #include
  172. #include
  173. using namespace std;
  174. #define PI 3.14159
  175. #ifndef SHAPE_OBJ
  176. #define SHAPE_OBJ
  177. #include “Shape.h”
  178. #endif
  179. #ifndef CIRCLE_OBJ
  180. #define CIRCLE_OBJ
  181. #include “Circle.h”
  182. #endif
  183. #ifndef RECTANGLE_OBJ
  184. #define RECTANGLE_OBJ
  185. #include “MyRectangle.h”
  186. #endif
  187. Download: http://solutionzip.com/downloads/polymorphism-c/
Advertisement
Add Comment
Please, Sign In to add comment