1. सीखें
  2. /
  3. पाठ्यक्रम
  4. /
  5. Intermediate Object-Oriented Programming in Python

Connected

अभ्यास

Overloading +

Comparison operators aren't the only operators that can be overloaded. Classes in Python can implement custom functionality for arithmetic operators such as + and -. In this example, you'll practice overloading the + operator for a Storage class.

निर्देश

100 XP
  • Define a magic method to overload the + operator for the Storage class.
  • Create a new Storage object called total_storage with a capacity that equals the combined capacity of both objects.
  • Add the onboard_storage and external_drive objects to create a total_storage object, and print the capacity of total_storage.