MET CS 563 FALL 2008

HOMEWORK 9-Part I: Inheritance Polymorphism

DUE: Saturday, 12/1e/08

TOTAL: 100pts

1.  Inheritance (50 pts)

Implement a base class Person. Derive classes Student and Instructor from Person. A person has a name and a birthday. A Student has a major, and an Instructor has a salary. Write the class definitions, and the member functions print() for all classes. Add access methods as needed. There should be one constructor for each object, which accepts as parameters all members necessary for that object (recall that since Student and Instructor are derived from Person, they must also accept parameters for name and birthday since these need to be passed to the parent class, Person.

Implement a main() which does the follow things:

·  Create a Person object with the name “Fred” and the birthday “March 14, 1987”. Call the print() on this object.

·  Create a Student object with the name “Bill” and the birthday “January 13, 1985” and the major “Computer Science”. Call the print() on this object.

·  Create an Instructor object with the name “Bill” and the birthday “January 13, 1985” and the salary Call the print() on this object.

·  Create a Person object dynamically with the name “Fred” and the birthday “March 14, 1987”. Call the print() on this object. Free the memory for this object.

·  Create a Student object dynamically with the name “Bill” and the birthday “January 13, 1985” and the major “Computer Science”. Call the print() on this object. Free the memory for this object.

·  Create an Instructor object dynamically with the name “Bill” and the birthday “January 13, 1985” and the salary Call the print() on this object. Free the memory for this object.

***Remember to create an object dynamically, you must use the “new” keyword and obtain a pointer to the object. For example:

int* pDynamicInt = new int(5); // Create an int dynamically.

///… Do some stuff here

delete pDynamicInt; // free memory

Also recall that the parameters for the object can be passed in after the “new” declaration. For instance, if you have a constructor for a class MyClass which accept too ints as parameters, foo and bar, you can do:

int foo = 10;

int bar = 5;

MyClass* pMyClass = new MyClass(foo, bar);

Draw and UML diagram of the class hierarchy and include it in the documentation file of Assignment 9 <first_name<last_name>-A9-Documentation.doc along with the documentation of problem 2

Structure your program in seven files:

file / contents
<first_name<last_name>-A9.1-Person.h / Person class definitions
<first_name<last_name>-A9.1-Person.cpp / Person method definitions
<first_name<last_name>-A9.1-Student.h / Student class definitions
<first_name<last_name>-A9.1-Student.cpp / Student method definitions
<first_name<last_name>-A9.1-Instructor.h / Instructor class definitions
<first_name<last_name>-A9.1-Instructor.cpp / Instructor method definitions
<first_name<last_name>-A9.1-InheritanceTest.cpp / Vector3D method definitions

2.  Dynamic Binding, also called Late Binding or Polymorphism (50 pts)

Consider a graphics System that has classes for various figures: rectangles, squares, triangles, circles, etc. A rectangle has data members height, width, and center point. A square has center point and an edge. A circle has data members center and radius. The several specific shape classes are derived from a common class, Figure.

Implement only Rectangle and Triangle classes. Derive them from Figure.

Each class has stubs for erase and draw that only print a message identifying the functions, e.g.

erase of the Rectangle class outputs Rectangle::erase()

draw of the Triangle class outputs Triangle::draw()

etc.

The class Figure has member function center that calls erase and draw. The functions erase and draw are only stubs, consequently center does little but call erase and draw. Add a message to center announcing that it is being called.

Part I: Sketch out the class hierarchy

a) Write the class definitions using no virtual functions. Compile and test.

b) Make the base class member functions virtual. Compile and test.

c) Explain the differences.

Use the following main function:

int main()

{

Triangle tri;

tri.draw();

cout < "\nDerived class Triangle object calling"

< " center().\n";

tri.center();

Rectangle rect;

rect.draw();

cout < "\nDerived class Rectangle object calling"

< " center().\n";

rect.center();

return 0;

}

Part II: Flesh out the class methods

Give new definitions and member functions for Figure::center, Figure::draw, Figure::erase, Triangle::draw, Triangle::erase, Rectangle::draw, Rectangle::erase, so that the draw functions actually draw figures on the screen using asterisks (*). For the erase function, clear the screen, e.g. by outputting blank lines. There are lot of details in this and you will have to decide on some of them on your own

Write a program for testing each one of the functions and also to demonstrate dynamic binding.

Files for problem 2 (only the final version with completed method definition needs be submitted:

<first_name<last_name>-A9.2-Graphics.h / Problem 2 Class definitions
<first_name<last_name>-A9.2-Graphics.cpp / Problem 2 Method Implementation
<first_name<last_name>-A9.2-GraphicsTest.h / Problem 2 Test function
<first_name<last_name>-A9-Documentation.doc / 1. Documentation for problem 1:
-  UML diagrams of the class hierarchies;
-  list of file names and their contents;
-  discussion of inheritance vs. composition
2. Sample run for the stub version with explanation of differences if any.