-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopeator_overloading.py
90 lines (74 loc) · 1.75 KB
/
opeator_overloading.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
##'''
##Operator Overloading
##
##It is a feature of Polymorphism. It uses the normal operators to be overloaded
##and return the result.
##
##'''
##
##
##class Number (object):
##
## def __init__(self, a, b, c):
##
## self.a = a
## self.b = b
## self.c = c
##
## ## Creating a function named checkGreater
## def check_greater(self, obj1):
## if (self.a > obj1.a):
## if (self.b > obj1.b):
## if (self.c > obj1.c):
## return True
##
## return False
##
## def __gt__(self, obj1):
## if (self.a > obj1.a):
## if (self.b > obj1.b):
## if (self.c > obj1.c):
## return True
##
## return False
##
## def __add__(self, obj):
## n = Number(0, 0, 0)
## n.a = self.a + obj.a
## n.b = self.b + obj.b
## n.c = self.c + obj.c
## return n
##
##n1 = Number(4,5,6)
##n2 = Number (3,2,4)
##print (n1.check_greater(n2))
##
##print (n1 > n2)
class Complex(object):
def __init__(self, real, img):
self.real = real
self.img = img
def __add__(self, obj):
real = self.real + obj.real
img = self.img + obj.img
return Complex(real, img)
def __repr__(self):
return f'{self.real}+{self.img}i'
def __sub__(self, obj):
real = self.real - obj.real
img = self.img - obj.img
return Complex(real, img)
def __mul__(self, obj):
real = self.real * obj.real
img = self.img * obj.img
return Complex(real, img)
def __gt__(self, obj):
pass
def __lt__(self, obj):
pass
def __div__(self, obj):
pass
c1 = Complex(5, 4)
c2 = Complex(6, 4)
c3 = c1 + c2
c4 = Complex(3, 2)