|
//File: crect04.cpp //Date: 02/27/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); int getWidth(void); CRectangle(void); CRectangle(const int length, const int width); void display(void); bool isEqualTo(const CRectangle &rect2); bool operator ==(const CRectangle &rect2); friend ostream & operator << (ostream &bob, const CRectangle &rec); }; //////////////////////////////////////////////////////////// // prototype //////////////////////////////////////////////////////////// void testConstructor1(void); void testConstructor2(void); void testAssign(void); void testIsEqualTo(void); void testOperatorEqual(void); void testOperatorInsertion(void); //////////////////////////////////////////////////////////// // main //////////////////////////////////////////////////////////// void main(void) { //testConstructor1(); //testConstructor2(); //testAssign(); testIsEqualTo(); testOperatorEqual(); testOperatorInsertion(); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ // 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) { 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) { 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) { 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) { 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) { 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); 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"; } } |