|
// rect001.cpp #include <iostream.h> ///////////////////////////////////////////////////////////////////// // Constant definitions // length and width should be in [MIN_DIM, MAX_DIM] ///////////////////////////////////////////////////////////////////// const int MAX_DIM = 19; const int MIN_DIM = 1; ///////////////////////////////////////////////////////////////////// // CRectangle class definition ///////////////////////////////////////////////////////////////////// class CRectangle { private: int length; int width; public: CRectangle(void); //CRectangle r1; CRectangle(int len, int wid); //CRectangle x1(a, b); int perimeter(void); int area(void); //p01 void setDimension(int len, int wid); //p01 void setLength(int len); //p01 void setWidth(int wid); //p01 int getLength(void); //p01 int getWidth(void); //p01 void getDimension(int &len, int &wid);//p01 void display(void); }; ///////////////////////////////////////////////////////////////////// // getLength function member ///////////////////////////////////////////////////////////////////// int CRectangle::getLength(void) //p01 { return length; } ///////////////////////////////////////////////////////////////////// // testGetLength function ///////////////////////////////////////////////////////////////////// void testGetLength(void) { cout << "testGetLength(void)\n"; cout << "-------------------\n"; CRectangle r1(4,5); r1.display(); cout << r1.getLength() << endl; CRectangle r2(14,51); r2.display(); cout << r2.getLength() << endl; } ///////////////////////////////////////////////////////////////////// // perimeter function member ///////////////////////////////////////////////////////////////////// int CRectangle::perimeter(void) { return (2*(length+width)); } ///////////////////////////////////////////////////////////////////// // Constructor for CRectangle // initializes the lenght and width with len and wid // makes sure that the values are between MIN_DIM and MAX_DIM // sets them to 1 if the the supplied values are out of range ///////////////////////////////////////////////////////////////////// CRectangle::CRectangle(int len, int wid) { if ((len >= MIN_DIM) && (len <= MAX_DIM)) length = len; else length = 1; if ((wid >= MIN_DIM) && (wid <= MAX_DIM)) width = wid; else width = 1; } ///////////////////////////////////////////////////////////////////// // Default constructor // Sets lenght and width to 1 ///////////////////////////////////////////////////////////////////// CRectangle::CRectangle(void) { length = 1; width = 1; } ///////////////////////////////////////////////////////////////////// // Display member functio ///////////////////////////////////////////////////////////////////// void CRectangle::display(void) { cout << "Rectangle (" << length << ", " << width << ')' << endl; } ///////////////////////////////////////////////////////////////////// // Test function for default constructor ///////////////////////////////////////////////////////////////////// void testConstructor1(void) { cout << "testConstructor1(void)\n"; cout << "----------------------\n"; CRectangle r1; r1.display(); CRectangle myRectangles[5]; for (int i=0; i<5; i++) myRectangles[i].display(); } ///////////////////////////////////////////////////////////////////// // Test function for constructor with given values for length & width ///////////////////////////////////////////////////////////////////// void testConstructor2(void) { cout << "testConstructor2(void)\n"; cout << "----------------------\n"; CRectangle r1(4,5); r1.display(); CRectangle r2(14,51); r2.display(); } ///////////////////////////////////////////////////////////////////// // Test function for perimeter ///////////////////////////////////////////////////////////////////// void testPerimeter(void) { cout << "testPerimeter(void)\n"; cout << "-------------------\n"; CRectangle r1(4,5); r1.display(); cout << "Perimeter: " << r1.perimeter() << endl; CRectangle r2(14,51); r2.display(); cout << "Perimeter: " << r2.perimeter() << endl; } ///////////////////////////////////////////////////////////////////// // main function ///////////////////////////////////////////////////////////////////// void main(void) { //testConstructor1(); //testConstructor2(); //testPerimeter(); testGetLength(); } |