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

Recommended Answers

All 16 Replies

What is your question???

Anyway, line 129, where does 'ch' come from?

Thanks for the reply..Basically I am planning to use ch as the character used to draw the rectangle.

So basically what I am after is a method that draws the rectangle on the screen according to its current properties. Example: If Height is 4, Width is 8, and Character is *, i would like to draw the version on the left if the Filled flag is true, or the version on the right if the filled flag is false.

* * * * * * * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * * * * * *

If the height is 1, the result should be a horizontal line using the character; if the width is 1, a vertical line; if both are 1, a single point using the character; if either is 0, draw nothing.

Is there something I am doing wrong..Sorry for all the questions, this is my 6th C++ class....

Here is the example, it didn't seem to work the last time

* * * * * * * *				* * * * * * * *
	* * * * * * * *				*             *
	* * * * * * * *				*             *
	* * * * * * * *				* * * * * * * *

Firstly you need some way of setting your draw character (ch).
Either by having a property to initialise it or use your constructor.
Secondly your rectangle class should have a Draw() method.
Call it from your main ie. myRectangle.Draw()
If you like you can even specify your draw character in your Draw method, passing it as a parameter.
eg. myRectangle.Draw('*')

Work on this.
Cheers Milton

Do you want to draw it in a separate function call? Or you want it to be drawn inside the displayStatics() function? If you want a separate function call, as Milton said, create a new function and call it whatever you want that make sense (in this case draw() would be the best name). Otherwise, you just need to add the drawing part at the end of displayStatics() function.

ch <- a character initiate to an ASCII (could be '*' or user defined)

// draw a filled rectangle (the easiest)
for each i in 0 to height-1
  for each j in 0 to width-1
    display ch

// draw a rectangle border (a little bit more difficult)
for each i in 0 to height-1
  if i is equal to 0 or i is equal to height-1  // display the top and bottom border
    for each j in 0 to width-1
      display ch
  else  // display left and right border
    display ch
    for each j in 1 to width-2
      display a space (' ')
    display ch

I would like to use it a sa seperate function...How would I do it?

I also tried to change it, but there are errors around the for statements about intilizing..

#include <iostream>
using namespace std;

class Rectangle 

{

private:

double height;
double width;
char ch;
bool val;


public:

Rectangle ();              // default constructor 

Rectangle (double heig, double wid);  
 
double getHeight () const;			// This function returns the height of the rectangle

double getWidth () const;			// This function returns the width of the rectangle

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;				//unit rectangle 

Rectangle myRectangle(2.0,2.0);			//rectangle initialized with length = 2.0 & width = 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;

}

So I would like to know where to put the function prototype inside the curly braces of the class.

I would also like to put the for loops inside your draw function, and use the scope resolution opertaor for it:

void Rectangle::draw()
{
etc...
}

Then there must be a way to call it from within int main

- Put the function prototype under line 32.
- Add the function you just wrote above above main().
- The 'ch' I mentioned in my previous post could be a variable that you need to either declare & initialize it yourself, or let user assign it.

// if it is going to be your own initilize
// and the call will be myRectangle.draw();
void Rectangle::draw() {
  char ch = '*';
  ... etc ...
}

// if it is user defined
// and the call will be myRectangle.draw('*');
// or whatever char you can assign to
void Rectangle::draw(char ch) {
  ... etc ...
}

- After you complete the function, put myRectangle.draw(); under line 152.

added a ondraw() function which draws a filled rectangle when flag is true and its boundary only when flag false. You can change the character easily. call it from main().

#include <iostream>
#include<iomanip>
using namespace std;

class Rectangle 

{

private:

double height;
double width;
char ch;
bool val;


public:

Rectangle ();              // default constructor 

Rectangle (double heig, double wid);  
 
double getHeight () const;			// This function returns the height of the rectangle

double getWidth () const;			// This function returns the width of the rectangle

void setHeight (double h);

void setWidth (double w);

double getArea () const;

void displayStatistics () const;

void ondraw();

void offdraw();

};

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;

}

void Rectangle::ondraw()
{
	bool filled=false;
	int num;
	cout<<"Enter 1 for  filled or 2 for unfilled";
	cin>>num;
	if 	(num==1)
		filled=true;
	if(filled)
		{
		for (int i = 1; i <= height; i++)
			{
			for (int j = 1; j <= width; j++)
			{
				cout << "*";
				}
			cout << endl;
			}
		}
	else
		{
		cout<<endl;
		for ( int y = 0; y < width; y++)//this "for" loop will draw the top row
			{
			cout << "*";
			}
		for ( y = 2; y < height; y++) //this "for" loop will draw the rows in the middle
			{
			cout <<"\n"<< "*"<<setw(width-1)<<"*" ; //it consist of a starting character, spacing between the characters (by setw() manipulator), and an ending character
			}
		cout<< endl;
		//this creates a new line for the bottom row
		for ( y = 0; y < width; y++) //this is the bottom row. Just like the top row
			{
			cout  << "*";
			}
	}
	}
int main ()

{

Rectangle unitRectangle;				//unit rectangle 

Rectangle myRectangle(15.0,8.0);			//rectangle initialized with length = 2.0 & width = 2.0

cout <<"Display units as default = 0 :"<<endl;

unitRectangle.displayStatistics ();

cout <<endl<<"Now displaying hard-coded data myRectangle :"<<endl;

myRectangle.displayStatistics ();
// ----------




//----------------

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 ();
myRectangle.ondraw();

//myRectangle.draw();

cout <<endl;

system("PAUSE");
return 0;

}

Thanks, I ran this code, and got an error
rror C2065: 'y' : undeclared identifier..where should the "y" be declared

I am getting an error when trying to compile this
Error C2065: 'y' : undeclared identifier...
where do I need to declare 'Y'

Add int before y in the line(s) of error.

Cheers

Hello, and tks for your advise..I am stuck on 2 more probs...
I need a method that flips the orientation of the rectangle (i.e., it simply switches the value of the Height data member with that of the Width data member). How and where do I do that.

Then I need a method that allows the user to toggle the Filled flag (i.e., if it’s on, it turns it off, and vice versa).
That too i would like to know how and where do I do that.

I have already given you the psuedo code for the drawing part. It is a bit different compared to mtbs, but it is adaptable to suit both filled and empty rectangle. If you follow mtbs code, you need to modify the middle part drawing. You just need a user input to indicate whether or not the rectangle is going to be filled.

I think mtbs gives too much code too you. It seems that you now start to ask for it instead of trying to do something first...

I have already given you the psuedo code for the drawing part. It is a bit different compared to mtbs, but it is adaptable to suit both filled and empty rectangle. If you follow mtbs code, you need to modify the middle part drawing. You just need a user input to indicate whether or not the rectangle is going to be filled.

I think mtbs gives too much code too you. It seems that you now start to ask for it instead of trying to do something first...

Incorporate this to swap values

int temp=height;
height=width;
width=temp;

you can add this at the beginning of ondraw() or elsewhere to toggle bool flags

bool filled=false;
	int num;
	cout<<"Enter 1 for  filled or 2 for unfilled"<<endl;
	cin>>num;
	if 	(num==1)
	{
	filled=true;
	cout<<"Flag is set on"<<endl;
	}
	else if(num==2)
	{
		cout<<"Flag is set off"<<endl;
	}
	cout<<"Toggling"<<endl;
	if(num==1) filled=false;
	if(num==2) filled=true;
		if(filled)
		{
			do something.......
		}
		else
		{
			do something...
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.