2
2
#include < string>
3
3
using namespace std ;
4
4
5
- // class 'Rectangle'
6
- class Rectangle {
5
+ // class 'Rectangle'
6
+ class Rectangle
7
+ {
7
8
private:
8
- double length, breadth;
9
+ double length, breadth;
10
+
9
11
public:
10
- double getArea () {
12
+ double getArea ()
13
+ {
11
14
return length * breadth;
12
15
}
13
- void setLength (double length) {
16
+ void setLength (double length)
17
+ {
14
18
this ->length = length;
15
- }
16
- void setBreadth (double breadth) {
19
+ }
20
+ void setBreadth (double breadth)
21
+ {
17
22
this ->breadth = breadth;
18
-
19
23
}
20
- Rectangle operator +(Rectangle& r2) {
24
+ Rectangle operator +(Rectangle &r2)
25
+ {
21
26
Rectangle temp;
22
27
temp.setLength (length + r2.length );
23
28
temp.setBreadth (breadth + r2.breadth );
24
29
return temp;
25
30
}
26
-
27
31
};
28
32
// void function for the inputing data for class 'Rectangle' & uisng the overloading
29
- void RectangleFirst () {
33
+ void RectangleFirst ()
34
+ {
30
35
Rectangle r3, r1, r2;
31
36
int temp;
32
37
cout << " Rectangle 1" << endl;
@@ -36,7 +41,8 @@ void RectangleFirst() {
36
41
cout << " Breadth: " ;
37
42
cin >> temp;
38
43
r1.setBreadth (temp);
39
- cout << " Area: " << r1.getArea () << endl << endl;
44
+ cout << " Area: " << r1.getArea () << endl
45
+ << endl;
40
46
41
47
cout << " Rectangle 2" << endl;
42
48
cout << " Length: " ;
@@ -45,63 +51,79 @@ void RectangleFirst() {
45
51
cout << " Breadth: " ;
46
52
cin >> temp;
47
53
r2.setBreadth (temp);
48
- cout << " Area: " << r2.getArea () << endl << endl;
54
+ cout << " Area: " << r2.getArea () << endl
55
+ << endl;
49
56
50
57
r3 = r1 + r2; // overloading by the binary operator
51
58
cout << " Rectangle 3 Area: " << r3.getArea () << endl;
52
-
53
59
}
54
60
55
- class Distance {
61
+ class Distance
62
+ {
56
63
private:
57
64
float Km, M;
65
+
58
66
public:
59
- void setKm (int Km) {
67
+ void setKm (int Km)
68
+ {
60
69
this ->Km = Km;
61
70
}
62
- void setM (int M) {
71
+ void setM (int M)
72
+ {
63
73
this ->M = M;
64
74
}
65
- Distance operator ==(Distance& d) {
66
- if ((Km == d.Km ) && (M == d.M )) {
75
+ Distance operator ==(Distance &d)
76
+ {
77
+ if ((Km == d.Km ) && (M == d.M ))
78
+ {
67
79
cout << " They are EQUAL.\n " ;
68
80
return *this ;
69
81
}
70
- else {
82
+ else
83
+ {
71
84
cout << " NOT EQUAL.\n " ;
72
85
return *this ;
73
86
}
74
87
}
75
88
};
76
- void DistanceSecond () {
89
+ void DistanceSecond ()
90
+ {
77
91
Distance d1, d2;
78
- float k1,m1,k2,m2;
92
+ float k1, m1, k2, m2;
79
93
cout << " First distance: \n " ;
80
94
cout << " Kilometers: " ;
81
95
cin >> k1;
82
96
cout << " Meters: " ;
83
97
cin >> m1;
84
- if (m1 > 1000 ) {
98
+ if (m1 > 1000 )
99
+ {
85
100
k1 = m1 / 1000 ;
86
101
}
87
- cout << endl << endl;
102
+ cout << endl
103
+ << endl;
88
104
89
105
cout << " Second distance: \n " ;
90
106
cout << " Kilometers: " ;
91
107
cin >> k2;
92
108
cout << " Meters: " ;
93
109
cin >> m2;
94
- if (m2 > 1000 ) {
110
+ if (m2 > 1000 )
111
+ {
95
112
k2 = m2 / 1000 ;
96
113
}
97
114
d1 == d2;
98
115
}
99
- int main () {
116
+ int main ()
117
+ {
100
118
int choice;
101
119
102
- do {
120
+ do
121
+ {
103
122
104
- cout << " 1. Rectangle" << endl << " 2. Distance" << endl << " 3. Exit" << endl << " Your choice: " ;
123
+ cout << " 1. Rectangle" << endl
124
+ << " 2. Distance" << endl
125
+ << " 3. Exit" << endl
126
+ << " Your choice: " ;
105
127
cin >> choice;
106
128
switch (choice)
107
129
{
0 commit comments