#include <iostream>
#include <string>
using namespace std; // Добавлено использование пространства имен std
class Animal {
protected:
string name;
int age;
public:
Animal(string n, int a);
virtual void makeSound();
void eat();
virtual void sleep();
};
class Mammal : protected Animal {
protected:
bool isSleeping; // Исправлено: добавлено поле isSleeping
public:
Mammal(string n, int a);
void makeSound() override; // Использование override для переопределения метода
void eat();
void sleep() override; // Использование override для переопределения метода
};
class Dog : public Mammal {
private:
bool isTailWagging;
public:
Dog(string n, int a);
void makeSound() override; // Использование override для переопределения метода
// void eat() override; // Использование override для переопределения метода
void sleep() override; // Использование override для переопределения метода
void fetch();
void wagTail();
};
class Cat : public Mammal {
private:
int numberOfLives;
public:
Cat(string n, int a);
void makeSound() override; // Использование override для переопределения метода
// void eat() override; // Использование override для переопределения метода
void sleep() override; // Использование override для переопределения метода
};
class Tail : public Dog {
public:
Tail(string n, int a);
};
// Реализация методов классов
Animal::Animal(string n, int a) {
name = n;
age = a;
}
void Animal::makeSound() {
cout << "This is a generic animal sound." << endl;
}
void Animal::eat() {
cout << "The animal is eating." << endl;
}
void Animal::sleep() {
cout << "The animal is sleeping." << endl;
}
Mammal::Mammal(string n, int a) : Animal(n, a) {
isSleeping = false;
}
void Mammal::makeSound() {
cout << "This is a generic mammal sound." << endl;
}
void Mammal::eat() {
cout << "The mammal is eating." << endl;
}
void Mammal::sleep() {
cout << "The mammal is sleeping." << endl;
isSleeping = true;
}
Dog::Dog(string n, int a) : Mammal(n, a) {
isTailWagging = false;
}
void Dog::makeSound() {
cout << "Woof!" << endl;
}
//void Dog::eat() {
// cout << "The dog is eating." << endl;
//}
void Dog::sleep() {
cout << "The dog is sleeping." << endl;
isSleeping = true;
}
void Dog::fetch() {
cout << "The dog is fetching." << endl;
}
void Dog::wagTail() {
if (isSleeping) {
cout << "The dog can't wag its tail because it's sleeping." << endl;
} else {
cout << "The dog is wagging its tail." << endl;
isTailWagging = true;
}
}
Cat::Cat(string n, int a) : Mammal(n, a) {
numberOfLives = 9;
}
void Cat::makeSound() {
cout << "Meow!" << endl;
}
//void Cat::eat() {
// cout << "The cat is eating." << endl;
//}
void Cat::sleep() {
cout << "The cat is sleeping." << endl;
isSleeping = true;
}
Tail::Tail(string n, int a) : Dog(n, a) {} // Исправлено: передача параметров в базовый класс
int main() {
Dog d("Fido", 3);
Cat c("Fluffy", 5);
Dog d1("Barky", 3);
Mammal *arr[] = {&d, &c, &d1};
// should woof, meow, woof
for (int i = 0; i < 3; i++){
arr[i]->makeSound();
}
// should eat in dog, cat, dog order
for (int i = 0; i < 3; i++){
arr[i]->eat();
}
// should sleep in dog, cat, dog order
for (int i = 0; i < 3; i++){
arr[i]->sleep();
}
// shouldn't do anything as they're sleeping
for (int i = 0; i < 3; i++){
arr[i]->makeSound();
}
// shouldn't eat actually, as they are sleeping
for (int i = 0; i < 3; i++){
arr[i]->eat();
}
// shouldn't wag tails, they are sleeping
// for (int i = 0; i < 3; i++){
// arr[i]->wagTail();
}
// Tail t("Taily", 2);
// t.makeSound();
//}