-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperator_Overloading.cpp
More file actions
51 lines (36 loc) · 1 KB
/
Operator_Overloading.cpp
File metadata and controls
51 lines (36 loc) · 1 KB
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
#include<iostream>
using namespace std;
class Point{
public:
int x,y;
Point(){
this->x=10;
this->y=20;
}
Point(int x, int y){
this->x=x;
this->y=y;
}
void disp(){
cout<<"X = "<<this->x<<" Y = "<<this->y<<endl;
}
};
//operator function as non member function of the class
Point operator+(Point &pt1, Point &pt2){ // global/non-member function
//if we want to overload binary operator using non momber function
//::operator+(pt1,pt2) //then operator+() takes two arguments
Point temp;
temp.x= pt1.x + pt2.x;
temp.y= pt1.y + pt2.y;
return temp;
}
int main(){
Point pt1(15,25);
Point pt2(30,40);
Point pt3;
pt1.disp();
pt2.disp();
pt3=pt1+pt2; // + binary operator
pt3.disp();
return 0;
}