-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeb102026_Operator_Overloading.py
More file actions
46 lines (36 loc) · 977 Bytes
/
Copy pathFeb102026_Operator_Overloading.py
File metadata and controls
46 lines (36 loc) · 977 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
class Point:
def __init__(self, x, y):
print("init called.")
self.x=x
self.y=y
def __str__(self):
print("str called.")
return "{0},{1}".format(self.x,self.y)
def __add__(self, obj):
print("add called.")
x=self.x+obj.x
y=self.y+obj.y
return Point(x,y)
def __sub__(self, obj):
print("sub called.")
x=self.x-obj.x
y=self.y-obj.y
return Point(x,y)
def __mul__(self, obj):
print("mul called.")
x=self.x*obj.x
y=self.y*obj.y
return Point(x,y)
def __truediv__(self, obj):
print("div called.")
x=self.x/obj.x
y=self.y/obj.y
return Point(x,y)
p1=Point(10,20)
print(p1)
p2=Point(30,40)
print(p2)
print("Addition of 2 Objects: ", p1+p2)
print("Subtraction of 2 Objects: ", p1-p2)
print("Multiplication of 2 Objects: ", p1*p2)
print("Division of 2 Objects: ", p1/p2)