CRect06
Home ] Up ]

 

//File:    crect06.cpp
//Date:    03/04/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);
    bool isSquare(void) const;
    friend bool operator != (CRectangle r1, CRectangle r2);
  };


////////////////////////////////////////////////////////////
// prototype
////////////////////////////////////////////////////////////
void testConstructor1(void);
void testConstructor2(void);
void testAssign(void);
void testIsEqualTo(void);
void testOperatorEqual(void);
void testOperatorInsertion(void);
void testConstructor3(void);
void testIsSquare(void);
void testOperatorNotEqual(void);


////////////////////////////////////////////////////////////
// main
////////////////////////////////////////////////////////////
void main(void)
  {
  //testConstructor1();
  //testConstructor2();
  //testAssign();
  //testIsEqualTo();
  //testOperatorEqual();
  //testOperatorInsertion();
  //testConstructor3();
  //testIsSquare();
  //testOperatorNotEqual();

  CRectangle a[10];
  for (int i=0; i<=9; i++)
    //a[i].display();
    cout << a[i];

  CRectangle *p;
  p = new CRectangle;
  cout << p << endl;
  cout << *p;
  (*p).display();
  p->display();
  }


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
// testOperatorNotEqual
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void testOperatorNotEqual(void)
  {
  cout << "=================\n";
  cout << "testOperatorNotEqual\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 NOT equal\n";
      else
        cout << "are equal\n";

    r1 = r2;
    cout << "After r1=r2;\n";
    r1.display();
    r2.display();
    if (r1!=r2)
        cout << "are NOT equal\n";
      else
        cout << "are equal\n";

    cout << "-----------------------\n";
    }
  }


////////////////////////////////////////////////////////////
// operator !=
////////////////////////////////////////////////////////////
bool operator != (CRectangle r1, CRectangle r2)
  {
  return ((r1.m_length != r2.m_length)||(r1.m_width != r2.m_width));
  }


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
// testIsSquare
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void testIsSquare(void)
  {
  cout << "============\n";
  cout << "testIsSquare\n";
  cout << "============\n";

  for (int i=1; i<=TEST_COUNT; i++)
    {
    CRectangle r1('r');

    cout << "After CRectangle r1('r');\n";
   
    r1.display();

    if (r1.isSquare())
        cout << "is square\n";
      else
        cout << "is NOT square\n";

    cout << "-----------------------\n";
    }
  }


////////////////////////////////////////////////////////////
/*
Name: 
  isSquare
 
Description:
  a member function to check if a rectangle object is 
  square or not
 
Examples:
  rect(4, 4) is square
  rect(4, 5) is not square
 
Algorithm:
  if m_length is equal to m_width then return true 
  else return false
 
Class member/friend of:
  member of class CRectangle
 
Prototype:
  bool isSquare(void) const;
 
Returns:
  boolean value
 
Parameters:
  none
 
Shared external names:
  none
 
Shared member names:
  m_length, m_width
 
Local variables used:
  none
*/
////////////////////////////////////////////////////////////
//Source code
////////////////////////////////////////////////////////////
bool CRectangle::isSquare(void) const
  {
  return (this->m_length == this->m_width);
  }


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
// 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";
    }
  }