Question 50: Write a Python program to create a class to print the area of a square and a rectangle. The class has two methods with the same name but different number of parameters. The method for printing area of rectangle has two parameters which are length and breadth respectively while the other method for printing area of square has one parameter which is side of square. Download hole Program / Project code, by clicking following link: What is the output of the following program ? # parent class
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breath
def breathe(self):
print("I breathe oxygen.")
# function feed
def feed(self):
print("I eat food.")
# child class
class Herbivorous(Animal):
# function feed
def feed(self):
print("I eat only plants. I am vegetarian.")
herbi = Herbivorous()
herbi.feed()
# calling some other function
herbi.breathe()
The provided Python program defines a parent class Animal and a child class Herbivorous. The child class Herbivorous inherits from the parent class Animal and overrides the feed method. Here's the expected output of the program: output:
I eat only plants.
I am vegetarian.
I breathe oxygen.
Here's a breakdown of the output: - An instance of the Herbivorous class, named herbi, is created.
- The feed method in the Herbivorous class overrides the feed method in the parent Animal class. When you call herbi.feed(), it prints "I eat only plants. I am vegetarian." This demonstrates method overriding.
- The breathe method is inherited from the parent Animal class. When you call herbi.breathe(), it prints "I breathe oxygen." This demonstrates that the child class inherits methods from the parent class.
Programming Code: Following code write in: BP_P50.py # Python Program
# Methods with same name but different number of parameters.
# create class
class Cal_Area():
# Define Methods
def area(self, length, breadth):
print(length * breadth)
def area(self, side):
print(side * side)
# Creating class instance
a = Cal_Area()
# Call Methods
# print("Area of Square is: ", a.area(1))
print("Area of Rectangle is: ", a.area(3, 50))
a.area(1)
# Thanks for Reading.
Output:
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breath
def breathe(self):
print("I breathe oxygen.")
# function feed
def feed(self):
print("I eat food.")
# child class
class Herbivorous(Animal):
# function feed
def feed(self):
print("I eat only plants. I am vegetarian.")
herbi = Herbivorous()
herbi.feed()
# calling some other function
herbi.breathe()
The provided Python program defines a parent class Animal and a child class Herbivorous. The child class Herbivorous inherits from the parent class Animal and overrides the feed method. Here's the expected output of the program:
I eat only plants.
I am vegetarian.
I breathe oxygen.
- An instance of the Herbivorous class, named herbi, is created.
- The feed method in the Herbivorous class overrides the feed method in the parent Animal class. When you call herbi.feed(), it prints "I eat only plants. I am vegetarian." This demonstrates method overriding.
- The breathe method is inherited from the parent Animal class. When you call herbi.breathe(), it prints "I breathe oxygen." This demonstrates that the child class inherits methods from the parent class.
# Python Program # Methods with same name but different number of parameters. # create class class Cal_Area(): # Define Methods def area(self, length, breadth): print(length * breadth) def area(self, side): print(side * side) # Creating class instance a = Cal_Area() # Call Methods # print("Area of Square is: ", a.area(1)) print("Area of Rectangle is: ", a.area(3, 50)) a.area(1) # Thanks for Reading.
Output:
0 Comments