Language Bar

Friday, January 28, 2011

CS201 Assignment 5 SOLUTION


Assignment No. 5
Semester: Fall 2010
CS201: Introduction to Programming
Total Marks: 20

Due Date: Jan 31st  ,2011

Instructions:
Please read the following instructions carefully before submitting your assignment:
It should be clear that your assignment will not get any credit if:

§         The assignment is submitted after due date.
§         The submitted assignment does not open or file is corrupt.

All types of plagiarism are strictly prohibited.

Note: You have to upload only .cpp file. Assignment in any other format (extension) will not be accepted. If you will submit code any other file format like .doc or .txt etc. you will get zero marks.

Objective
The objective of this assignment is to provide hands on experience of using

§         User defined manipulators
§         Static member
§         Copy Constructor

Guidelines
§         Code should be properly aligned and well commented.
§         Follow C/C++ rules while writing variables names, function names etc.
§         Use only Dev-C++ IDE for this assignment.

Assignment


Problem Statement:

  • 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.

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
 

Detailed Description:

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.


GOOD LUCK

Deadline:
Your Assignment solution must be submitted on or before Jan 31st, 2011.






---------------------------------------------------------------------------------------------------
Solution


/*YASIR JAVAED MC090200982*/

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;

class CRectangle
{
      private:
              // Rectangle class is CRectangle. it is private nobody can make rectangle without its attributes.
              CRectangle()
              {
              }
      public:
              // Data members are
              int iBot;
              int iTop;
              int iLeft;
              int iRight;

              // Static variable to ensure rectangles calculate.
              static int rectCount;

              // Constructor of the class CRectangle.
              CRectangle(int bottom,int top,int left,int right)
              {
                          iBot = bottom;
                          iTop = top;
                          iLeft = left;
                          iRight = right;
                         
                          CRectangle::rectCount++;
              }

              // Dynamic Copy constructor of the class.
              CRectangle(const CRectangle& lhs)
              {
                          iBot = lhs.iBot;
                          iTop = lhs.iTop;
                          iLeft = lhs.iLeft;
                          iRight = lhs.iRight;
                         
                          CRectangle::rectCount++;
              }
             
              // Destructor of the class.
              ~CRectangle()
              {
                          CRectangle::rectCount--;
              }

              // Function to compute and obtain area of a rectangle.
              long Area()
              {
                   return(long)( abs(iTop-iBot) * abs(iLeft-iRight) );
              }
};


int CRectangle::rectCount=0;


int main ()
{
 
    CRectangle rect1 = CRectangle(0,80,0,80);
    CRectangle rect2 = CRectangle(0,70,0,80);
   
    cout<<"Area of rectangle 1 is "<<rect1. Area()<<endl;
    cout<<"\nArea of rectangle 2 is "<<rect2. Area()<<endl;
    cout<<"\nThe number of rectangles is "<<CRectangle::rectCount<<endl;

    {
         CRectangle rectCopied = CRectangle(rect1);
         cout<<"\nAfter using copy constructor, total number of rectangles is "<<CRectangle::rectCount<<endl;
    }

         
    cout<<"\nThe number of rectangles is "<<CRectangle::rectCount<<endl;

    getch();
    return 0;
}
 



                                  

2 comments:

  1. hey there and thank you for your info – I have certainly picked up something new from right here.
    I did however expertise some technical issues using this web site, since I
    experienced to reload the site many times previous to I could get it to load
    properly. I had been wondering if your hosting is OK?
    Not that I am complaining, but slow loading instances times will very frequently affect your placement in google and can damage your high-quality score if ads and marketing with Adwords.
    Well I'm adding this RSS to my email and could look out for a lot more of your respective fascinating content. Make sure you update this again soon.

    Feel free to surf to my web page :: cellulite treatment

    ReplyDelete
  2. I have read so many posts on the topic of the blogger lovers
    except this post is genuinely a good article, keep it up.



    Here is my web site: best cellulite treatment

    ReplyDelete

Amazon

Disqus

comments powered by Disqus

CONTACT US FORM