Created
September 23, 2023 23:56
-
-
Save mostlyvirtual/667c38961b2b4e13dc5423f4b451dc58 to your computer and use it in GitHub Desktop.
Python Calculator Demonstrating Classes with Inheritance and Polymorphism
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define a class called Calculator to encapsulate arithmetic operations | |
import math | |
class Calculator: | |
""" | |
Initializes the object with the given var_x and var_y values. | |
Parameters: | |
var_x (int): The value for the var_x coordinate. | |
var_y (int): The value for the var_y coordinate. | |
""" | |
# Initialize the class with default parameters | |
def __init__(self, var_x, var_y): | |
self.var_x = var_x # First operand | |
self.var_y = var_y # Second operand | |
# Method to add two numbers | |
def add(self): | |
return self.var_x + self.var_y | |
# Method to subtract two numbers | |
def subtract(self): | |
return self.var_x - self.var_y | |
# Method to multiply two numbers | |
def multiply(self): | |
return self.var_x * self.var_y | |
def divide(self): | |
if self.var_y == 0: | |
return "Cannot divide by zero" | |
return self.var_x / self.var_y | |
# Define a subclass called AdvancedCalculator that inherits from Calculator | |
class AdvancedCalculator(Calculator): | |
# Method to divide two numbers, demonstrating polymorphism by overriding the parent class method | |
def divide(self): | |
if self.var_y == 0: | |
return "Cannot divide by zero, obviously" | |
return self.var_x / self.var_y | |
# Evar_xtending the add method from the parent class | |
def add(self): | |
# Calling the parent class's add method | |
result = super().add() | |
# Returning the square root of the sum | |
return math.sqrt(result) | |
# Method to calculate the power of var_x raised to var_y | |
def power(self): | |
return self.var_x ** self.var_y | |
# Create an instance of Calculator | |
calc1 = Calculator(5, 3) | |
# Use the methods of Calculator | |
print(calc1.add()) # Output: 8 | |
print(calc1.subtract()) # Output: 2 | |
print(calc1.multiply()) # Output: 15 | |
print(calc1.divide()) # Output: 1.666... | |
calc3 = Calculator(5, 0) | |
# Use the methods of Calculator | |
print(calc3.divide()) # Output: Cannot divide by zero | |
print(calc3.add()) # Output: 5 | |
# Create an instance of AdvancedCalculator | |
# Note that AdvancedCalculator inherits from Calculator | |
# Note that AdvancedCalculator's add method overrides the parent class | |
calc2 = AdvancedCalculator(5, 3) | |
print('- AdvancedCalculator starts here:') | |
# Use the methods of AdvancedCalculator, demonstrating inheritance | |
print(calc2.add()) # Output: 2.828.. (specific to AdvancedCalculator) | |
print(calc2.divide()) # Output: 1.666... | |
print(calc2.power()) # Output: 125 (specific to AdvancedCalculator) | |
calc4 = AdvancedCalculator(5, 0) | |
print(calc4.divide()) # Output: Cannot divide by zero, obviously (specific to AdvancedCalculator) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment