Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <memory>
- using namespace std;
- class Animal {
- public:
- Animal(const string& type) : type_(type) {}
- void Eat(const string& fruit) {
- cout << type_ << " eats " << fruit << endl;
- }
- virtual void Sound() const = 0;
- private:
- const string type_;
- };
- class Cat : public Animal {
- public:
- Cat() : Animal("cat") {}
- void Sound() const override {
- cout << "Meow!" << endl;
- }
- };
- class Dog : public Animal {
- public:
- Dog() : Animal("dog") {}
- void Sound() const override {
- cout << "Whaf!" << endl;
- }
- };
- class Parrot : public Animal {
- public:
- Parrot(const string& name) : Animal("parrot"), name_(name) {}
- void Sound() const override {
- cout << name_ << " is good!" << endl;
- }
- private:
- const string& name_;
- };
- class Horse : public Animal {
- public:
- Horse() : Animal("horse") {}
- void Sound() const override {
- cout << "some sound" << endl;
- }
- };
- void MakeSound(const Animal& a) {
- a.Sound();
- }
- int main() {
- vector<shared_ptr<Animal>> animals{
- make_shared<Cat>(),
- make_shared<Dog>(),
- make_shared<Parrot>("Kesha"),
- make_shared<Horse>(),
- };
- for (auto a : animals) {
- MakeSound(*a);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement