Monday 29 August 2011

You are required to write a program of class rectangle named CRectangle which should draw two rectangles by using iTop, iRight, iBot, and iLeft. Then program should calculate the area of both. Number of rectangles should be displayed to user by using static variable counter which would keep track of increment or decrement of rectangles. Dynamically Create another rectangle by using copy constructor and increment in counter should be displayed on screen. Decrement in static variable counter (after de-allocating memory which was dynamically allocated) should be displayed again on screen.

Detailed Description:

Sample Output

Area of rectangle 1 is 6400

Area of rectangle 2 is  5600

The number of rectangles is 2          

After using copy constructor, total number of rectangles is 3

The number of rectangles is 2


1. The class should have 4 members: iTop, iRight, iBot, and iLeft, all of type int.

2. Area of both rectangles should be calculated by using iTop, iRight, iBot, and iLeft.

3. It should have a default constructor and a copy constructor.

4. It should have a destructor.

5. It should contain all required getters and setters.

6. It should contain a static member, iRefCount, which keeps track of the number of CRectangle objects. Increment it in the constructor, and decrement it in the destructor.

Hint:

CRectangle would implement rectangles by using left, right, top and bottom and calculate their areas to compare.

Answer
using namespace std;
#include <iostream>
#include <conio.h>
class CRectangle {
    private:

 int   iLeft;                                                                    // Class members
 int   iTop;       
 int   iRight;     
 int   iBottom;    
 static int iRefCount;                                                        // Static variables   
public:

 CRectangle::CRectangle (int l = 0, int t = 0, int r = 1, int b = 1);        // Constructors
 CRectangle::CRectangle (CRectangle& rect);
 CRectangle::~CRectangle ();


 int GetLeft() // Getters
    {
        return iLeft;
            }                
 int GetTop() 
            {
        return iTop;
            }
 int GetRight() 
            {
        return iRight;
            }
 int GetBottom()
            {
       return iBottom;
            }

 void SetLeft(int iValue)                              // Setters
            {
        iLeft = iValue;
            }              
           
    void SetTop(int iValue) 
            {
        iTop = iValue;
            }
       
 void SetRight(int iValue) 
            {
       iRight = iValue;
            }
           
 void SetBottom(int iValue) 
            {
       iBottom = iValue;
            }
 int Area() 
         {
          return((iRight - iLeft) * (iBottom - iTop));
          }

 static int GetRefCount()
         {
          return iRefCount;
         }
};
int CRectangle::iRefCount = 0;                                  // Static variables
CRectangle::CRectangle (int l ,int t , int r , int b)         // Default constructor
{
 iLeft = l;
 iTop = t;
 iRight = r;
 iBottom = b;

 iRefCount++;
}
CRectangle::CRectangle (CRectangle& rect)                        // Copy constructor.
{
 iLeft = rect.iLeft;
 iTop = rect.iTop;
 iRight = rect.iRight;
 iBottom = rect.iBottom;
 iRefCount++;
}
CRectangle::~CRectangle ()
{
 iRefCount--;
}
int main()
{
 CRectangle rect1(80, 120, 160, 200);
 CRectangle rect2(100, 140, 180, 210);
 int iArea = rect1.Area();                           // Calc area and output.
 cout << "Area of rectangle 1 is " << iArea << endl;
 iArea = rect2.Area();
 cout << "Area of rectangle 2 is " << iArea << endl;


 cout << "The number of rectangles is " << rect1.GetRefCount() << endl;    // Output total number of rectangles.


 CRectangle* pRectangle = new CRectangle();                               // Dynamically create a new instance of CRectangle.

 cout << "After using Copy constructor, total number of rectangles is " << rect1.GetRefCount() << endl;   // Output total number of rectangles.
 
 delete pRectangle;                                                       // Delete the rectangle object.


 cout << "The number of rectangles is " << rect1.GetRefCount() << endl;  // Output total number of rectangles.
 pRectangle = NULL;
    getch();
 return 0;
}


No comments:

Post a Comment