You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
719 B
C++
42 lines
719 B
C++
11 years ago
|
#include <iostream>
|
||
|
#include <vector>
|
||
|
|
||
|
class Animal {
|
||
|
public:
|
||
|
// Note: using *virtual* does the difference here!
|
||
|
virtual void makeSound() {
|
||
|
std::cout << "<generic animal sound>" << std::endl;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class Cow : public Animal {
|
||
|
public:
|
||
|
void makeSound() {
|
||
|
std::cout << "Mooh." << std::endl;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class Cat : public Animal {
|
||
|
public:
|
||
|
void makeSound() {
|
||
|
std::cout << "Meow?" << std::endl;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
int main() {
|
||
|
Cat cat;
|
||
|
Cow cow;
|
||
|
|
||
|
// Does nothing:
|
||
|
std::vector<Animal> animals = { cat, cow };
|
||
|
for (auto &a: animals) {
|
||
|
a.makeSound();
|
||
|
}
|
||
|
|
||
|
// Meow? Mooh.
|
||
|
std::vector<Animal*> animalptrs = { &cat, &cow };
|
||
|
for (auto &a: animalptrs) {
|
||
|
a->makeSound();
|
||
|
}
|
||
|
}
|