CS 14 - Lab 7
CS 14
Homepage
Virtual Functions
You will be using the code you wrote from last weeks lab and adding to it
to demonstrate the use of 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.
Pre-lab Preperation
You must complete lab 6 before this lab section. You will use your code from
lab 6 to build upon.
The Lab
Add the shape class to your code from lab 6. 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). You may use your
overloaded insertion operator if you'd like. 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 main.cc for testing your code.
The output of this code should look something like
this.
Point Breakdown - In Lab Demo
For this assignment, you will demo your program in lab. Please show the output
from main.cc to the TA. Remember, you can get partial
credit for showing that you have some of the correct results.
The TA will also
look at your code for correctness when checking out. Remember, to
receive credit for this lab, you MUST turn the code in online as well
as demo in lab, however, you will only receive points for what you demo
to the TA during lab section. 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. Remember to put both partners names on the code that you
turn in.
Remeber to compile your code using the flags "-g -Wall -W -Werror -pedantic". You must
use a makefile.
- 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
- 3 points - 1 point for each test for tests 1 to 3
- 2 points - for test 4
- 1 point - for test 5
- 2 points - for test 6
- Deductions:
- -1 point - not logically seperating your code into seperate files
- -1 point - not using protections correctly (i.e. just making everything
public)
- -1 point - no 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.
- +.5 Extra Credit - Using the overloaded insertion operator in
printShapeInfo.