Discount Get COMP 220 ALL WEEKS Solution ONLY $92.95 $123.93
COMP 220 Week 1 - Blackjack Program C++ [$19.99]
COMP 220 Week 2 - Resistor Program C++ [$15.99]
COMP 220 Week 3 - Bank Account Program C++ [$19.99]
COMP 220 Week 4 - Filter Program C++ [$19.99]
COMP 220 Week 5 - Pointers and Pointer Operations C++ [$15.99]
COMP 220 Week 6 - Filter Program C++ [$15.99]
COMP 220 Week 7 - Polymorphism C++ [$15.99]
Get Selected Weeks Solution with ONLY NOT Add Selected Tutorials To Cart
COMP220 COMP 220 COMP/220 Week 7 - Lab 7 - iLab 7 - Polymorphism C++
    COMP220 COMP 220 COMP/220 Week 7 - Lab 7 - iLab 7 - Polymorphism C++

COMP220 COMP 220 COMP/220 Week 7 - Lab 7 - iLab 7 - Polymorphism C++

$15.99

iLab 7 of 7: Polymorphism

i L A B  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.

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.

i L A B  S T E P S            

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.

REQUIRED MAIN

Use this main/driver file to use to TEST the code you write;

You are required to use it.

Sincerely,

======================================================================

 #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

  specificand 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;

 }

My tutorial contains Visual C++ 2010 Project.

If you have more questions, please contact me via email support@extutorials.com

I will help you any time

Thank you !

Attached Files

Move over [ preview ] file name to preview content in it!

  • COMP220 iLab7.zip [32.3KB]
    • COMP220 iLab7
      • Circle.cpp [0.6KB] [ preview ]
      • Circle.h [0.3KB] [ preview ]
      • COMP220 iLab7.vcxproj [4.1KB]
      • COMP220 iLab7.vcxproj.filters [1.5KB]
      • COMP220 iLab7.vcxproj.user [0.1KB]
      • Rectangle.cpp [0.8KB] [ preview ]
      • Rectangle.h [0.4KB] [ preview ]
      • Shape.cpp [81Bytes] [ preview ]
      • Shape.h [0.2KB] [ preview ]
      • TestMain.cpp [4.5KB] [ preview ]
    • COMP220 iLab7.sln [0.9KB]
    • COMP220 iLab7.suo [18.4KB]

Write a review

Your Name:


Your Review: Note: HTML is not translated!

Rating: Bad           Good

Enter the code in the box below:



PURCHASE SAVE
2 Tutorials 10%
3 Tutorials 13%
4 Tutorials 16%
5 Tutorials 19%
6 Tutorials 22%
7 Tutorials 25%
8 Tutorials 28%
9 Tutorials 31%
10 Tutorials 34%
add url more

9MZQASHWN73B