-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab1.cpp
71 lines (53 loc) · 1.2 KB
/
lab1.cpp
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
#include <iostream>
#include <cmath>
using namespace std;
class Rectangle
{
private:
double height;
double width;
public:
Rectangle() { width = height = 0; }
Rectangle(double w, double h) { width = w; height = h; }
~Rectangle() {}
double getWidth() { return width; }
double getHeight() { return height; }
void setWidth(double w) {
while (w <= 0) {
cout << "This parameter is not correct!Enter again, please" << endl;
cin >> w;
}
width = w;
}
void setHeight(double h) {
if (h <= 0) {
cout << "This parameter is not correct!Enter again, please" << endl;
cin >> h;
}
height = h;
}
void Print() { cout << "Height: " << height <<"\nWidth:" << width << endl; }
double Area() {
double a = width * height;
return a;
}
double Perimeter() {
double p = 2 * (width + height);
return p;
}
};
int main()
{
double width, height;
Rectangle obj;
cout << "Enter the width: ";
cin >> width;
obj.setWidth(width);
cout << "Enter the height: ";
cin >> height;
obj.setHeight(height);
obj.Print();
cout << "Area: " << obj.Area() << endl;
cout << "Perimeter: " << obj.Perimeter() << endl;
return 0;
}