diff --git a/.gitignore b/.gitignore index fe0f2c7..62207b3 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ rtti future return-type-deduction division +poly diff --git a/CMakeLists.txt b/CMakeLists.txt index f31796c..b9b388d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/poly.cpp b/poly.cpp new file mode 100644 index 0000000..8e5c2dc --- /dev/null +++ b/poly.cpp @@ -0,0 +1,37 @@ +#include +#include + +/* abstract */ +class Animal { + public: + // virtual std::string talk() = 0; /* pure virtual */ + virtual std::string talk() { ; /* or an implementation */ + return ""; + } +}; + +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; +}