Sunday, May 12, 2024

Addition and Subtraction Using Polymorphism in Python

 class MathOperation:

    def perform_operation(self, a, b):

        return 0  # Base class default implementation


class Addition(MathOperation):

    def perform_operation(self, a, b):

        return a + b


class Subtraction(MathOperation):

    def perform_operation(self, a, b):

        return a - b


if __name__ == "__main__":

    operation = None

    num1, num2 = 0, 0


    print("\n\n\tAddition and Subtraction Using Polymorphism in Python\n")

    num1 = int(input("\tEnter the first number: "))

    num2 = int(input("\tEnter the second number: "))

    

    op = input("\tEnter the operation (+ for addition, - for subtraction): ")


    if op == '+':

        operation = Addition()

    elif op == '-':

        operation = Subtraction()

    else:

        print("\tInvalid operation.")

        exit()


    result = operation.perform_operation(num1, num2)

    print("\n\tThe Result:", result)


    print("\n\tEnd of Program. Thank you for using this program.")


No comments:

Post a Comment