Polymorphism for C++ Shape class, Unix based with use of makefile


Program

This short program covers inheritance and polymorphism.

You will create a series of classes to represent some simple geometric shapes and a small program to test your classes.

class Shape

Shape is an abstract base class.

Data Members

A Shape has the following private data member:

  • a color, which is of type string,

Methods

The Shape class should have the following public methods:

  • a constructor that takes a const string& argument and uses it to initialize the shape’s color. Since the Shape class is abstract, this constructor will only be invoked by a derived-class constructor.
  • virtual destructor. The method body for this destructor can be empty, since it does not need to delete any dynamic storage. If you don’t have a virtual destructor for an abstract base class, the compiler may produce a warning message.
  • virtual method called print() that takes no arguments and returns nothing. The method should print the color.
  • pure virtual method called get_area() that takes no arguments and returns a double. Since it is pure virtual (or abstract), this method has no definition, only a prototype. It must be defined in any concrete class derived from Shape.

class Circle

Circle is derived from Shape using public inheritance.

Data Members

A Circle has the following private data member:

  • a radius, which is of type int,

Methods

The Circle class should have the following methods:

  • a constructor that takes a string to initialize the circle’s color and an int to initiallize the circle’s radius. The color string should be passed to the Shapeconstructor.
  • an overridden version of print() that takes no arguments and returns nothing. The method should call the base class print() method to print the color, then print the word “circle” followed by the circle’s radius and area, e.g.:
  • green circle, radius 10, area 314.159
  • an overridden version of get_area() that takes no arguments and returns a double. This method should compute and return the circle’s area based on its radius.

class Rectangle

Rectangle is derived from Shape using public inheritance.

Data Members

A Rectangle has the following private data member:

  • a height, which is of type int,
  • a width, which is of type int,

Methods

The Rectangle class should have the following methods:

  • a constructor that takes a string to initialize the circle’s color and two ints to initialize the rectangle’s height and width. The color string should be passed to the Shape constructor.
  • an overridden version of print() that takes no arguments and returns nothing. The method should call the base class print() method to print the color, then print the word “rectangle” followed by the rectangle’s height, width, and area, e.g.:
  • red rectangle, height 8, width 6, area 48
  • an overridden version of get_area() that takes no arguments and returns a double. This method should compute and return the rectangle’s area based on its height and width.

class Triangle

Triangle is derived from Shape using public inheritance.

Data Members

A Triangle has the following private data member:

  • a height, which is of type int,
  • a base, which is of type int,

Methods

The Triangle class should have the following methods:

  • a constructor that takes a string to initialize the triangle’s color and two ints to initialize the triangle’s height and base. The color string should be passed to the Shape constructor.
  • an overridden version of print() that takes no arguments and returns nothing. The method should call the base class print() method to print the color, then print the word “triangle” followed by the triangle’s height, base, and area, e.g.:
  • black triangle, height 4, base 10, area 20
  • an overridden version of get_area() that takes no arguments and returns a double. This method should compute and return the rectangle’s area based on its height and width.

Main Program

Write a test program that creates either an array or an STL vector of pointers to Shape objects.

Dynamically create some Circles, Rectangles, and Triangles (at least two of each). After creating each object, add it to the array or vector.

Loop through the array or vector of Shape pointers and call the print() method for each of them.

Loop through the array or vector of Shape pointers again and call the print() method for each of the Triangle objects in the array or vector.

Loop through the list of Shape pointers one more time and delete each object.

Output example:

Printing all shapes…

green circle, radius 10, area 314.159

red rectangle, height 8, width 6, area 48

yellow triangle, height 8, base 4, area 16

black triangle, height 4, base 10, area 20

orange circle, radius 5, area 78.5397

blue rectangle, height 3, width 7, area 21

 

Printing only triangles…

yellow triangle, height 8, base 4, area 16

black triangle, height 4, base 10, area 20

Other Points

  • Your code should enforce const correctness. Methods that do not alter the object should be const, for example.
  • Code for each of your classes should be placed in separate files in the usual fashion for non-template C++ classes. That means a header file and a source code file for each class. Do not submit all of the code in one big file just because you’re feeling lazy. 🙂
  • You must have a complete makefile, with a separate rule for compiling a separate object file for the code from each source code file. The linking rule should link all of your object files together to create one executable. The name of your final executable should be main.
  • Remember, use of the dynamic_cast operator requires Run Time Type Information (RTTI) to be enabled. It’s on by default in g++, but may not be if you’re working with a different compiler / IDE.
  • Don’t forget to get rid of all compiler warnings when the -Wall compilation option is used.

Leave a Reply

Your email address will not be published. Required fields are marked *



  • File Format: C++ .cpp, C++ Header .h, Makefile
  • Platform: Linux