Answer to Question #250519 in C++ for Love

Question #250519
Creat a class named Rectangle with integer instance variables named width, height, area and perimeter. Override the default constructor and use it to set these variables to zero. Also include another constructor that accepts perimeters for the width and height instance variables and initializes the instance variables area and perimeter to zero.
Include the following accessor and mutator methods;
I) setWidth which accepts a parameter that can be used to assign the width instance variable.
2) setHeight which accepts a parameter that can be used to assign the height instance variable.
3) getArea that returns the value of area instance variable.
4) getPerimeter that returns the value of perimeter instance variable.

Also include following custom methods:
1) computeArea which computes a rectangle's area but doesn't return a value
2) computePerimeter Which computes a rectangle's perimeter and also doesn't return a value.
1
Expert's answer
2021-10-13T00:29:40-0400
#include<iostream>
using namespace std;
class Rectangle{
	private:
		int width, height, area, perimeter;
	public:
		Rectange(){
			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 rec(8,8);
    rec.computeArea();
    rec.computePerimeter();
    cout<<"The area of the rectangle:  "<<rec.getArea()<<endl;
    cout<<"The perimeter of the rectangle:  "<<rec.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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS