[9:36am] cat rectangle.h // rectangle.h // Delcaration of the rectangle class // Problem 6.12 in D&D // prevent multiple inclusions of header file #ifndef rectangle_H #define rectangle_H // rectangle class definition class rectangle { public: rectangle(float = 1, float = 1); void setrectangle(float, float); void setlength(float); void setwidth(float); float perimeter(); float area(); void printrec(); void printlw(); private: float length; float width; }; #endif ----------------------------------------------------------------------- [9:38am] cat rectangle.cc // rectangle.cc // Member function definitions for rectangle class // Problem 6.12 in D&D #include #include #include "rectangle.h" // constructor function to initialize private data // note default values of 1 (see class definition) rectangle::rectangle(float l, float w) { setrectangle(l, w); } // set functions for rectangle void rectangle::setrectangle(float l, float w) { length = (l > 0 && l <= 20) ? l : 1; width = (w > 0 && w <= 20) ? w : 1; } void rectangle::setlength(float l) { length = (l > 0 && l <= 20) ? l : 1; } void rectangle::setwidth(float w) { width = (w > 0 && w <= 20) ? w : 1; } // rectangle calculations float rectangle::perimeter() { return (2 * length) + (2 * width); } float rectangle::area() { return length * width; } // printing functions // prints a rectangle of approximate integer size void rectangle::printrec() { int l, w; for (l = 1; l <= floor(length + 0.5); l++) { for (w = 1; w <= floor(width + 0.5); w++) cout << "*"; cout << endl; } } // prints length and width of rectangle void rectangle::printlw() { cout << "The rectangle has a length of " << length << " and a width of " << width << endl; } ----------------------------------------------------------------------- [9:39am] cat 6.12-rectangle.cc // Driver program to test rectangle class // problem 6.12 in D&D #include #include "rectangle.h" main () { float l, w; char again; rectangle r, rand1; do { cout << "Input length and width of rectangle: "; cin >> l >> w; r.setrectangle(l, w); rand1.setlength(l-1); rand1.setwidth(w-1); cout << endl; cout << "The rectangle r:" << endl; r.printlw(); r.printrec(); cout << "The perimeter of r is: " << r.perimeter() << endl; cout << "The area of r is: " << r.area() << endl; cout << endl; cout << "The rectangle rand1:" << endl; rand1.printlw(); rand1.printrec(); cout << "The perimeter of rand1 is: " << rand1.perimeter() << endl; cout << "The area of rand1 is: " << rand1.area() << endl; cout << endl; cout << endl; cout << "Setting rectangle rand1 to be the same as original rectangle r" << endl; rand1 = r; cout << "The rectangle rand1 is now:" << endl; rand1.printlw(); rand1.printrec(); cout << "The perimeter of rand1 is: " << rand1.perimeter() << endl; cout << "The area of rand1 is: " << rand1.area() << endl; cout << endl; cout << "Would you like to try again? (y/n) "; cin >> again; } while (again == 'y'); return 0; } ----------------------------------------------------------------------- [9:44am] CC 6.12-rectangle.cc rectangle.cc 6.12-rectangle.cc: rectangle.cc: [9:44am] a.out Input length and width of rectangle: 3 4 The rectangle r: The rectangle has a length of 3 and a width of 4 **** **** **** The perimeter of r is: 14 The area of r is: 12 The rectangle rand1: The rectangle has a length of 2 and a width of 3 *** *** The perimeter of rand1 is: 10 The area of rand1 is: 6 Setting rectangle rand1 to be the same as original rectangle r The rectangle rand1 is now: The rectangle has a length of 3 and a width of 4 **** **** **** The perimeter of rand1 is: 14 The area of rand1 is: 12 Would you like to try again? (y/n) y Input length and width of rectangle: 7 3 The rectangle r: The rectangle has a length of 7 and a width of 3 *** *** *** *** *** *** *** The perimeter of r is: 20 The area of r is: 21 The rectangle rand1: The rectangle has a length of 6 and a width of 2 ** ** ** ** ** ** The perimeter of rand1 is: 16 The area of rand1 is: 12 Setting rectangle rand1 to be the same as original rectangle r The rectangle rand1 is now: The rectangle has a length of 7 and a width of 3 *** *** *** *** *** *** *** The perimeter of rand1 is: 20 The area of rand1 is: 21 Would you like to try again? (y/n) y Input length and width of rectangle: 2 2 The rectangle r: The rectangle has a length of 2 and a width of 2 ** ** The perimeter of r is: 8 The area of r is: 4 The rectangle rand1: The rectangle has a length of 1 and a width of 1 * The perimeter of rand1 is: 4 The area of rand1 is: 1 Setting rectangle rand1 to be the same as original rectangle r The rectangle rand1 is now: The rectangle has a length of 2 and a width of 2 ** ** The perimeter of rand1 is: 8 The area of rand1 is: 4 Would you like to try again? (y/n) n [9:45am]