|
// Prog02.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(int len, int wid); //CRectangle x1(a, b); int perimeter(void); int area(void); void setDimension(int len, int wid); // x1.setDimension(a,b); void setLength(int len); void setWidth(int wid); int getLength(void); int getWidth(void); void getDimension(int &len, int &wid); // x1.getDimension(p, q); void display(void); }; ///////////////////////////////////////////////////////////////////// // 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; } ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// CRectangle::CRectangle(void) { length = 1; width = 1; } ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// void CRectangle::display(void) { cout << "Rectangle (" << length << ", " << width << ')' << endl; } ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// 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(); } ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// void testConstructor2(void) { cout << "testConstructor2(void)\n"; cout << "----------------------\n"; CRectangle r1(4,5); r1.display(); CRectangle r2(14,51); r2.display(); } ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// 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; } ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// void main(void) { testPerimeter(); } |