Hello Guys
I am having problems with an assignment. I need to create a class to make a rectangle. Then I have to - Declare an object of the class (rect1) using the default constructor.
Now prompt the user for a Width, Height and Character. Store the results in temporary variables, then set the properties of rect1 by passing these values to its “Set” function.
Draw rect1 using the new settings.
This is where I am stuck at, here is the code, and hopefully I am doing this right.
The data members (private) should be:
Data Members (all private):
Height of the rectangle.
Width of the rectangle.
Character used to draw the rectangle.
Flag determining whether the rectangle is Filled (or unfilled).
#include <iostream>
using namespace std;
class Rectangle
{
private:
double height;
double width;
char ch;
bool val;
public:
Rectangle ();
Rectangle (double heig, double wid);
double getHeight () const;
double getWidth () const;
void setHeight (double h);
void setWidth (double w);
double getArea () const;
void displayStatistics () const;
};
Rectangle::Rectangle ()
{
height = 0.0;
width = 0.0;
}
Rectangle::Rectangle (double heig, double wid)
{
height = heig;
width = wid;
}
void Rectangle::setHeight (double l)
{
height = l;
}
void Rectangle::setWidth (double w)
{
width = w;
}
double Rectangle::getHeight () const
{
return height;
}
double Rectangle::getWidth () const
{
return width;
}
double Rectangle::getArea () const
{
return (height * width);
}
void Rectangle::displayStatistics () const
{
cout <<endl <<"Height = " <<getHeight ()<<" unit (s)";
cout <<endl <<"Width = " <<getWidth () <<" unit (s)";
cout <<endl <<"Area = " <<getArea () <<" square unit (s)";
cout <<endl;
}
int main ()
{
Rectangle unitRectangle;
Rectangle myRectangle(2.0,2.0);
cout <<"Display units as default = 0 :"<<endl;
unitRectangle.displayStatistics ();
cout <<endl<<"Now displaying hard-coded data myRectangle :"<<endl;
myRectangle.displayStatistics ();
for(int y=0 ; y <height ; y++)
{
for(int x=0 ; x <width ; x++)
cout << ch;
cout << endl;
}
double num1, num2;
cout << endl;
cout << "Enter the height:";
cin >> num1;
cout << "Enter the Width:";
cin >> num2;
myRectangle.setHeight(num1);
myRectangle.setWidth (num2);
cout <<endl <<"The user size of the rectangle is " << endl;
myRectangle.displayStatistics ();
cout <<endl;
system("PAUSE");
return 0;
}
Thanks