CS
2500 Spring 2011
Lab 4
Abstract Data Types and C++ Classes
Please see modifications below
We want to
create an ADT that represents a rectangle (similar to the one for
a sphere we saw in class). Below is a pseudocode
implementation of the operations for the rectangle ADT.
+Rectangle()
// Creates a rectangle and initializes its length and width to default
// values.
+Rectangle (in length:double, in width:double)
// Creates a rectangle and initilializes its length and width to the
// values received as parameters.
+setLength (in length:double)
// Sets or modifies the length of an existing rectangle.
// Checks to make sure that the new length is greater than 0.
+setWidth (in width:double)
// Sets or modifies the width of an existing rectangle.
// Checks to make sure that the new width is greater than 0.
+getLength():double {query}
// Returns a rectangle’s length.
+getWidth():double {query}
// Returns a rectangle’s width.
+area():double {query}
// Determines a rectangle’s area.
+perimeter():double {query}
// Determines a rectangle’s perimeter.
+displayStatistics(){query}
// Displays statistics of a rectangle.
1. Implement
the rectangle ADT in C++, based on the pseudocode above. (Note you will
need to create a header file and an implementation file.)
2. Write a
client program to test your ADT. The client should allow a user to
create some rectangles and manipulate them and then print out the
statistics.
More details on the client
there are two
options: interact with the user OR hardcoded
you will get 10
points of extra credit for doing both
All functionality should be
tested, for example:
create a
default object and one with length and width specified (hard coded or
by user input) and display statistics
test get and
set for each changable data value, here is some code you could consider
cout << "Setting length to 12\n";
myrect.setlength(12);
cout << "New length: " << myrect.getlength() << endl;
display
statistics once you have finished the changes
Do the rectangle first then the
colored rectangle
Script should show the hard coded
output and minimal user interaction
You do need to be able to
demonstrate catching bad input (e.g. negative length values) and
handling it
Part 2, Colored
Rectangle
In order to get
a feel for inheritance, we would like to create a subclass of
Rectangle, called ColoredRectangle.
1. Create a C++
header file and implementation file for your new class. Follow
Carrano's subclass ColoredSphere on pages 151 and 152. For example you
will need to write two new constructors and functions to getColor and
setColor.
2. Modify your
client to use your ColoredRectangle class.
Some
notes on compiling classes and clients:
1. I
have modified Sphere.cpp and SphereDemo.cpp so that they should compile
without warnings.
2. Here are two
ways to compile a client:
a. compile it with the class: quick, but can compound debugging:
mmartin@sol:(~/2500) g++ SphereDemo.cpp Sphere.cpp
mmartin@sol:(~/2500) a.out
Radius = 1
Diameter = 2
Circumference = 6.2831
Area = 12.5664
Volume = 4.18879
8.4
b. Compile the class first (using -c flag), with a named output file
(-o flag):
mmartin@sol:(~/2500)
g++ -c Sphere.cpp -o Sphere.o
this creates an
object file called Sphere.o (like a.out)
if it doesn't
compile you can debug your class before confusing the situation with
possible bugs in the client
Now compile your client:
mmartin@sol:(~/2500) g++ SphereDemo.cpp Sphere.o -o SphD.o
mmartin@sol:(~/2500) SphD.o
Radius = 1
Diameter = 2
Circumference = 6.28318
Area = 12.5664
Volume = 4.18879
8.4
This creates
the object file SphD.o.
If you want to
know more about g++, just type man g++ on the command line.
Here is the required
comment format for
significant
functions (courtesy of Dr. Sarraille):
/****************************************
FUNCTION NAME:
INPUT:
OUTPUT:
PRECONDITIONS:
POSTCONDITIONS:
CALLERS:
CALLEES:
*****************************************/
What to Submit
Name your files:
Rectangle.h
Rectangle.cpp
rectangleDemo.cpp
ColoredRectangle.h
ColoredRectangle.cpp
script_rectangle
Turn in your programs
and script to the CS Homework Submission System at https://hopper.csustan.edu/cshomework/
Choose instructor: cs2500mm
Choose course: CS2500
Choose assignment: Lab4