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:
- string name;
- Animal (const std::string& givenName) : name(givenName) {
- }
- string speak () {
- return "";
- }
- };
- class Dog: public Animal {
- public:
- Dog (const std::string& givenName) : Animal (givenName) {
- }
- string speak ()
- { return "Woof, woof!"; }
- };
- class Cat: public Animal {
- public:
- Cat (const std::string& givenName) : Animal (givenName) {
- }
- string speak ()
- { return "Meow..."; }
- };
- int main() {
- std::vector<std::unique_ptr<Animal>> animals;
- animals.push_back( new Dog("Skip") );
- animals.push_back( new Cat("Snowball") );
- for( int i = 0; i< animals.size(); ++i ) {
- cout << animals[i]->name << " says: " << animals[i]->speak() << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement