Homework 1 - Inheritance and Polymorphism
Due: Friday April 4, 2008 at 5:00pm
This assignment focuses on the concepts of inheritance and polymorphism. You
will be defining three classes with the following hierarchy:
Shape
/ \
/ \
Line Circle
Shape class
The Shape class is the parent class for this exercise. It defines the general
characteristics of the shapes: a set of (x,y) coordinates and a length. The
Shape class should have the following:
- Member variables
- Integers for x, y and length
- Member functions
- A default constructor that sets all variables to the value 0
- A copy constructor
- A constructor that takes three integers to set the three variables
- virtual void print_shape() - A polymorphic function that prints
"Undefined shape" to stdout for the Shape class and will be redefined in
the children classes.
- void set_shape(int, int, int) - A function that takes three integers
to set the values of x, y and length.
Line class
This is a child class of the Shape class. For simplicity, this class will be
just a straight line. The (x,y) coordinate is the start of the line
and length indicates how long the line is.
(Note: You could implement slope by having a new member variable for
this class)
- Member functions
- Default, copy and three integer constructors (can invoke parent
constructors)
- virtual void print_shape() - For this class, print_shape should print
"Shape: Line X value: <x> Y value: <y> Length: <length>"
to stdout.
Circle class
This is a child class of the Shape class. The (x,y) coordinate is the center
of the circle and length is the radius.
- Member functions
- Default, copy and three integer constructors (can invoke parent
constructors)
- virtual void print_shape() - For this class, print_shape should print
"Shape: Circle Center: (<x>, <y>) Radius: <length>" to
stdout.
Menu Program
The menu program allows you to test if your implementation of the above
classes are correct. Your menu should be implemented in a fashion similar
to
menu_example.cpp.
Your main()
function must have a pointer variable
of type Shape that will be used to store the current shape. Remember to delete
the current shape before allocating a new one. The function to create each
new shape should return a pointer to the new shape due to the way Helios
handles pointers. See pointer_example.cpp
for an example of this.
Your menu should be as follows:
Welcome to the Shape Menu
===========================
1. Create a new line
2. Create a new circle
3. Print the current shape
0. Exit
===========================
Options 1 and 2 should check that the new line/circle was actually allocated
and print a message if the allocation failed.
Option 3 should gracefully handle the case where 3 is selected before 1 or 2.
It should print an error message about no shape existing (core dumps when 3
is selected first will be a deduction in your grade).
Submit your completed code as an attachment in an email to the instructor.
If you use multiple files (seperate compilation), be sure to include all
files in the attachment list.