1. (20 points) Create a new project named hw4_lastname and create a class named
ID: 3669795 • Letter: 1
Question
1. (20 points) Create a new project named hw4_lastname and create a class named Rectangle that represents a rectangle (1) Your member variables will be height and width. (2) Your member functions will be setHeight (double h): mutator for height variable setWidth (double w): mutator for width variable setDimensions (double h, double w): sets both height and width getArea () : returns the area of your rectangle getPerimeter: returns the perimeter of your rectangle print): prints a rectangle with your dimensions composed of asterisks (3) Create a default constructor that sets the default values of Rectangle class to 0 Make sure that the mutators for height and width only take positive numbers! Test your class with the driver program given below //-- Test driver for class Rectangle #include using namespace std; int main () Rectangle recl, rec2, recl.setHeight (10) recl.setWidth (20) rec2.setDimensions (5, 10) coutExplanation / Answer
class Rectangle
{
public:
double height, width;
void setHeight(double h)
{
height =h;
}
void setWidth(double w)
{
width = w;
}
void setDimensions(double h, double w)
{
height = h;
width=w;
}
double getArea()
{
double a=height*width;
return a;
}
double getPerimeter()
{
double a=2*height + 2*width;
return a;
}
void print()
{
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
cout<<"* ";
}
cout<<endl;
}
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.