Calculator using OOPs
- Basic Exercise on Object Oriented Programming
- Create one class calculator
-
While creating object of calculator class take two user inputs.
-
Display addition , subtraction, multiplication and division of two input numbers.
Exercise Solution
class calculator():
def __init__(self,x,y):
self.x=x
self.y=y
def add(self):
print("Sum :",self.x+self.y)
def subtraction(self):
print("Subtraction :",self.x-self.y)
def multiplication(self):
print("Multiplication :",self.x*self.y)
def division(self):
print("Division :",self.x/self.y)
#x=int(input("Enter first number : "))
#y=int(input("Enter second number : "))
#n1=calculator(x,y) # object
n1=calculator(5,4) # object
n1.add()
n1.subtraction()
n1.multiplication()
n1.division()
Output
Sum : 9
Subtraction : 1
Multiplication : 20
Division : 1.25
Answer these Questions
-
What is the name of the method used in calculator class ?
-
List all objects created in above code ?
-
What is the data type of created object
« Tutorial on Class Object & Method
Exercise constructor ( basic )»
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com