This short assignment covers inheritance and subtype polymorphism. For this project, you will create a series of classes to represent some simple geometric shapes and a small program to test your classes.
None. You will write all of the files for this assignment including a suitable makefile.
For this assignment, you will need to write the following files:
Shape.h
- contains the class definition for the abstract Shape
class.Shape.cpp
- contains the member function definitions for the abstract Shape
class.Circle.h
- contains the class definition for the Circle
class.Circle.cpp
- contains the member function definitions for the Circle
class.Rectangle.h
- contains the class definition for the Rectangle
class.Rectangle.cpp
- contains the member function definitions for the Rectangle
class.Triangle.h
- contains the class definition for the Triangle
class.Triangle.cpp
- contains the member function definitions for the Triangle
class.main.cpp
- contains the main()
function, with code to create objects of the various classes and test them.
makefile
- a makefile to build the project.This probably seems like a lot, but all four of the classes are quite small and highly similar to each other.
class Shape
Shape
is an abstract base class.
Data Members
A Shape
has the following private data member:
color
, which is of type string
Member Functions
The Shape
class must have the following public member functions:
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.)
a virtual destructor. The method body for this destructor can be empty, since it does not need to delete any dynamic storage. However, if you don't have a virtual destructor for an abstract base class, the compiler may produce a warning message (as demonstrated in lecture.)
a virtual member function called print()
that takes no arguments and returns nothing. It must be const
. The function must print the color
and the area of the shape like this:
green [area 314.159]
a pure virtual method called get_area()
that takes no arguments and returns a double
. It must be const
. Since it is pure virtual (or abstract), this method has no definition, only a prototype. It must be defined in any concrete class that is derived from/is a subclass of Shape
.
class Circle
Circle
is derived from Shape
using public
inheritance.
Data Members
A Circle
has the following private data member:
radius
, which is of type int
Member Functions
The Circle
class must have the following member functions:
a constructor that takes a const string&
argument to initialize the circle's color and an int
to initialize the circle's radius. The color
string must be passed to the Shape
constructor using a constructor initialization list (as seen in lecture).
an overridden version of print()
that takes no arguments and returns nothing. The member function must call the base class print()
method (as seen in lecture) to print the circle's color and area, then print a comma, the word "circle", and the radius, e.g.:
green [area 314.159], circle radius 10
an overridden version of get_area()
that takes no arguments and returns a double
. This method must 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 members:
height
, which is of type int
width
, which is of type int
Member Functions
The Rectangle
class must have the following member functions:
a constructor that takes a const string&
argument to initialize the rectangle's color and two int
arguments to initialize the rectangle's height and width. The color
string must be passed to the Shape
constructor.
an overridden version of print()
that takes no arguments and returns nothing. The function must call the base class print()
method to print the rectangle's color and area, a comma, the word "rectangle" and the rectangle's height, width e.g.:
red [area 48], rectangle, height 8, width 6
an overridden version of get_area()
that takes no arguments and returns a double
. This method must 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 members:
height
, which is of type int
base
, which is of type int
Member Functions
The Triangle
class must have the following member functions:
a constructor that takes a const string&
argument to initialize the triangle's color and two int
arguments to initialize the triangle's height and base. The color
string must be passed to the Shape
constructor.
an overridden version of print()
that takes no arguments and returns nothing. The method must call the base class print()
method to print the triangle's color and area, a comma, print the word "triangle" and the triangle's height, and base e.g.:
black [area 20], triangle, height 4, base 10
an overridden version of get_area()
that takes no arguments and returns a double
. This method must compute and return the rectangle's area based on its height and width.
Write a main()
function that does the following:
Declare an array of pointers to Shape
objects.
Dynamically create Circles
, Rectangles
, and Triangles
based on the reference output below. After creating each object, add its address to the array.
Loop through the array of Shape
pointers and call the print()
method for each of them.
Loop through the array of Shape
pointers again and call the print()
method ONLY for each of the Circle
objects. In order to discern whether a Shape
is a Circle, use a dynamic_cast
as discussed in the course web site.
Loop through the list of Shape
pointers one more time and delete each object.
Output from your program must look like this:
Printing all shapes... green [area 314.159], circle, radius 10 red [area 48], rectangle, height 8, width 6 yellow [area 16], triangle, height 8, base 4 black [area 20], triangle, height 4, base 10 orange [area 78.5398], circle, radius 5 blue [area 21], rectangle, height 3, width 7 Printing only circles... green [area 314.159], circle, radius 10 orange [area 78.5398], circle, radius 5
Note that when grading your output, we will not require perfect whitespace match. We will use a diff command with the following parameters to check your output:
diff -w -a youroutput.txt answerkey.txt
Your code must enforce const
correctness. Member functions that do not alter the object must be const
.
Code for each of your classes must 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. Failure to do so will result in a grade of zero.
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 must link all of your object files together to create one executable. The name of your final executable must be assign8
. You have plenty of example makefiles from previous assignments to look at and use as examples. If you are having trouble with this, reach out to your TA or instructor.
Everything inheritance- and polymorphism-related that needs to be done in this assignment is also done in the inheritance example available on the course website. Use that as your model.
dynamic_cast
operator requires Run Time Type Information (RTTI) to be enabled. It's on by default in g++
on turing & hopper, but may not be if you're working with a different compiler/IDE. If something seems strange, do your development and testing on turing or hopper.As always, programs that do not compile on turing/hopper
automatically receive 0 points.
Submit your program using the electronic submission guidelines posted on the course web site and discussed in class.