Last active
December 10, 2015 21:18
-
-
Save kallepersson/4494149 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <iostream> | |
| using namespace std; | |
| //dog.h | |
| class Dog { | |
| public: | |
| Dog(string name); | |
| string say_hello(); | |
| private: | |
| string name; | |
| }; | |
| //dog.cpp | |
| Dog::Dog(string dogName) { | |
| name = dogName; | |
| }; | |
| string Dog::say_hello() { | |
| return "Woff, I am " + name; | |
| }; | |
| //cat.h | |
| class Cat { | |
| public: | |
| Cat(string name); | |
| string say_hello(); | |
| private: | |
| string name; | |
| }; | |
| //cat.cpp | |
| Cat::Cat(string catName) { | |
| name = catName; | |
| }; | |
| string Cat::say_hello() { | |
| return "Miaou, I am " + name; | |
| }; | |
| //main.cpp | |
| template <class T> | |
| string helloAnimal (T * entity) { | |
| return (string) entity->say_hello(); | |
| } | |
| int main(int argc, const char * argv[]) | |
| { | |
| Dog d("Foo"); | |
| Cat c("Barry"); | |
| cout << helloAnimal<Dog>(&d) << endl; | |
| cout << helloAnimal<Cat>(&c) << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment