the rectangle class do not use strings Define a class called Rectangle. The clas
ID: 3568693 • Letter: T
Question
the rectangle class
do not use strings
Define a class called Rectangle. The class contains:
- private member variables height and width. These both have the data type float.
- a default constructor that creates a rectangle with width of 1 and height of 1
- a constructor that takes an argument for width, but gives, as a default value, a height of 2.
- three mutator functions
- set height
- set width
- set both height and width
- three accessor functions
- get height
- get width
- get both height and width
- an output function that outputs the height and the weight
- a function getArea() that returns the area of the rectangle
- a function getPerimeter() that returns the perimeter of the rectangle
You must determine the appropriate public/private status of all of the member functions.
Write a program for the class Rectangle. The program has the following functions
getValues() - this function asks the user for height and width values. It returns no value. These values must be available to the class Rectangle's constructors and set functions.
printRectangle() - this function outputs to stdout (the terminal/monitor) the area and perimeter of the rectangle as:
the area of your rectangle is: (rectangle's area)
the perimeter of your rectangle is: (rectangle's perimeter)
It must have use values given by the class Rectangle's getArea() and getPerimeter() functions.
In your program, get the height and width values from your user. Create two different rectangles, one that uses these values, another that uses the user given value for width, but the default value of 2 for the rectangle's height.
Alter the value of both rectangles to the user given height.
Output the area and perimeter for both rectangles.
Explanation / Answer
#include<iostream>
using namespace std;
class Rectangle
{
private:
float height;
float width;
public:
Rectangle();
Rectangle(float Width,float Height);
float getHeight();
void setHeight(float height);
float getWidth();
void setWidth(float width);
void setWidthHeight(float Width,float Height);
void displayWidthHeight();
float getArea();
float getPerimeter();
};
Rectangle::Rectangle()
{
width=1;
height=1;
};
Rectangle::Rectangle(float Width,float Height=2.0)
{
width=Width;
height=Height;
};
float Rectangle::getHeight(){
return height;
};
void Rectangle::setHeight(float height) {
this->height = height;
};
float Rectangle::getWidth(){
return width;
};
void Rectangle::setWidth(float width) {
this->width = width;
};
void Rectangle::setWidthHeight(float Width,float Height)
{
width=Width;
height=Height;
};
void Rectangle::displayWidthHeight()
{
cout<<"Width:"<<width<<" Height:"<<height<<endl;
}
float Rectangle::getArea()
{
return width*height;
}
float Rectangle::getPerimeter()
{
return 2*(width+height);
}
float inputWidth;
float inputHeight;
void getValues()
{
cout<<"Enter width:"<<endl;
cin>>inputWidth;
cout<<"Enter height:"<<endl;
cin>>inputHeight;
}
void printRectangle(Rectangle r)
{
cout<<" the area of your rectangle is:"<<r.getArea()<<endl;
cout<<" the perimeter of your rectangle is:"<<r.getPerimeter()<<endl;
}
int main()
{
getValues();
Rectangle rect1(inputWidth,inputHeight);
rect1.displayWidthHeight();
Rectangle rect2(inputWidth);
rect2.displayWidthHeight();
cout<<"Printing details for rectangle 1"<<endl;
printRectangle(rect1);
cout<<"Printing details for rectangle 2"<<endl;
printRectangle(rect2);
rect1.setHeight(inputHeight);
rect2.setHeight(inputHeight);
cout<<"Heights changed for both rectangles"<<endl;
rect1.displayWidthHeight();
cout<<"Printing details for rectangle 1"<<endl;
printRectangle(rect1);
cout<<"Printing details for rectangle 2"<<endl;
rect2.displayWidthHeight();
printRectangle(rect2);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.