Skip to content

Instantly share code, notes, and snippets.

@nicolekorch
Created November 19, 2025 18:04
Show Gist options
  • Select an option

  • Save nicolekorch/af353788ab9065e9369a05bc5f8d4cbd to your computer and use it in GitHub Desktop.

Select an option

Save nicolekorch/af353788ab9065e9369a05bc5f8d4cbd to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
using namespace std;
class Pet {
protected:
string name;
public:
Pet(string n) : name(n) {}
virtual void Sound() = 0;
virtual void Show() = 0;
virtual void Type() = 0;
virtual ~Pet() {}
};
class Dog : public Pet {
public:
Dog(string n) : Pet(n) {}
void Sound() { cout << "Гав" << endl; }
void Show() { cout << name << endl; }
void Type() { cout << "Собака" << endl; }
};
class Cat : public Pet {
public:
Cat(string n) : Pet(n) {}
void Sound() { cout << "Мяу" << endl; }
void Show() { cout << name << endl; }
void Type() { cout << "Кошка" << endl; }
};
class Parrot : public Pet {
public:
Parrot(string n) : Pet(n) {}
void Sound() { cout << "Кар" << endl; }
void Show() { cout << name << endl; }
void Type() { cout << "Попугай" << endl; }
};
class Hamster : public Pet {
public:
Hamster(string n) : Pet(n) {}
void Sound() { cout << "Писк" << endl; }
void Show() { cout << name << endl; }
void Type() { cout << "Хомяк" << endl; }
};
int main() {
Pet* pets[4] = {
new Dog("Шарик"),
new Cat("Мурка"),
new Parrot("Кеша"),
new Hamster("Бобик")
};
for (int i = 0; i < 4; i++) {
pets[i]->Show();
pets[i]->Type();
pets[i]->Sound();
cout << endl;
}
for (int i = 0; i < 4; i++) delete pets[i];
return 0;
}
@nicolekorch
Copy link
Author

бобик

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment