Lab
Exercise #6
Put
your name, the compiler and operating system used, and Lab Exercise #6
in a comment at the
top of your program. Email your source code.
Modify example, Program
13-1 from the textbook, by adding a data member, color (use
type string)
and 3 member functions, setColor(), getColor() and getPerimeter()
to the class. Use the
main() function below to test your code.
int main() { Rectangle box; // Define an instance of the Rectangle class double rectWidth; // Local variable for width double rectLength; // Local variable for length string rectColor;
// Get the rectangle's width and length from the user. cout << "This program will calculate the area of a\n"; cout << "rectangle. What is the width? "; cin >> rectWidth; cout << "What is the length? "; cin >> rectLength; cout << "What is the color? "; cin >> rectColor;
// Store the width and length of the rectangle // in the box object. box.setWidth(rectWidth); box.setLength(rectLength); box.setColor(rectColor);
// Display the rectangle's data. cout << "Here is the rectangle's data:\n"; cout << "Width: " << box.getWidth() << endl; cout << "Length: " << box.getLength() << endl; cout << "Color: " << box.getColor() << endl; cout << "Area: " << box.getArea() << endl; cout << "Perimeter: " << box.getPerimeter() << endl; return 0; }
|
Sample output
This program will calculate the area of a rectangle. What is the width? 4 What is the length? 5 What is the color? red Here is the rectangle's data: Width: 4 Length: 5 Color: red Area: 20 Perimeter: 18 |
|