-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoops_operator_overloading.cpp
More file actions
57 lines (46 loc) · 899 Bytes
/
oops_operator_overloading.cpp
File metadata and controls
57 lines (46 loc) · 899 Bytes
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
#include <iostream>
using namespace std;
class Rectangle
{
private:
int length = 0;
int width = 0;
public:
void setLength(int);
void setWidth(int);
int volume();
Rectangle operator+(Rectangle &other);
};
void Rectangle::setLength(int l)
{
this->length = l;
}
void Rectangle::setWidth(int w)
{
this->width = w;
}
int Rectangle::volume()
{
return this->width * this->length;
}
Rectangle Rectangle::operator+(Rectangle &other)
{
Rectangle r3;
r3.setLength(this->length + other.length);
r3.setWidth(this->width + other.width);
return r3;
}
int main()
{
Rectangle r1;
r1.setLength(10);
r1.setWidth(5);
Rectangle r2;
r2.setLength(20);
r2.setWidth(10);
cout << "Vol1: " << r1.volume() << endl;
cout << "Vol2: " << r2.volume() << endl;
Rectangle r3 = r1 + r2;
cout << r3.volume() << endl;
return 1;
}