Operator Overloading
"O! call back yesterday, bid time return."
— William Shakespeare
Common
- Overload the "+" Operator for Adding Distances: Imagine you are calculating the total distance covered by two cars in a road trip. Define a class
Distance
with attributeskm
(kilometers). Overload the + operator so that adding twoDistance
objects returns the total distance. For example, if one car covers 50 km and another covers 70 km, adding them should give 120 km. - Overload the "==" Operator to Compare Books: You are working in a library and need to check if two books are the same. Define a class
Book
with attributestitle
andauthor
. Overload the == operator to compare twoBook
objects and returnTrue
if both the title and author are the same. For example, two books titled "Python Basics" by "John Doe" should be considered equal. - Overload the "-" Operator to Subtract Time: You want to calculate how much time is left in an exam. Define a class
Time
with attributeshours
andminutes
. Overload the - operator to subtract twoTime
objects and return the time difference. For instance, if the exam started at 3 hours and 30 minutes, and 1 hour and 45 minutes have passed, the remaining time should be calculated. - Overload the "*" Operator to Scale Salaries: Suppose you need to give a 10% raise to the salary of all employees. Define a class
Salary
with an attributeamount
. Overload the * operator to multiply the salary by a percentage. For example, multiplying aSalary
object with 1.1 should return a new salary that is 10% higher than the original. - Overload the "/" Operator for Cutting Pizza: Imagine you are sharing a pizza with your friends. Define a class
Pizza
with an attributeslices
(number of slices). Overload the / operator to divide the pizza into equal parts. For instance, dividing aPizza
with 8 slices by 4 should return how many slices each person gets. - Overload the ">" Operator to Compare Exam Scores: You are comparing the scores of students in a test. Define a class
Score
with an attributemarks
. Overload the > operator to compare twoScore
objects and returnTrue
if one student has higher marks than another. For example, if student A has 85 marks and student B has 75, comparing them should show that student A scored higher. - Overload the "<" Operator to Compare Heights: You are helping to sort students by height for a school event. Define a class
Height
with an attributecm
(centimeters). Overload the < operator to compare twoHeight
objects and returnTrue
if one person is shorter than the other. For example, if one person is 150 cm and another is 160 cm, the comparison should correctly reflect who is shorter. - Overload the "str()" Function to Display Shopping Cart Items: You are building a shopping app that needs to display cart items in a readable way. Define a class
Item
with attributesname
andprice
. Overload the __str__() method so that when you print anItem
, it displays as "Item: [name], Price: [price]". For example, if an item is "Laptop" with a price of 1000, printing it should display "Item: Laptop, Price: 1000". - Overload the "len()" Function for Counting Words in a Sentence: Imagine you are counting words in sentences for a project. Define a class
Sentence
with an attributetext
(a string). Overload the __len__() method so that calling len() on aSentence
object returns the number of words in the sentence. For example, "Python is fun" should return 3 words. - Overload the "%" Operator to Check Divisibility for a Contest: You are organizing a contest where you check if the contestant's number is divisible by a certain value. Define a class
ContestantNumber
with an attributenumber
. Overload the % operator to check if a contestant's number is divisible by a given value. For example, for contestant number 24, checking if it’s divisible by 6 should returnTrue
.