Hi there. I am trying to create a class rectangle. It should have data members length and width of type float (which should default to 1). The class should have member functions that calc. area() and perimeter() and also separate get() and set() functions for length and width. These should require the user to enter valid length and width between 0 and 20.0 (use a do while loop). Then I have to add a draw function, but I'm not even there yet. Can someone look at my code and give me a general idea of where I am going? It all looks like a big mess to me and I just keep making it worse now! Thanks for taking the time to help me.

#include <iostream>
using namespace std;
class Rectangle // Class Rectangle
{
public:
Rectangle(float=1, float=1); // Constructor
float getLength(){return length;}
void setLength(float L);
float getWidth(){return width;}
void setWidth(float W);
double perimeter(void){return (length*2 + width*2);} // Perimeter function
double area(void) {return length*width);} // Area funcion

private:
float length;
float width;
}; // End of class Rectangle

Rectangle::Rectangle(float L, float W) // Scope function
{
length=L;
width=W;
}
double Rectangle:perimeter(float L, float W)
{



}
double Rectangle::area(float L, float W)
{


}
void Rectangle::setWidth(float W)
{
if ((W < 0.0) || (W > 20.0))   
     {  
        width = 1.0;
     }  
     else 
        {  
          width = W;  
        }  
   return;  


}
void Rectangle::setLength(float L)
{
do
cout << "Please enter a valid length: " <<endl;
cin >> L;
	while ((L < 0.0) || (L > 20.0))   
     
     else 
        {  
          Length = L;  
        }  
   return;  


}
void Rectangle::get(float L, float W)
{



}
int main() // main()
{


return 0;
} // End of main()

Recommended Answers

All 25 Replies

Since we aren't in you head, it's really hard for us to know where you're going. The only thing I can really tell you is your set functions should not input values. They should only set the private var with the value passed into the set-functions. Do the input in the main code or, better yet, a function that main calls.

I don't need you to be in my head. My goal is written at the top of my first post. I just need to follow that, but I am making some mistakes. I've made some changes below. Anyone have feedback?

#include <iostream>
using namespace std;
class Rectangle // Class Rectangle
{
public:
	Rectangle(float L, float W); // Constructor
	float getLength(){return length;}
	void setLength(float L);
	float getWidth(){return width;}
	void setWidth(float W);
	double perimeter(void){return (length*2 + width*2);} // Perimeter function
	double area(void) {return (length*width);} // Area funcion
	
private:
	float L, W, length, width;
		}; // End of class Rectangle

Rectangle::Rectangle(float L, float W) // Scope function
{
length=L;
width=W;
}

void Rectangle::setWidth(float W)//I'd like to change this to a do/ while loop??
{
if ((W < 0.0) || (W > 20.0))   
     {  
        width = 1.0;
     }  
     else 
        {  
          width = W;  
        }  
   return;  


}
float getWidth()
{
	return W;//Is this right?
}

void Rectangle::setLength(float L)//same here-do/while loop?
{
if ((L < 0.0) || (L > 20.0))   
     {  
        length = 1.0;
     }  
     else 
        {  
          length = L;  
        }  
   return;  
}
float getLength()
{
	return L;
}

void Rectangle::get(float L, float W)
{



}
double Rectangle:perimeter(float L, float W)
{
perimeter = (2*L) + (2*W);
cout << "The perimeter of the rectangle is: " << perimeter << endl;
}

double Rectangle::area(float L, float W)
{
area = L*W;
cout << "The area of the rectangle is :  " << area << endl;
}

int main() // main() I'm sure something is wrong here. 
// I understand the main function, but when I use classes I get confused...
{
rectangle MyRectangle;
cout << "Please enter the length of the rectangle: " << endl;
cin >> MyRectangle.getlength >> endl;
cout << "Please enter the width of the rectangle: " << endl;
cin >> MyRectangle.getwidth >> endl;


return 0;
} // End of main()

BTW, here are the errors I'm getting:
1>------ Build started: Project: more_practice, Configuration: Debug Win32 ------
1>Compiling...
1>more.cpp
1>c:\users\laura\desktop\more_practice\more_practice\more.cpp(40) : error C2065: 'W' : undeclared identifier
1>c:\users\laura\desktop\more_practice\more_practice\more.cpp(57) : error C2065: 'L' : undeclared identifier
1>c:\users\laura\desktop\more_practice\more_practice\more.cpp(60) : error C2039: 'get' : is not a member of 'Rectangle'
1> c:\users\laura\desktop\more_practice\more_practice\more.cpp(4) : see declaration of 'Rectangle'
1>c:\users\laura\desktop\more_practice\more_practice\more.cpp(66) : error C2470: 'Rectangle' : looks like a function definition, but there is no parameter list; skipping apparent body
1>c:\users\laura\desktop\more_practice\more_practice\more.cpp(73) : error C2511: 'double Rectangle::area(float,float)' : overloaded member function not found in 'Rectangle'
1> c:\users\laura\desktop\more_practice\more_practice\more.cpp(4) : see declaration of 'Rectangle'
1>c:\users\laura\desktop\more_practice\more_practice\more.cpp(81) : error C2065: 'rectangle' : undeclared identifier
1>c:\users\laura\desktop\more_practice\more_practice\more.cpp(81) : error C2146: syntax error : missing ';' before identifier 'MyRectangle'
1>c:\users\laura\desktop\more_practice\more_practice\more.cpp(81) : error C2065: 'MyRectangle' : undeclared identifier
1>c:\users\laura\desktop\more_practice\more_practice\more.cpp(83) : error C2065: 'MyRectangle' : undeclared identifier
1>c:\users\laura\desktop\more_practice\more_practice\more.cpp(83) : error C2228: left of '.getlength' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\laura\desktop\more_practice\more_practice\more.cpp(85) : error C2065: 'MyRectangle' : undeclared identifier
1>c:\users\laura\desktop\more_practice\more_practice\more.cpp(85) : error C2228: left of '.getwidth' must have class/struct/union
1> type is ''unknown-type''
1>Build log was saved at "file://c:\Users\Laura\Desktop\more_practice\more_practice\Debug\BuildLog.htm"
1>more_practice - 12 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

You've posted straightforward C++ code but the Debug Win32 bit implies you are compiling it as a windows project instead of a console project.

You forgot to prefix the functions with your classname... right now the compiler sees getWidth() and getLength() as functions declared/defined outside of your class that are independant of your class, so the variables W and L are searched for in global scope and not found--

//...

// prefix getWidth with classname
float getWidth()
{
	return W;//Is this right?
}

//...
// prefix getLength with class name
float getLength()
{
	return L;
}

//...

-- And your function --

void Rectangle::get(float L, float W)
{



}

-- wasn't declared in your class. This is easily remedied by declaring it in your class--

class Rectangle{

 public:
   //...
   void get(float L, float W);
   //...

}

--

// You only have on colon specified between Rectangle and perimeter versus ::
double Rectangle:perimeter(float L, float W)

-- check the comment above area --

class Rectangle // Class Rectangle
{
public:
	Rectangle(float L, float W); // Constructor
	float getLength(){return length;}
	void setLength(float L);
	float getWidth(){return width;}
	void setWidth(float W);
	double perimeter(void){return (length*2 + width*2);} // Perimeter function

        // notice area no longer accepts void but instead desired parameter
	double area(float length, float width) {return (length*width);} // Area funcion

-- Be sure to spell out Rectangle exactly the way your class has the name spelled out --

int main() // main() I'm sure something is wrong here. 
// I understand the main function, but when I use classes I get confused...
{
Rectangle MyRectangle;

-- if you want to shorthand spelling Rectangle you can do so with a typedef, but for now be sure to check your spelling and the compiler's warnings/errors thoroughly.

The other errors should be removed after fixing the Rectangle declaration. If you have any more errors or questions please ask.

EDIT: A remark. Why is it that you have two different types of widths and lengths declared in your class?--

private:
	float L, W, length, width;

-- your setters set the length and width portion, but your getters get the L and W portions. Why not just use 'length' and 'width' only? The whole point of a variable is to be used as a reference value for further calculating yet you have 2 different values for the same purpose?

That was really helpful. I'm going to go over it again to make sure I understand. Do I not need L,W, length and width? I changed some but I think I need the others...ugh. Here is what I have now and the errors.

#include <iostream>
using namespace std;
class Rectangle // Class Rectangle
{
public:
	Rectangle(float L, float W); // Constructor
	float getLength(){return L;}
	void setLength(float L);
	float getWidth(){return W;}
	void setWidth(float W);
	double perimeter(void){return (length*2 + width*2);} // Perimeter function
	double area (void){return (length*width);} // Area funcion
	void get(float L, float W);
	
private:
	float L, W, length, width;
		}; // End of class Rectangle

Rectangle::Rectangle(float L, float W) // Scope function
{
length=L;
width=W;
}

void Rectangle::setWidth(float W)//I'd like to change this to a do/ while loop??
{
if ((W < 0.0) || (W > 20.0))   
     {  
        width = 1.0;
     }  
     else 
        {  
          width = W;  
        }  
   return;  


}
float Rectangle::getWidth()
{
	return W;//Is this right?
}

void Rectangle::setLength(float L)//same here-do/while loop?
{
if ((L < 0.0) || (L > 20.0))   
     {  
        length = 1.0;
     }  
     else 
        {  
          length = L;  
        }  
   return;  
}
float Rectangle::getLength()
{
	return L;
}

void Rectangle::get(float L, float W)
{



}
double Rectangle::perimeter(float L, float W)
{
perimeter = (2*L) + (2*W);
cout << "The perimeter of the rectangle is: " << perimeter << endl;
}

double Rectangle::area(float L, float W)
{
area = L*W;
cout << "The area of the rectangle is :  " << area << endl;
}

int main() // main() I'm sure something is wrong here. 
// I understand the main function, but when I use classes I get confused...
{
Rectangle MyRectangle;
cout << "Please enter the length of the rectangle: " << endl;
cin >> MyRectangle.getLength >> endl;
cout << "Please enter the width of the rectangle: " << endl;
cin >> MyRectangle.getWidth >> endl;


return 0;
} // End of main()

1>------ Build started: Project: Rectangle, Configuration: Debug Win32 ------
1>Compiling...
1>Rectangle.cpp
1>c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(40) : error C2084: function 'float Rectangle::getWidth(void)' already has a body
1> c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(9) : see previous definition of 'getWidth'
1>c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(57) : error C2084: function 'float Rectangle::getLength(void)' already has a body
1> c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(7) : see previous definition of 'getLength'
1>c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(68) : error C2511: 'double Rectangle::perimeter(float,float)' : overloaded member function not found in 'Rectangle'
1> c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(4) : see declaration of 'Rectangle'
1>c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(74) : error C2511: 'double Rectangle::area(float,float)' : overloaded member function not found in 'Rectangle'
1> c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(4) : see declaration of 'Rectangle'
1>c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(82) : error C2512: 'Rectangle' : no appropriate default constructor available
1>c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(84) : error C3867: 'Rectangle::getLength': function call missing argument list; use '&Rectangle::getLength' to create a pointer to member
1>c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(84) : error C2088: '>>' : illegal for class
1>c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(86) : error C3867: 'Rectangle::getWidth': function call missing argument list; use '&Rectangle::getWidth' to create a pointer to member
1>c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(86) : error C2088: '>>' : illegal for class
1>Build log was saved at "file://c:\Users\Laura\Desktop\Rectangle\Rectangle\Debug\BuildLog.htm"
1>Rectangle - 9 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

It seems that you already had the functions getLength() and getWidth() defined in your class. You can actually delete the duplicate functions defined outside of class scope, so these functions you should completely remove from your code--

float Rectangle::getWidth()
{
	return W;//Is this right?
}

float Rectangle::getLength()
{
	return L;
}

-- because your class already has them defined and double-defining is illegal.

My apologies for not catching this the first time. I immediately assumed you followed the header/source file format for classes within one file. What I mean is I assumed this--

// file data.h

struct data{

   void p();

}
// file data.cpp

#include <iostream>

using std::cout;
using std::endl;

void data::p(){
    
   cout << "data:p()" << endl;
}

-where the class functions are not yet defined until later in the same file or in a different file. That is the usual format for classes, but you don't really have to follow it.

Alright, another thing. About rectangles. What is needed to define the perimeter and area of the rectangle? A rectangle is nothing more than a shape that is surrounded with 4 lines enclosing the shape with four 90 degree angles and each set of parallel sides have the same length as its parallel side (but it isn't restricted to not being the same length of its perpendicular side, so a rectangle can be a square... but a square cant be a rectangle). You only need 1 length and 1 width to determine the perimeter and area of a rectangle. That being said, you only need 2 variables in your Rectangle class. Let's keep length and width and toss out L and W variables. Use L and W for your function parameters--

class Rectangle // Class Rectangle
{
public:
	Rectangle(float L, float W); // Constructor
	float getLength(){return length;}
	void setLength(float L);
	float getWidth(){return width;}
	void setWidth(float W);
	double perimeter(void){return (length*2 + width*2);} // Perimeter function
	double area (void){return (length*width);} // Area funcion
	void get(float L, float W);
	
private:
	float length, width;
		}; // End of class Rectangle
Rectangle::Rectangle(float L, float W) // Scope function
{
 setLength(L);
 setWidth(W);
}

void Rectangle::setWidth(float W)//I'd like to change this to a do/ while loop??
{
if ((W < 0.0) || (W > 20.0))   
     {  
        width = 1.0;
     }  
     else 
        {  
          width = W;  
        }  
   return;  


}

void Rectangle::setLength(float L)//same here-do/while loop?
{
if ((L < 0.0) || (L > 20.0))   
     {  
        length = 1.0;
     }  
     else 
        {  
          length = L;  
        }  
   return;  
}

// What are we trying to get?
void Rectangle::get(float L, float W)
{



}

int main() // main() I'm sure something is wrong here. 
// I understand the main function, but when I use classes I get confused...
{
Rectangle MyRectangle (2.0, 3.0);
cout << "Please enter the length of the rectangle: " << endl;
cin >> MyRectangle.getLength;
cout << "Please enter the width of the rectangle: " << endl;
cin >> MyRectangle.getWidth;


return 0;
} // End of main()

-- some of the code was "fixed" without a compiler.

The part you should now work on is processing the input and passing it to the MyRectangle object. I advise that you pass the input to two float variables in main scope (float x, float y), cin to the floats then pass the floats to your constructor. You will be likely to get an error from trying to cin to the function address (though I haven't compiled this so I don't know! XD).

commented: ++ for good advice +34

Okay, I've cleaned it up quite a bit, but I'm still getting some errors. I also have to figure out the draw part once I get this to run and I need to change the if /else to do/ while to verify the parameters. The errors I'm getting are re: overloaded member not part of class??

#include <iostream>
using namespace std;
class Rectangle // Class Rectangle
{
	public:
	Rectangle(float L=1, float W=1); // Constructor
	float getLength;()/*{return length;}*/
	void setLength(float L);
	float getWidth();/*{return width;}*/
	void setWidth(float W);
	double perimeter(void);/*{return (length*2 + width*2);}*/ // Perimeter function
	double area (void);/*{return (length*width);}*/ // Area funcion
		
private:
	float length, width;
		}; // End of class Rectangle

Rectangle::Rectangle(float L, float W) // Scope function
{
setLength(L);
setWidth(W);
}

void Rectangle::setWidth(float W)//I'd like to change this to a do/ while loop??
{
if ((W < 0.0) || (W > 20.0))   
     {  
        width = 1.0;
     }  
     else 
        {  
          width = W;  
        }  
   return;  
}
float Rectangle::getWidth()const
{
	return width;
}

void Rectangle::setLength(float L)//same here-do/while loop?
{
if ((L < 0.0) || (L > 20.0))   
     {  
        length = 1.0;
     }  
     else 
        {  
          length = L;  
        }  
   return;  
}
float Rectangle::getLength()const
{
	return length;
}


float Rectangle::perimeter(float L, float W)
{
perimeter = (2*L) + (2*W);
cout << "The perimeter of the rectangle is: " << perimeter << endl;
}

float Rectangle::area(float L, float W)
{
area = L*W;
cout << "The area of the rectangle is :  " << area << endl;
}

int main() 
{
float length, width;
Rectangle MyRectangle;

cout << "Enter The Length Of The Rectangle: ";
cin >> length;

cout << "Enter The Width Of Rectangle: ";
cin >> width;

cout <<"The area of the rectangle is : "<< MyRectangle.area() << endl;
cout <<"The perimeter is: " <<MyRectangle.perimeter()<<endl;

return 0 ;
} // End of main()

Here are the errors:1>------ Build started: Project: Rectangle, Configuration: Debug Win32 ------
1>Compiling...
1>Rectangle.cpp
1>c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(37) : error C2511: 'float Rectangle::getWidth(void) const' : overloaded member function not found in 'Rectangle'
1> c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(4) : see declaration of 'Rectangle'
1>c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(54) : error C2063: 'Rectangle::getLength' : not a function
1>c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(60) : error C2511: 'float Rectangle::perimeter(float,float)' : overloaded member function not found in 'Rectangle'
1> c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(4) : see declaration of 'Rectangle'
1>c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(66) : error C2511: 'float Rectangle::area(float,float)' : overloaded member function not found in 'Rectangle'
1> c:\users\laura\desktop\rectangle\rectangle\rectangle.cpp(4) : see declaration of 'Rectangle'
1>Build log was saved at "file://c:\Users\Laura\Desktop\Rectangle\Rectangle\Debug\BuildLog.htm"
1>Rectangle - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I also just realized I'm trying to cout in two places, inside the functions and in main...doing it in main is correct, yes?

Okay folks, still working here. Happy Halloween everybody! My code now compiles! Yay! Only I need some help to change the if/ else parts to do/ while and then I'm working on the draw function. I need to add the draw function to class rectangle, truncate the numbers for the purpose of drawing them (ie 6.11 would become 6) I believe I can do this by converting the float to int. It would look something like this:
++++
++++
++++

Any new input? Here's the working code without the changes I am going to make:

#include <iostream>
using namespace std;
class Rectangle // Class Rectangle
{
	public:
	Rectangle(float L=1, float W=1); // Constructor
	float getLength() const; //{return length;}
	void setLength(float L);//{length = L;}
	float getWidth()const;// {return width;}
	void setWidth(float W);//{width = W;}
	float perimeter(float ,float);/*{return (length*2 + width*2);}*/ // Perimeter function
	float area (float, float);/*{return (length*width);}*/ // Area funcion
		
private:
	float length, width;
		}; // End of class Rectangle

Rectangle::Rectangle(float L, float W) // Scope function
{
setLength(L);
setWidth(W);
}

void Rectangle::setWidth(float W)//I'd like to change this to a do/ while loop??
{
if ((W < 0.0) || (W > 20.0))   
     {  
        width = 1.0;
     }  
     else 
        {  
          width = W;  
        }  
   return;  
}
float Rectangle::getWidth()const
{
	return width;
}

void Rectangle::setLength(float L)//same here-do/while loop?
{
if ((L < 0.0) || (L > 20.0))   
     {  
        length = 1.0;
     }  
     else 
        {  
          length = L;  
        }  
   return;  
}
float Rectangle::getLength()const
{
	return length;
}


float Rectangle::perimeter(float L, float W)
{
return((2*L) + (2*W));
//cout << "The perimeter of the rectangle is: " << perimeter << endl;
}

float Rectangle::area(float L, float W)
{
return(L*W);
//cout << "The area of the rectangle is :  " << area << endl;
}

int main() 
{
float length, width;
Rectangle MyRectangle;

cout << "Enter The Length Of The Rectangle: ";
cin >> length;

cout << "Enter The Width Of Rectangle: ";
cin >> width;

cout <<"The area of the rectangle is : "<< MyRectangle.area(length, width) << endl;
cout <<"The perimeter is: " <<MyRectangle.perimeter(length, width)<<endl;

return 0 ;
} // End of main()

Still need help with the get and set functions and calling them in main. Here's where I am:

#include <iostream>
using namespace std;
class Rectangle // Class Rectangle
{
	public:
	Rectangle(float L=1, float W=1); // Constructor
	float getLength() const; 
	void setLength(float L);
	float getWidth()const;
	void setWidth(float W);
	float perimeter(float ,float); // Perimeter function
	float area (float, float); // Area funcion
		
private:
	float length, width;
		}; // End of class Rectangle

Rectangle::Rectangle(float L, float W) // Scope function
{
setLength(L);
setWidth(W);
}

void Rectangle::setWidth(float W)//I'd like to change this to a do/ while loop??
{
do
{
	cout << "Enter the width of the rectangle: \n";
	cin >> W;	
}
while ((W < 0.0) || (W > 20.0));
}

float Rectangle::getWidth()const
{
	return width;
}

void Rectangle::setLength(float L)//same here-do/while loop?
{
do
{
	cout << "Enter the length of the rectangle: \n";
	cin >> L;
}
while ((L < 0.0) || (L > 20.0));


			/*if ((L < 0.0) || (L > 20.0))   
     {  
        length = 1.0;
     }  
     else 
        {  
          length = L;  
        }  
   return;  */
}
float Rectangle::getLength()const
{
	return length;
}


float Rectangle::perimeter(float L, float W)
{
return((2*L) + (2*W));
}

float Rectangle::area(float L, float W)
{
return(L*W);
}

int main() 
{
Rectangle MyRectangle;
float length, width;
//cout << "Enter The Length Of The Rectangle: ";
//cin >> length;

//cout << "Enter The Width Of Rectangle: ";
//cin >> width;
MyRectangle.setWidth(width);
MyRectangle.getWidth();
MyRectangle.setLength(length);
MyRectangle.getLength();
cout <<"The area of the rectangle is : "<< MyRectangle.area(length, width) << endl;
cout <<"The perimeter is: " <<MyRectangle.perimeter(length, width)<<endl;

return 0 ;
} // End of main()

It compiles, asks the user for length and width then crashes...any help?? Please!

Please format your code better. It's hard to read with the formatting you have used.

In setWidth() and setLength() you read in the values but did not load the private data. So your values in the class are junk.

Then, right after each set you call get and throw away the value you got by not putting it anywhere. Both get s are wasted calls.

Then you call area() with uninitialized local values for length and width. Shouldn't area and perimeter use the values stored in the class, not passed in values? After all, you're trying to get the values for MyRectangle , not just any ol' rectangle.

Does the program display the width and the length once the input has been accomplished for each? The reason I ask is you don't actually set the value of the appropriate member variables to user input within the set functions. You just get a valid value for L and W.

Why are you passing the user input for width and length to area()? Once you have set the width an length of the rectangle use what's already there, not outside information. I'd change area() to take no parameters. If you might want to use area more than just calculate it you could even create a member variable called area and change the method that calculates area to calculateArea() and a method called getArea() to return the value of area, just like you do for width and length.

Lerner, you know what they say about great minds.... :)

First, sorry about the formatting, I get to changing things and getsloppy. Okay,

In setWidth() and setLength() you read in the values but did not load the private data. So your values in the class are junk. Then, right after each set you call get and throw away the value you got by not putting it anywhere. Both get s are wasted calls.

Okay, these are clearly my problems. How would I load the private data here? L=length? And then-where would I put the 'get' values and how? This was my original question...I was trying to do that in main, but didn't know the syntax. I know I am missing the obvious here, but I'm really new to this. Can you make it even simpler for me?

How would I load the private data here?

Here? Where?

Where to you actually read the data? Where is the data at that time? Where do you want it?

And then-where would I put the 'get' values and how?

Where you need them. There's a possibility you won't need them. But then again...

This was my original question...I was trying to do that in main, but didn't know the syntax.

You can't just put something any ol' place just because you think you need it. You need to think it through and analyze what you are trying to accomplish. Answer the problem by writing down the steps you go through to solve it, then look at how to convert those steps into code.

after you validate that L is within the acceptable parameters then assign the value of L to length. Likewise with W and width.

Your formula to calculate the area is correct, it's length, or L, times width, or W. Since you went to the trouble of putting that information into a class then use it to your advantage.

declare this in the private section of the class:
float area;

declare these in the public section of the class:
void Rectangle::calculateArea(){area = width * length;} //note how this resembles a set function
float Rectangle::getArea()const {return area;}

//call these after calling the set functions for width and length
myRectangle.calculateArea();
cout << myRectangle.getArea();

PS: I like WaltP's answer better. You'll learn more if you follow his approach.

Okay guys. I appreciate both of your answers. Can you look at this again. I tried doing it your way, Lerner, doing the same thing for perimeter as you suggested for area. Now it compiles, asks for length and width twice each and the correct calculations as long as I enter the same values both times for length and width. can you tell me why? Ugh, I think my brain is fried.

#include <iostream>
using namespace std;
class Rectangle // Class Rectangle
{
	public:
	Rectangle(float L=1, float W=1); // Constructor
	float getLength() const; 
	void setLength(float L);
	float getWidth()const;
	void setWidth(float W);
	/*float perimeter(float ,float); */// Perimeter function
	//float area (float, float); // Area funcion
	void Rectangle::calculatePerimeter() {perimeter = ((2*length) + (2*width));}
	float Rectangle::getPerimeter()const {return perimeter;}
	void Rectangle::calculateArea(){area = width * length;} 
	float Rectangle::getArea()const {return area;}
	
private:
	float length, width, area, perimeter;
		}; // End of class Rectangle

Rectangle::Rectangle(float L, float W) // Scope function
{
setLength(L);
setWidth(W);
calculateArea();
calculatePerimeter();
}

void Rectangle::setWidth(float W)
{
  do
  {
	cout << "Enter the width of the rectangle: \n";
	cin >> W;	
  }
    while ((W < 0.0) || (W > 20.0));
	width = W;
}
float Rectangle::getWidth()const
{
	return width;
}

void Rectangle::setLength(float L)
{
  do
  {
	cout << "Enter the length of the rectangle: \n";
	cin >> L;
  }
    while ((L < 0.0) || (L > 20.0));
	length = L;
}
float Rectangle::getLength()const
{
	return length;
}

//float Rectangle::perimeter(float L, float W)
//{
//return((2*L) + (2*W));
//}

//float Rectangle::area(float L, float W)
//{
//return(L*W);
//}

int main() 
{
Rectangle MyRectangle;
float length = 0.0;
float width = 0.0;

MyRectangle.setWidth(width);
MyRectangle.getWidth();
MyRectangle.setLength(length);
MyRectangle.getLength();
MyRectangle.calculateArea();
MyRectangle.calculatePerimeter();
cout << "The area of the rectangle is: " << MyRectangle.getArea() << endl;
cout <<"The perimeter is: " <<MyRectangle.getPerimeter() << endl;

return 0 ;
} // End of main()

Here is the output:#include <iostream>
using namespace std;
class Rectangle // Class Rectangle
{
public:
Rectangle(float L=1, float W=1); // Constructor
float getLength() const;
void setLength(float L);
float getWidth()const;
void setWidth(float W);
/*float perimeter(float ,float); */// Perimeter function
//float area (float, float); // Area funcion
void Rectangle::calculatePerimeter() {perimeter = ((2*length) + (2*width));}
float Rectangle::getPerimeter()const {return perimeter;}
void Rectangle::calculateArea(){area = width * length;}
float Rectangle::getArea()const {return area;}

private:
float length, width, area, perimeter;
}; // End of class Rectangle

Rectangle::Rectangle(float L, float W) // Scope function
{
setLength(L);
setWidth(W);
calculateArea();
calculatePerimeter();
}

void Rectangle::setWidth(float W)
{
do
{
cout << "Enter the width of the rectangle: \n";
cin >> W;
}
while ((W < 0.0) || (W > 20.0));
width = W;
}
float Rectangle::getWidth()const
{
return width;
}

void Rectangle::setLength(float L)
{
do
{
cout << "Enter the length of the rectangle: \n";
cin >> L;
}
while ((L < 0.0) || (L > 20.0));
length = L;
}
float Rectangle::getLength()const
{
return length;
}

//float Rectangle::perimeter(float L, float W)
//{
//return((2*L) + (2*W));
//}

//float Rectangle::area(float L, float W)
//{
//return(L*W);
//}

int main()
{
Rectangle MyRectangle;
float length = 0.0;
float width = 0.0;

MyRectangle.setWidth(width);
MyRectangle.getWidth();
MyRectangle.setLength(length);
MyRectangle.getLength();
MyRectangle.calculateArea();
MyRectangle.calculatePerimeter();
cout << "The area of the rectangle is: " << MyRectangle.getArea() << endl;
cout <<"The perimeter is: " <<MyRectangle.getPerimeter() << endl;

return 0 ;
} // End of main()
Here is the output:
Enter the length of the rectangle:
4.0
Enter the width of the rectangle:
5.0
Enter the width of the rectangle:
5.0
Enter the length of the rectangle:
4.0
The area of the rectangle is: 20
The perimeter is: 18
Press any key to continue . . .

Here's my best guess. When you declare myRectangle it looks like you are using a default constructor with no parameters, but you are actually calling the 2 parameter constructor with default parameters. Then within the constructor you call the set functions in addition to calling them in explicitly in main. Within the constructor you don't need to call the set functions, you can assign the values of L and W directly to the appropriate variables. In fact, the ideal way to do that would be within an initialization list, but doing it within the body of the constructor is acceptable as well.

Ideally I would have functions do one task only. So I would have the set functions only set the value, not ask for the input to. I'd put the input requests in main() and pass the values obtained to the set functions. otherwise, why bother to pass anything to the set functions if you are going to ignore the parameter anyway? But the choice is yours.

Well, I got it to run correctly by just taking the set functions out of the constructor. Now I have to add a draw function to the rectangle class using length and width but truncatd to integers. I can use any character to do this. I'm working on this but I think I'm going about it wrong. Any input? Here's the code with the draw part I'm working on.

#include <iostream>
using namespace std;
class Rectangle // Class Rectangle
{
	public:
	Rectangle(float L=1, float W=1); // Constructor
	float getLength() const; 
	void setLength(float L);
	float getWidth()const;
	void setWidth(float W);
	void Rectangle::calculatePerimeter() {perimeter = ((2*length) + (2*width));}
	float Rectangle::getPerimeter()const {return perimeter;}
	void Rectangle::calculateArea(){area = width * length;} 
	float Rectangle::getArea()const {return area;}
	   
	
private:
	float length, width, area, perimeter, x, y;
		}; // End of class Rectangle

Rectangle::Rectangle(float L, float W) 
{
calculateArea();
calculatePerimeter();
}

void Rectangle::setWidth(float W)
{
  do
  {
	cout << "Enter the width of the rectangle: \n";
	cin >> W;	
  }
    while ((W < 0.0) || (W > 20.0));
	width = W;
}
float Rectangle::getWidth()const
{
	return width;
}

void Rectangle::setLength(float L)
{
  do
  {
	cout << "Enter the length of the rectangle: \n";
	cin >> L;
  }
    while ((L < 0.0) || (L > 20.0));
	length = L;
}
float Rectangle::getLength()const
{
	return length;
}

//float Rectangle::perimeter(float L, float W)
//{
//return((2*L) + (2*W));
//}

//float Rectangle::area(float L, float W)
//{
//return(L*W);
//}
int Rectangle::draw(int x, int y)
length = x;
width = y;
cout << "Please enter the character to use to draw the rectangle: " << endl ;
cin >> char1 ;

for (int x = 0; x < length; x ++) 
{
    for (int y = 0; y < width; y ++) 
	{  
        cout << char1;  
    }  
    cout << endl;  
 } 




int main() 
{
Rectangle MyRectangle;
float length = 0.0;
float width = 0.0;

MyRectangle.setWidth(width);
MyRectangle.getWidth();
MyRectangle.setLength(length);
MyRectangle.getLength();
MyRectangle.calculateArea();
MyRectangle.calculatePerimeter();
cout << "The area of the rectangle is: " << MyRectangle.getArea() << endl;
cout <<"The perimeter is: " <<MyRectangle.getPerimeter() << endl;

return 0 ;
} // End of main()

draw() needs to be declared in the class, too. There is no reason to pass parameters to draw(). As long as it's a class method it can access all the private member variables it wants.

draw() needs to be declared in the class, too. There is no reason to pass parameters to draw(). As long as it's a class method it can access all the private member variables it wants.

Can you show me what this would look like? int draw (int x, int y);?? I keep chaging things and I've messed it up again... Does my draw function look correct in my previous post? Except draw() shouldn't accept parameters? Should it be int or void? Ugh..help

What is x and y?
Are these values already in your class as private variables?
If so, don't pass them as parameters, use them from the class. Your draw() method is part of the class so it has access to all the private values.

I was trying to use x and y as integers because I have to truncate the flaoting point variables length and width to do the draw. Is that not the way to do it?

You can truncate the float values to int by casting the member variables to int or you can use built in functions floor() or ceil() or you can write your own rounding function if you want. But none of those approaches mean you have to pass values to draw() as long as draw() is a member function.

int Rectangle::draw()
{
 int ilength = static_cast<int> length; //C++ style cast of float to int
 int iwidth = (int)width; //C style cast of float to int
 cout << "Please enter the character to use to draw the rectangle: " << endl ;
 cin >> char1 ; 
 for (int x = 0; x < ilength; x ++) 
 {    
   for (int y = 0; y < iwidth; y ++) 	
   {            
     cout << char1;      
   }      
   cout << endl;   
 } 
}
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.