add another polymorphism example

master
neingeist 10 years ago
parent 9c89f7382a
commit 93ecf568d1

1
.gitignore vendored

@ -21,3 +21,4 @@ rtti
future
return-type-deduction
division
poly

@ -18,6 +18,7 @@ add_executable(classes classes.cpp)
add_executable(lvalues lvalues.cpp)
add_executable(bad_alloc bad_alloc.cpp)
add_executable(rtti rtti.cpp)
add_executable(poly poly.cpp)
add_executable(division division.cpp)
add_executable(future future.cpp)

@ -0,0 +1,37 @@
#include <iostream>
#include <string>
/* abstract */
class Animal {
public:
// virtual std::string talk() = 0; /* pure virtual */
virtual std::string talk() { ; /* or an implementation */
return "<sound>";
}
};
class Cat : public Animal {
std::string talk() {
return "Meow!";
}
};
class Dog : public Animal {
std::string talk() {
return "Woof!";
}
};
/* Note: need pointer or ref here! */
void letsHear(Animal& a) {
std::cout << a.talk() << std::endl;
}
int main() {
Cat c;
Dog d;
letsHear(c);
letsHear(d);
return 0;
}
Loading…
Cancel
Save