Creating Class rectangle, length & width, functions area, perimeter, get and set

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#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()

Here are some changes I've made. Can anyone offer feedback for me? I'll also post the errors I get.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#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()

Errors: ya-there are a lot!
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 ==========
Anthony,

I can't make head or tails of your program, but I am still learning C++. The tutorial on classes on this site uses the area of a rectangle as an example. It is a very good example to which you can compare your own program. Then you will get a good appreciation for just how confused your code is.

Good Luck.
Oay, I've cleaned it up quite a bit. It's making more sense now, but I'm still a few errors and need to add the draw part once I get this to run. Can anyone help? Thanks! Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#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)
{
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() << endl;
cout <<"The perimeter is: " <<MyRectangle.perimeter()<<endl;

return 0 ;
} // End of main()

And here are my 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 ==========
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#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()
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#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() 
Your set functions don't act the way I expect them to...instead of setting the width/length to the value I pass, it ignores that and instead prompts me for a value between 0 and 20 for some reason.

Oh, and your (IMO) poor indentation makes it really hard to read your program. I suggest you try to clean that up as well.
Last edited on
I will try to clean up the indents. I want to limit the length and width to 0.0-20.0, so is it doing what I want or is there an error in my set functions?
Topic archived. No new replies allowed.