|
//File: crect05.cpp //Date: 03/01/2002 //Authors: Us //////////////////////////////////////////////////////////// //Problem: define CRectangle class and some data members, // functions, and operators for it //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// // includes //////////////////////////////////////////////////////////// #include <iostream.h> #include <stdlib.h> #include <time.h> //////////////////////////////////////////////////////////// // constants //////////////////////////////////////////////////////////// const int TEST_COUNT = 5; //////////////////////////////////////////////////////////// // class CRectangle definition //////////////////////////////////////////////////////////// class CRectangle { private: int m_length; int m_width; public: int getLength(void) const; int getWidth(void) const; CRectangle(void); CRectangle(const int length, const int width=1); void display(void) const; bool isEqualTo(const CRectangle &rect2) const; bool operator ==(const CRectangle &rect2) const; friend ostream & operator << (ostream &bob, const CRectangle &rec); CRectangle(char ch); }; //////////////////////////////////////////////////////////// // prototype //////////////////////////////////////////////////////////// void testConstructor1(void); void testConstructor2(void); void testAssign(void); void testIsEqualTo(void); void testOperatorEqual(void); void testOperatorInsertion(void); void testConstructor3(void); //////////////////////////////////////////////////////////// // main //////////////////////////////////////////////////////////// void main(void) { testConstructor1(); testConstructor2(); testAssign(); testIsEqualTo(); testOperatorEqual(); testOperatorInsertion(); testConstructor3(); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ // testConstructor3 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ void testConstructor3(void) { cout << "================\n"; cout << "testConstructor3\n"; cout << "================\n"; for (int i=1; i<=TEST_COUNT; i++) { CRectangle r1('r'); cout << "After CRectangle r1('r');\n"; cout << "Length = " << r1.getLength() << endl; cout << "Width = " << r1.getWidth() << endl; r1.display(); cout << "-----------------------\n"; } } //////////////////////////////////////////////////////////// // constructor CRectangle(char ch) //////////////////////////////////////////////////////////// CRectangle::CRectangle(char ch) { if (('r' == ch) || ('R' == ch)) { this->m_length = rand()%10+1; this->m_width = rand()%10+1; } else { this->m_length = 1; this->m_width = 1; } } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ // testOperatorInsertion /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ void testOperatorInsertion(void) { cout << "=====================\n"; cout << "testOperatorInsertion\n"; cout << "=====================\n"; for (int i=1; i<=TEST_COUNT; i++) { int a = rand()%10+1; int b = rand()%10+1; CRectangle r1(a, b); cout << r1; cout << "-----------------------\n"; } } //////////////////////////////////////////////////////////// // operator << //////////////////////////////////////////////////////////// ostream & operator << (ostream &bob, const CRectangle &rec) { bob << "rect(" << rec.m_length << ", " << rec.m_width << ')' << endl; return bob; } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ // testOperatorEqual /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ void testOperatorEqual(void) { cout << "=================\n"; cout << "testOperatorEqual\n"; cout << "=================\n"; for (int i=1; i<=TEST_COUNT; i++) { int a = rand()%10+1; int b = rand()%10+1; CRectangle r1(a, b); a = rand()%10+1; b = rand()%10+1; CRectangle r2(a, b); r1.display(); r2.display(); if (r1==r2) cout << "are equal\n"; else cout << "are not equal\n"; r1 = r2; cout << "After r1=r2;\n"; r1.display(); r2.display(); if (r1==r2) cout << "are equal\n"; else cout << "are not equal\n"; cout << "-----------------------\n"; } } //////////////////////////////////////////////////////////// // CRectangle::operator == //////////////////////////////////////////////////////////// bool CRectangle::operator ==(const CRectangle &rect2) const { return ((this->m_length==rect2.m_length) &&(this->m_width==rect2.m_width)); }; /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ // testIsEqualTo /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ void testIsEqualTo(void) { cout << "=============\n"; cout << "testIsEqualTo\n"; cout << "=============\n"; for (int i=1; i<=TEST_COUNT; i++) { int a = rand()%10+1; int b = rand()%10+1; CRectangle r1(a, b); a = rand()%10+1; b = rand()%10+1; CRectangle r2(a, b); r1.display(); r2.display(); if (r1.isEqualTo(r2)) cout << "are equal\n"; else cout << "are not equal\n"; r1 = r2; cout << "After r1=r2;\n"; r1.display(); r2.display(); if (r1.isEqualTo(r2)) cout << "are equal\n"; else cout << "are not equal\n"; cout << "-----------------------\n"; } } //////////////////////////////////////////////////////////// // CRectangle::isEqualTo //////////////////////////////////////////////////////////// bool CRectangle::isEqualTo(const CRectangle &rect2) const { return ((this->m_length==rect2.m_length) &&(this->m_width==rect2.m_width)); }; /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ // testAssign /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ // Description: // tests the assign operator for CRectangle class // Example: // if r1 and r2 are two CRectangle objects // if r1=r2; // then r1 should be equal to r2 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ void testAssign(void) { cout << "==========\n"; cout << "testAssign\n"; cout << "==========\n"; for (int i=1; i<=TEST_COUNT; i++) { int a = -10 + rand()%21; int b = -10 + rand()%21; CRectangle r1(a, b); a = -10 + rand()%21; b = -10 + rand()%21; CRectangle r2(a, b); CRectangle r3; r1.display(); r2.display(); r3.display(); r3 = r1 = r2; cout << "After r3=r1=r2;\n"; r1.display(); r2.display(); r3.display(); cout << "-----------------------\n"; } } //////////////////////////////////////////////////////////// // CRectangle::display //////////////////////////////////////////////////////////// // Description: // displays the rectangle object // Example: // if rect is an object with length=3 and width=4 // then rect.display(); will output rect(3, 4) //////////////////////////////////////////////////////////// void CRectangle::display(void) const { cout << "rect(" << (*this).m_length; cout << ", " << this->m_width << ")\n"; } //////////////////////////////////////////////////////////// // CRectangle::getLength //////////////////////////////////////////////////////////// // Description: // returns the length of the rectangle // Example: // if rect is an object with length=3 and width=4 // then a = rect.getLength(); will put 3 in a //////////////////////////////////////////////////////////// int CRectangle::getLength(void) const { return this->m_length; }; //////////////////////////////////////////////////////////// // CRectangle::getWidth //////////////////////////////////////////////////////////// // Description: // returns the width of the rectangle // Example: // if rect is an object with length=3 and width=4 // then a = rect.getWidth(); will put 4 in a //////////////////////////////////////////////////////////// int CRectangle::getWidth(void) const { return this->m_width; }; //////////////////////////////////////////////////////////// // CRectangle::CRectangle //////////////////////////////////////////////////////////// // Description: // constructs a rectangle object with given length and width // makes sure that the dimensions are not negative or zero // if a dimension is not valid it is set to default value 1 // Example: // if a=3 and b=4 then CRectangle rect(a, b); will create // an object rect of type CRectangle with length=3 and width=4 //////////////////////////////////////////////////////////// CRectangle::CRectangle(const int a, const int b) { if (a < 0) this->m_length = -a; else this->m_length = a; if (b < 0) this->m_width = -b; else this->m_width = b; if (0 == a) this->m_length = 1; if (0 == b) this->m_width = 1; } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ // testConstructor2 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ // Description: // tests the constructor CRectangle(a, b) for CRectangle // class by constructing a rectangle object with randomly // chosen values for length and width and displaying the // rectangle object /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ void testConstructor2(void) { cout << "================\n"; cout << "testConstructor2\n"; cout << "================\n"; for (int i=1; i<=TEST_COUNT; i++) { int a = -10 + rand()%21; int b = -10 + rand()%21; //CRectangle r1(a, b); CRectangle r1(a); cout << "a= " << a << ", b= " << b << endl; cout << "After CRectangle r1(a, b);\n"; cout << "Length = " << r1.getLength() << endl; cout << "Width = " << r1.getWidth() << endl; r1.display(); cout << "-----------------------\n"; } } //////////////////////////////////////////////////////////// // CRectangle::CRectangle // Description: // constructs a rectangle object with default values for // its length and width // Example: // CRectangle rect; will create an object // rect of type CRectangle with length=1 and width=1 //////////////////////////////////////////////////////////// CRectangle::CRectangle(void) { this->m_length=1; this->m_width=1; } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ // testConstructor1 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ // Description: // tests the constructor CRectangle for CRectangle // class by constructing a rectangle object with default // values (1) for length and width and displaying the // rectangle object /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ void testConstructor1(void) { cout << "================\n"; cout << "testConstructor1\n"; cout << "================\n"; for (int i=1; i<=TEST_COUNT; i++) { CRectangle r1; cout << "After CRectangle r1;\n"; cout << "Length = " << r1.getLength() << endl; cout << "Width = " << r1.getWidth() << endl; r1.display(); cout << "-----------------------\n"; } } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ // Sample Output /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* ================ testConstructor1 ================ After CRectangle r1; Length = 1 Width = 1 rect(1, 1) ----------------------- After CRectangle r1; Length = 1 Width = 1 rect(1, 1) ----------------------- After CRectangle r1; Length = 1 Width = 1 rect(1, 1) ----------------------- After CRectangle r1; Length = 1 Width = 1 rect(1, 1) ----------------------- After CRectangle r1; Length = 1 Width = 1 rect(1, 1) ----------------------- ================ testConstructor2 ================ a= 10, b= -2 After CRectangle r1(a, b); Length = 10 Width = 1 rect(10, 1) ----------------------- a= 3, b= 9 After CRectangle r1(a, b); Length = 3 Width = 1 rect(3, 1) ----------------------- a= 7, b= 6 After CRectangle r1(a, b); Length = 7 Width = 1 rect(7, 1) ----------------------- a= 2, b= -10 After CRectangle r1(a, b); Length = 2 Width = 1 rect(2, 1) ----------------------- a= 9, b= 10 After CRectangle r1(a, b); Length = 9 Width = 1 rect(9, 1) ----------------------- ========== testAssign ========== rect(4, 5) rect(3, 4) rect(1, 1) After r3=r1=r2; rect(3, 4) rect(3, 4) rect(3, 4) ----------------------- rect(3, 2) rect(3, 4) rect(1, 1) After r3=r1=r2; rect(3, 4) rect(3, 4) rect(3, 4) ----------------------- rect(8, 8) rect(1, 1) rect(1, 1) After r3=r1=r2; rect(1, 1) rect(1, 1) rect(1, 1) ----------------------- rect(7, 4) rect(9, 3) rect(1, 1) After r3=r1=r2; rect(9, 3) rect(9, 3) rect(9, 3) ----------------------- rect(2, 5) rect(10, 2) rect(1, 1) After r3=r1=r2; rect(10, 2) rect(10, 2) rect(10, 2) ----------------------- ============= testIsEqualTo ============= rect(8, 7) rect(2, 9) are not equal After r1=r2; rect(2, 9) rect(2, 9) are equal ----------------------- rect(10, 3) rect(8, 10) are not equal After r1=r2; rect(8, 10) rect(8, 10) are equal ----------------------- rect(6, 5) rect(4, 2) are not equal After r1=r2; rect(4, 2) rect(4, 2) are equal ----------------------- rect(3, 4) rect(4, 5) are not equal After r1=r2; rect(4, 5) rect(4, 5) are equal ----------------------- rect(2, 2) rect(4, 9) are not equal After r1=r2; rect(4, 9) rect(4, 9) are equal ----------------------- ================= testOperatorEqual ================= rect(8, 5) rect(3, 8) are not equal After r1=r2; rect(3, 8) rect(3, 8) are equal ----------------------- rect(8, 10) rect(4, 2) are not equal After r1=r2; rect(4, 2) rect(4, 2) are equal ----------------------- rect(10, 9) rect(7, 6) are not equal After r1=r2; rect(7, 6) rect(7, 6) are equal ----------------------- rect(1, 3) rect(9, 7) are not equal After r1=r2; rect(9, 7) rect(9, 7) are equal ----------------------- rect(1, 3) rect(5, 9) are not equal After r1=r2; rect(5, 9) rect(5, 9) are equal ----------------------- ===================== testOperatorInsertion ===================== rect(7, 6) ----------------------- rect(1, 10) ----------------------- rect(1, 1) ----------------------- rect(7, 2) ----------------------- rect(4, 9) ----------------------- ================ testConstructor3 ================ After CRectangle r1('r'); Length = 10 Width = 4 rect(10, 4) ----------------------- After CRectangle r1('r'); Length = 5 Width = 5 rect(5, 5) ----------------------- After CRectangle r1('r'); Length = 7 Width = 1 rect(7, 1) ----------------------- After CRectangle r1('r'); Length = 7 Width = 7 rect(7, 7) ----------------------- After CRectangle r1('r'); Length = 2 Width = 9 rect(2, 9) ----------------------- Press any key to continue */ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |