CS 14 - Lab
CS 14
Homepage
Class Inheritance, Operator Overloading, and Virtual Functions
In this lab you will get some programming experience with class inheritance,
operator overloading, and virtual functions.
You will start with 3 seperate classes that together
will define a cylinder and its location. The cylinder is made
up of its height (Cylinder class), area of a 2D splice of the cylinder
(Circle class), and xy point locations (Point class). You will have
to make good judgements on what functions/variables should be
public or private and what kind of inheritance to use. (Remember to use
good encapsulation)
Class Inheritance
Write a class called Point. It will contain two data members of type double
to hold the x and y coordinates of the point. It should also contain the
following function members:
- Point ( double x, double y ) - The constructor will take two double parameters that are used to
initialize the x and y cooridinates
- resetValues ( double x, double y ) -
A function called resetValues that takes two parameters and sets the x
and y coordinates
- A function called getX that returns the value of the x coordinate.
- A function called getY that returns the value of the y coordinate.
- Overload the insertion (<<) operator so that it can print out a point
class (the x and y coordinates).
Now write a class called Circle. It will inherit the class Point and have
one double data member for the radius. Write the following member
functions.
- Circle ( double radius, double x, double y ) -
The constructor will take three double parameters. One of those will be
the radius of the circle and the other two are used to initialize the
x and y coordinates representing the center of the circle.
- A function called resetRadius that takes one parameter and sets the
radius
- A function called getRadius that returns the radius of the circle.
- A function called getArea that calculates and returns the area of a
circle (PI * r^2). You will use PI = 3.14159265.
- Overload the insertion (<<) operator so that it can print out a circle
class (the x and y coordinates and the radius).
Lastly, write a class called Cylinder. This class will inherit the class
Circle. It will have a data member of type double to hold the height. It
will also contain the following member functions.
- Cylinder ( double height, double radius, double x, double y ) -
The constructor should take four double parameters. One will represent
the height of the cylinder and the other three will represent the radius,
x, and y coordinates and should be passed on to the circle constructor.
- A function called resetHeight that takes one parameter and sets the
height.
- A function called getHeight that returns the height of the cylinder
- A function called getVolume that calculates and returns the volume of the
cylinder (circleArea * height)
- Overload the insertion (<<) operator so that it can print out a cylinder
class (the x and y coordinates, the radius, and the height).
You should now be able to pass tests 1 through 3 in main.cc.
Virtual functions
An Abstract Base Class is a class in which you never intend to declare an
object of that type. It is solely used with inheritance to provide an
appropriate base class from which classes may inherit interface or
implementation. Abstract Base Classes typically do not contain data
members. This is an example of an Abstract
Base Class.
Shape.h has a couple of virtual functions. The first
two are called getArea and getVolume and simply return zero. These functions
will be used in the following way. The point class will inherit the Shape
class, thus inheriting these functions. This is appropriate since the area
and volume of a point are both zero. However, Circle inherits Point and
provides its own getArea function which will overide the zero returning getArea
function. However, the volume of a Circle is still zero. Cylinder inherits
from Circle and redefines its own getArea and getVolume functions.
The virtual function printShapeInfo does not do anything in the Shape function
and each function will overide this function to print the type of
shape it is.
Add the shape class to your code. Make
Point inherit the Shape class
Add a virtual function called printShapeInfo to the classes Point,
Circle, and Cylinder that prints the type of object to the screen and the
information stating the location of the object (note, do not include the
printing of the area or volume in this function). Use your
overloaded insertion operator. To do this you would have to
dereference the this pointer.
Write a member function getArea in the class Cylinder that computes and returns
the surface area of a cylinder. The formula is:
2 * circleArea + 2 * PI * radius * height
Check your code and verify for yourself how the virtual functions are
working. You may use tests 4 and 5 in main.cc for testing your code.
Point Breakdown - In Lab Demo
You must demo all programs in lab. The TA will look at the output of each
program and also
look at your code for correctness when checking out. Remember, to
receive credit for this lab, you MUST turn the code in online even if
you have already demoed in lab If you do not turn in your code online,
you will lose points for your lab. All code must be turned in online for
archival purposes.
Remeber to compile your code using the flags "-g -Wall -W -Werror -pedantic". You
must have a makefile for this lab.
- 2 points - Attendance - Lab attendance will be 20% of the grade for each
lab. You will receive 1 point when the lab begins and 1 point when the lab
ends. Attendance will be taken during the first and last 5 minutes of the
lab period
- 1 point - Completing mid-quarter evaluations (must be done during lab)
- 5 points - 1 point for each test case passed
- 1 point - operator overloading output. (Must implement operator
overloading correctly for the cylinder, circle, and point class and
you must use them to get these points, the TAs will check your code)
- 1 point - Good use of encapsulation i.e. not making everything public
- Deductions:
- -1 point - If both partners turn in the code. There should
only one turnin per group
- -1 point - not logically seperating your code into seperate files
- -1 point - not using a makefile
- -5 points - Turned into the wrong lab section.
- -1 point - No name on work turned in
- -5 points - Code not turned in at all. This deduction also applies to
turnins that do not contain a the partners name (only the name that is not
on the turnin will have 5 points deducted. The person that actually turned
the code in will only have 1 point deducted if their name is not on the
turnin).
- -5 points - If your currently assigned partner is in lab and you do not
work together.
- -1 point - If your code seg faults at any time.