class Time(object): def __init__(self, hour, min): self.hour = hour self.min = min def __str__(self): """overloading the str operator (STRing)""" return f"[ {self.hour:2d}:{self.min:02d} ]" def __repr__(self): """overloading the repr operator (REPResentation)""" return f"Time({self.hour:2d}, {self.min:02d})" def __gt__(self, other): """overloading the GreaterThan operator""" selfminutes = self.hour * 60 + self.min otherminutes = other.hour * 60 + other.min return selfminutes > otherminutes def __add__(self, other): hour = self.hour + other.hour minute = self.min + other.min while minute > 60: minute = minute - 60 hour = hour + 1 while hour > 24: hour = hour - 24 return self.__class__(hour, minute) class TimeUK(Time): """Derived (or inherited class)""" def __str__(self): """overloading the str operator (STRing)""" if self.hour < 12: ampm = "am" else: ampm = "pm" return f"[{self.hour:2}:{self.min:02}{ampm}]" if __name__ == "__main__": t1 = Time(11, 45) t2 = TimeUK(10, 20) print(t1, t2) print(f"The sum of t1 and t2 is {t1+t2}.")