Skip to content

Commit

Permalink
Create calculator.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jisunp04023 authored Oct 19, 2023
1 parent 15fbaee commit 91b42d3
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Calculator:
def __init__(self, a, b): # 자동 실행되는 생성자
self.a = a
self.b = b

def add(self):
return self.a + self.b

def sub(self):
return self.a - self.b

def mul(self):
return self.a * self.b

def div(self):
return self.a / self.b

cal1 = Calculator(3, 1)
cal2 = Calculator(4, 2)
print(cal1.a)
print(cal1.b)
print(cal1.sub())
print(cal1.mul())

print(cal2.add())
print(cal2.div())

class BabyCalculator(Calculator): # Calculator의 기능을 모두 상속 받음
def square(self): # a의 b승 계산하는 함수 추가
return self.a ** self.b

def div(self): # Calculator의 div 함수를 오버라이드함
if not self.b:
return 0
else:
return self.a / self.b

cal3 = BabyCalculator(6, 0)

print(cal3.square())
print(cal3.div())

0 comments on commit 91b42d3

Please sign in to comment.