Rectangle Class.

The question is:

create a rectangle class with attributes length and width, each of which defaults to 1. provide member functions that calculate the perimeter and the area of the rectangle. also provide set and get functions for the length and width attributes. the set functions should verify that legnth and width are each floating-point numbers larger than 0.0 and less than 20.0
thank you.
Sounds like a great homework problem.

We won't do your homework for you, but we will help you when you get stuck.

Post what you have so far. Tell us what you are having problems with, and we'll try to help you solve them.
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

/*Header File Rectangle.h */


class Rectangle 

{

private:
double length;
double width;

public:
 Rectangle ();


Rectangle (double len, double wid);



double getLength () const;
double getWidth () const;


void setLength (double l);
void setWidth (double w);
double getArea () const;
double getPerimeter () const;
void displayStatistics () const;


};




/*  Rectangle.cpp*/


#include <iostream>

#include "Rectangle.h"

using namespace std;




Rectangle::Rectangle ()

{

length =1.0;
width = 1.0;

}


Rectangle::Rectangle (double len, double wid)

{

length = len;

width = wid;

}


void Rectangle::setLength (double l)

{

length = l;

}


void Rectangle::setWidth (double w)

{

width = w;

}

double Rectangle::getLength () const

{

return length;


}


double Rectangle::getWidth () const

{

return width;

}


double Rectangle::getArea () const

{

return (length*width);

}


double Rectangle::getPerimeter () const

{

return (2*(length+width));

}



void Rectangle::displayStatistics () const

{

cout <<endl<<"length = " <<getLength ()<<" unit (s)";
cout <<endl<<"width = " <<getWidth ()<<" unit (s)";
cout <<endl<<"Area = " <<getArea ()<<" square unit (s)";
cout <<endl<<"Perimeter = " <<getPerimeter () <<" unit (s)";
cout <<endl;

}



/* test.cpp */


#include <iostream>
#include "Rectangle.h"


using namespace std;


int main ()

{
Rectangle unitRectangle;
Rectangle myRectangle(2.0,5.0);

cout <<"Displaying the statistics of unit rectangle :"<<endl;

unitRectangle.displayStatistics ();

cout <<endl<<"Now displaying the statistics of myRectangle :"<<endl;

myRectangle.displayStatistics ();

double num1,num2;

cout <<endl;
cout <<"Enter the length and width of a rectangle followed by space:";
cin>>num1 >> num2;

myRectangle.setLength(num1);
myRectangle.setWidth (num2);

cout <<endl <<"Now displaying the statistics of the new Rectangle with ";
cout <<"Users input "<<num1<<" and "<<num2<<": "<<endl;

myRectangle.displayStatistics ();

cout <<endl;

return 0;

}




where i have to set the set functions should verify that legnth and width are each floating-point numbers larger than 0.0 and less than 20.0
Input validation can be interesting. The validation statement in the assignment is part of the set functions, so that simplifies things. Because you are using a double for an argument, you are guaranteed a floating point value in the set function, so that part is taken care of for you. What you need to do is make sure the value is within the legal range before making the assignments in lines 74 and 83.

Validating user input to make sure that it is a legal floating point value (no letters, at most 1 period, etc.) is a more complex endeavor and probably not part of the set function.
Topic archived. No new replies allowed.