Question #251038

Write a main function that declares two rectangle object as follows:
The first name rect1 is created using the overridden default constructor.
The second named rect2 has a height of 5 and width of 10 assigned by the parameterized constructor.
Use setWidth and setHeight to assign rect1 a width of 100 and height of 25 respectively.
Call computeArea and computePerimeter for each rectangle object.
Call getArea and getPerimeter and display the area and perimeter of each rectangle.

Expert's answer

#include<iostream>
using namespace std;
class Rectangle{
	private:
		int width, height, area, perimeter;
	public:
		Rectangle(){
			width = 0;
			height = 0;
			area = 0;
			perimeter= 0;
		}
		Rectangle(int w, int h){
			width = w;
			height = h;
			area = 0;
			perimeter = 0;
		}
		
		void setWidth(int w){
			width = w;
		}
		void setHeight(int h){
			height = h;
		}
		
		
		int getArea(){
			return area;
		}
		
		int getPerimeter(){
			return perimeter;
		}
		void computeArea(){
			area = height * width;
		}
		void computePerimeter(){
		perimeter = 2 * (width + height);


		}
		
		
};


int main(){
	Rectangle rect1;
	Rectangle rect2(5,10);
	
	rect1.setWidth(100);
	rect1.setHeight(25);
	
    rect1.computeArea();
    rect1.computePerimeter();
    
    rect2.computeArea();
    rect2.computePerimeter();
    cout<<"Rectangle rect1\n";
    cout<<"The area of the rectangle:  "<<rect1.getArea()<<endl;
    cout<<"The perimeter of the rectangle:  "<<rect1.getPerimeter()<<endl;
    
    cout<<"Rectangle rect2\n";
    cout<<"The area of the rectangle:  "<<rect2.getArea()<<endl;
    cout<<"The perimeter of the rectangle:  "<<rect2.getPerimeter()<<endl;
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS