Operator Overloading

"O! call back yesterday, bid time return."

— William Shakespeare

Common


  1. 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 attributes km (kilometers). Overload the + operator so that adding two Distance objects returns the total distance. For example, if one car covers 50 km and another covers 70 km, adding them should give 120 km.
  2. 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 attributes title and author. Overload the == operator to compare two Book objects and return True if both the title and author are the same. For example, two books titled "Python Basics" by "John Doe" should be considered equal.
  3. Overload the "-" Operator to Subtract Time: You want to calculate how much time is left in an exam. Define a class Time with attributes hours and minutes. Overload the - operator to subtract two Time 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.
  4. 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 attribute amount. Overload the * operator to multiply the salary by a percentage. For example, multiplying a Salary object with 1.1 should return a new salary that is 10% higher than the original.
  5. Overload the "/" Operator for Cutting Pizza: Imagine you are sharing a pizza with your friends. Define a class Pizza with an attribute slices (number of slices). Overload the / operator to divide the pizza into equal parts. For instance, dividing a Pizza with 8 slices by 4 should return how many slices each person gets.
  6. Overload the ">" Operator to Compare Exam Scores: You are comparing the scores of students in a test. Define a class Score with an attribute marks. Overload the > operator to compare two Score objects and return True 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.
  7. Overload the "<" Operator to Compare Heights: You are helping to sort students by height for a school event. Define a class Height with an attribute cm (centimeters). Overload the < operator to compare two Height objects and return True 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.
  8. 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 attributes name and price. Overload the __str__() method so that when you print an Item, 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".
  9. 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 attribute text (a string). Overload the __len__() method so that calling len() on a Sentence object returns the number of words in the sentence. For example, "Python is fun" should return 3 words.
  10. 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 attribute number. 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 return True.

Find it difficult?

Don’t lose heart, don’t be under confident, just be consistent in your preparation and be sure of everything you’ve studied. You can request a class so that we can help you understant this topic.

Feel Confident?

Your first step in learning any new topic is to be aware of your strengths and weaknesses. Once you know this, your self-preparation can be meaningful and result-oriented. Attempt an quiz to get tested.