Homework 1 - Inheritance and Polymorphism
Due: Wednesday April 7, 2010 at 5:00pm
Coding Conventions
The following coding conventions should be used for all assignments.
Assignment
The purpose of this assignment is to create a class hierarchy using
inheritance and to see how polymorphism works when using a pointer of a parent
class. 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:
- Protected member variables
- Integers for x, y and length
- Public 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)
- Public member functions
- Default constructor (can invoke parent constructor)
- Copy constructor (can invoke parent constructor)
- Constructor that takes three integers (can invoke parent constructor)
- 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 constructor (can invoke parent constructor)
- Copy constructor (can invoke parent constructor)
- Constructor that takes three integers (can invoke parent constructor)
- 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. You will then call the
set_shape()
and print_shape()
member functions using
this parent class pointer. 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 try to allocate a new line or circle object, check that
the new line/circle was actually allocated and print a message if the
allocation failed. Be SURE to deallocate any old shape before allocating a
new shape when Option 1 or 2 is selected.
If the allocation works, Option 1 should prompt the user for the x,y
coordinates of the origin of the line and the length of the line. It should
then read those values into temporary integers. It should then call
set_shape()
to pass the values into the Line object.
Likewise, if allocation works, Option 2 should prompt for the x,y coordinates
of the origin of the circle and the radius of the circle, read those values
into temporary integers and call set_shape()
to pass those values
into the Circle object.
Option 3 should gracefully handle the case where 3 is selected before 1 or 2
(i.e. when there is no shape object allocated). It should print an error
message about no shape existing (core dumps when 3 is selected first will be
a deduction in your grade). If there is a shape allocated, Option 3 should
call print_shape()
to print information about the shape.
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.