CodeSolved

Solved Programming Questions & Exercises

Animal voice class

Practice Easy 2042/ Download 290 Views
Define the basic class with the name of the Make_Sound method. Then create DOG and CAT classes to inherit the Animal and each animal has its own sound.

2 Answers

class Animal:
    def make_sound(self):
        print("صدای حیوان")

class Dog(Animal):
    def make_sound(self):
        print("hp hp")


class Cat(Animal):
    def make_sound(self):
        return "me me"
m=Dog()
m.make_sound()

User 5347 Download Python
class Animal:
    def __init__(self, name):
        self.name = name

    def make_sound(self):
        if self.name == 'dog':
            return f"{self.name} : woof woof"
        elif self.name == 'cat':
            return f"{self.name} : meow meow"

class Dog(Animal):
    def __init__(self, name):
        super().__init__(name)

class Cat(Animal):
    def __init__(self, name):
        super().__init__(name)

dog1 = Dog('dog')
cat1 = Cat('cat')

print(dog1.make_sound())
print(cat1.make_sound())
User 4451 Download Python

Submit answer

Submitting answers is currently unavailable.

×
×
Close