login   register  
 All content arrowdown greybar search
 Tutorial  

COMP220 - iLab 7

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.

  • The class Shape should be an abstract class with the following pure virtual functions. An Abstract Class cannot be instantiated, You can only create objects from derived classes.
    1. area() Pure virtual functions have the prefix virtual and the suffix =0, virtual void show_area() = 0;
    2. perimeter() Pure virtual functions must be defined in the derived classes
  • The class Circle should be derived public from the class Shape and override both the area() and the perimeter() functions.
    1. The perimeter() function should correctly calculate the circumference of a circle, given a radius. The derived class functions don't need the prefix virtual
    2. The area() function should correctly calculate the area of a circle, given a radius keyword virtual is only needed in the base class
    3. Include all the necessary accessor and mutator functions to accomplish the requirements of the class.
  • The class Rectangle should be derived public from the class Shape and override both the area() and the perimeter() functions.
    1. The perimeter() function should correctly calculate the circumference of a rectangle, given its dimensions.
    2. The area() function should correctly calculate the area of a rectangle, given its dimensions.
    3. 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. I would suggest two constructors, one default constructor that prompts the user for input values
and one to set values needed to calculate the area and perimeter values. I would suggest you use the default constructor

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, non-class member function that will display all the information about each object.

  • Circle objects should display radius, circumference, and area.
  • Rectangle objects should display dimensions, perimeter. and area. Base and height is needed for area, perimeter is require some help from pythagoras

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.

An interesting thing about a base class pointer is a base class pointer can point to any class in the Inheritance tree: Base class, Sub class, Sub-Sub class, etc. Since you can change what a base class pointer points during run-time, this is referred to as "Run-Time Polymorphism" or "Dynamic (Late) Binding".

I would suggest add a few other geometric figures to the program and using a menu to select the figure to solve. The menu selection could determine the derived class object that the base class pointers points to.


COMP220 - iLab 7 1102

 Evangelist
quote
ttach...(3 more words & 4 attachments).
attachments
 Lab7.txt.txt (4KB)  
 Lab_Report.docx (170KB)  
 Polymorphism_program_code.zip (1288KB)  
 UML_diagram.jpg (28KB)  
 ~0 comment(s).

ask an expert