0% found this document useful (0 votes)
4 views1 page

Oops 3 06

The document provides examples of operator overloading in Python, specifically for the Student and Employee classes. It demonstrates how to overload comparison operators (>, <=) for Student objects based on marks and the multiplication operator (*) for Employee objects based on salary and days worked. The output of the operations is also included, showing the results of the comparisons and salary calculation.

Uploaded by

vishnu050621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Oops 3 06

The document provides examples of operator overloading in Python, specifically for the Student and Employee classes. It demonstrates how to overload comparison operators (>, <=) for Student objects based on marks and the multiplication operator (*) for Employee objects based on salary and days worked. The output of the operations is also included, showing the results of the comparisons and salary calculation.

Uploaded by

vishnu050621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

== ---> object.

__eq__(self,other)
!= ---> object.__ne__(self,other)

Overloading > and <= operators for Student class objects:

1) class Student:
2) def __init__(self,name,marks):
3) self.name=name
4) self.marks=marks
5) def __gt__(self,other):
6) return self.marks>other.marks
7) def __le__(self,other):
8) return self.marks<=other.marks
9)
10)
11) print("10>20 =",10>20)
12) s1=Student("Durga",100)
13) s2=Student("Ravi",200)
14) print("s1>s2=",s1>s2)
15) print("s1<s2=",s1<s2)
16) print("s1<=s2=",s1<=s2)
17) print("s1>=s2=",s1>=s2)

Output:
10>20 = False
s1>s2= False
s1<s2= True
s1<=s2= True
s1>=s2= False

Program to overload multiplication operator to work on Employee objects:

1) class Employee:
2) def __init__(self,name,salary):
3) self.name=name
4) self.salary=salary
5) def __mul__(self,other):
6) return self.salary*other.days
7)
8) class TimeSheet:
9) def __init__(self,name,days):
10) self.name=name
11) self.days=days
12)
13) e=Employee('Durga',500)
14) t=TimeSheet('Durga',25)
15) print('This Month Salary:',e*t)

Output: This Month Salary: 12500


nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
6  040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com

You might also like