CS 150 - Intro to Programming - Fall 2001
Lab Exercise 13

Topics:

Part 1 - The Rectangle Class

In this part of the lab you will declare and implement a Rectangle class. The Rectangle class will have 2 floating-point data members, one for the width and one for the height. The Rectangle class will have the following member functions:

//constructors
Rectangle();  
Rectangle(double w, double h);
input();        //input values for w and h of rectangle
output();       //output values for w and h of rectangle
getWidth();     //access the width member variable
getHeight();    //access the height member function
area();         //calculate and return the area as a double
perimeter();    //calculate and return the perimeter as a double

Put the class declaration in a file named rectangle.h, and the class function definitions in a file named rectangle.cpp.

Create a main (your Last NameLab13Part1.cpp) that will test the constructors and all member functions. Use "debug messages" in the body for each constructor and member function so you can see when they get called. Of course you will have to create a code warrior project (YourLastNameLab12Part1) and add the above files. See the square class in lab 12 or codeSamples on the web for hints on how to implement the Rectangle class - the book is also a good reference.

Part 2 - Friend Functions

In this part of the lab you will implement a friend function for the Rectangle class. Modify the files from part one - no need to create new ones. Add a friend function "equal" that takes two objects of type Rectangle, and returns a boolean value (true if they are equal, false otherwise). For this implementation, Rectangles are considered equal if they have the same dimensions. For example if R1 has width = 5, and height = 7, R2 has width = 7, and height = 5, and R3 has width = 5, and height = 7. The three rectangles would be equal. Add statements to main to test your implementation of equal.

Part 3 - Operator overloading

In this part of the lab you will overload the insertion and extraction operators for the Rectangle class. Modify the files from part one - again no need to create new ones. Add statements to main to test your implementation of the insertion and extraction operator.


Turn in a folder (YourLastNameLab13) with the following files: