//File: crect02.cpp
//Date: 02/15/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 TRectangle 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);
};
////////////////////////////////////////////////////////////
// prototype
////////////////////////////////////////////////////////////
void testConstructor1(void);
void testConstructor2(void);
////////////////////////////////////////////////////////////
// main
////////////////////////////////////////////////////////////
void main(void)
{
testConstructor1();
testConstructor2();
}
////////////////////////////////////////////////////////////
// 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";
}
}
|