#include #include using namespace std; class Rectangle { private: float h, w; float area; void ComputeArea(); public: /*Rectangle()//default constructor { this->h = 1; w = 1;//this->w=1; area = 1;//this->area=1; cout << "Default constructor is called" << endl; }*/ /*Rectangle(float a, float b)//constructor with 2 parameters { this->h = a; w = b; //this->w=b; this->ComputeArea();//this->area=h*w; cout << "constructor with 2 parameters is called" << endl; }*/ Rectangle(float a = 1, float b = 1); Rectangle(const Rectangle& T); //overrides compiler's copy const. ~Rectangle(); void SetDim(float a, float b);//setter void SetHeight(float a);//setter void SetWidth(float a);//setter float GetHeight()const;//getter float GetWidth()const//getter { return w;//return this->w; } float GetArea()const//getter { return area; //return this->area; } void DisplayRectangle()const //memeber function { cout << h << " " << w << " " << area << endl; } friend void DisplayRectangle2(Rectangle& T);//friend function (non-member) float SumArea1(Rectangle T)const //T is passed by value { return this->area + T.area; } float SumArea2(const Rectangle* T) const //T is passed by address { return this->area + T->area; } float SumArea3(const Rectangle & T)const //T is passed by reference { return this->area + T.area; } Rectangle DoubleRectangle1()const // a rectangle is returned by valye { Rectangle T; T.h = this->h * 2; T.w = this->w * 2; T.area = T.h * T.w;//T.ComputeArea(); return T; } Rectangle* DoubleRectangle2()const { Rectangle* T = new Rectangle; T->h = this->h * 2; T->w = this->w * 2; T->area = T->h * T->w;//T.ComputeArea(); return T; } void operator=(const Rectangle& T)//assignment operator { this->h = T.h; this->w = T.w; this->area = T.area; cout << "assignment operator is called" << endl; } /*Rectangle* operator+(const Rectangle& T) { Rectangle* N = new Rectangle; N->h = this->h + T.h; N->w = this->w + T.w; N->ComputeArea(); return N; }*/ /*float operator+(const Rectangle& T) { return this->area + T.area; }*/ friend Rectangle operator+(const Rectangle& T, const Rectangle& R); friend ostream& operator<<(ostream& out, const Rectangle& T); friend istream& operator>>(istream &in, Rectangle &T) { in >> T.h >> T.w; T.ComputeArea(); return in;//return cin } }; ostream& operator<<(ostream& out, const Rectangle& T) { out << "the height of the rectangle is " << T.h << " ,the width is " << T.w << " and the area is " << T.area; return cout;//return out } Rectangle operator+(const Rectangle& T, const Rectangle& R) { Rectangle N; N.h = T.h + R.h; N.w = T.w + R.w; N.ComputeArea(); return N; } Rectangle::~Rectangle() { cout << "Rectangle's destructor is called" << endl; } Rectangle::Rectangle(float a , float b) { this->h = a; w = b; //this->w=b; this->ComputeArea();//this->area=h*w; cout << "constructor with default arguments is called " << endl; } Rectangle::Rectangle( const Rectangle &T) //overrides compiler's copy const. { h = T.h; this->w = T.w; this->area = T.area; cout << " Copy constructor is called" << endl; } void Rectangle::SetDim(float a, float b)//setter { h = a;//this->h=a; this->w = b; //w=b; this->ComputeArea(); } void Rectangle::SetHeight(float a)//setter { this->h = a; ComputeArea();//this->ComputeArea(); } void Rectangle::SetWidth(float a)//setter { w = a;//this->w=a; ComputeArea(); //this->ComputeArea(); } float Rectangle::GetHeight()const //getter { return h;//return this->h; } void Rectangle::ComputeArea() { area = h * w; //this->area = this->h * this->w; } void DisplayRectangle1(const Rectangle &R)//non-member function { cout << R.GetHeight() << " " << R.GetWidth() << " " << R.GetArea() << endl; } void DisplayRectangle2(Rectangle& T)//friend function (non-member) { cout << T.h << " " << T.w << " " << T.area << endl; } void main()//non-member function { Rectangle R4(3, 5); //calls const. with default args R4.DisplayRectangle(); DisplayRectangle1(R4); DisplayRectangle2(R4); Rectangle R5 = R4;//Rectangle R5(R4);//calls copy constructor Rectangle R6(R4);//calls copy constructor //Rectangle* R7 = new Rectangle(3, 9); //cout << R5.GetArea() << endl; //cout << R6.GetArea() << endl; //Rectangle R8= R4.DoubleRectangle1(); Rectangle* R9; //R9 = R4.DoubleRectangle2(); Rectangle R10(10, 4); //R5 = R10;//calls assignment operator //cout << R5.GetArea() << endl; Rectangle R11; R11= R10.DoubleRectangle1(); R11 = R4 + R10; R11.DisplayRectangle(); //cout << R11 << endl << R4 << endl; int g; cin >> R11 >> R4; cout << R11 << endl << R4 << endl; Rectangle* M = new Rectangle(1, 3); cin >> *M; cout << *M << endl; delete M; //cout << R4.SumArea1(R5) << endl; //cout << R4.SumArea2(&R5) << endl; //cout << R4.SumArea3(R5) << endl; /*Rectangle x, y; Rectangle R1; Rectangle* p = &R1;//no const. p->SetDim(9, 3); cout << p->GetArea() << endl; Rectangle R2(3, 4); Rectangle *R3= new Rectangle;//calls default constructor Rectangle A1[3] = { {3,4},{10,8} }; Rectangle* B1 = new Rectangle[3]; float a, b; for (int i = 0; i < 3; i++) { cin >> a >> b; A1[i].SetDim(a, b); A1[i].DisplayRectangle(); cout << endl; } for (int i = 0; i < 3; i++) { cin >> a >> b; B1[i].SetDim(a, b); B1[i].DisplayRectangle(); cout << endl; } delete[]B1; Rectangle *R4= new Rectangle(3,10);//calls constructor with 2 args Rectangle* R5 = new Rectangle();//calls default constructor cout << R3->GetArea() << endl; R3->SetDim(10, 8); cout << R3->GetArea() << endl; cout << x.GetArea() << " " << R2.GetArea() << endl; x.SetDim(10, 20); y.SetDim(3, 4); x.DisplayRectangle(); y.DisplayRectangle(); cout << "The area of both rectangles is: "; cout << x.GetArea() + y.GetArea() << endl; delete R3; delete R4; delete R5;*/ }