حالت آفلاین — محتوا از حافظهٔ کش نمایش داده می‌شود
همهٔ تمرین‌ها

کلاس صدای حیوانات

تمرین آسان 1069 visibility link download flag

کلاس پایه ای با اسم Animal با متد make_sound تعریف کنید. سپس کلاس‌های Dog و Cat بسازید که از Animal ارث‌بری کنند و هر حیوان صدای مخصوص خودش را داشته باشد.

👨‍💻 20 ساعت قبل کاربر ناشناس این تمرین رو مشاهده کرد

پاسخ‌ها 5

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()

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

class dog (Animal):
    def __init__ (self):
        print("hop hop")

class cat(Animal):
    def __init__(self):
        print("meo meo")

m = dog()

class Animal:
    def make_sound(self):
        pass

class Dog(Animal):
    def make_sound(self):
        return "Woof"

class Cat(Animal):
    def make_sound(self):
        return "Meow"
پاسخ هوش مصنوعی Ai Python
C
#include <stdio.h>

typedef struct Animal Animal;

struct Animal {
    const char *(*make_sound)(const Animal *self);
};

typedef struct {
    Animal base;
} Dog;

typedef struct {
    Animal base;
} Cat;

const char *dog_make_sound(const Animal *self) {
    (void)self;
    return "Woof";
}

const char *cat_make_sound(const Animal *self) {
    (void)self;
    return "Meow";
}

int main(void) {
    Dog dog = {{dog_make_sound}};
    Cat cat = {{cat_make_sound}};

    printf("%s\n", dog.base.make_sound(&dog.base));
    printf("%s\n", cat.base.make_sound(&cat.base));

    return 0;
}
C++
#include <iostream>

class Animal {
public:
    virtual ~Animal() = default;
    virtual const char *make_sound() const = 0;
};

class Dog : public Animal {
public:
    const char *make_sound() const override {
        return "Woof";
    }
};

class Cat : public Animal {
public:
    const char *make_sound() const override {
        return "Meow";
    }
};

int main() {
    Dog dog;
    Cat cat;

    std::cout << dog.make_sound() << '\n';
    std::cout << cat.make_sound() << '\n';

    return 0;
}
پاسخ هوش مصنوعی Ai C & C++
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())

ارسال جواب

جوابت را با Markdown بنویس و ثبت کن

×
بستن