Skip to content

Instantly share code, notes, and snippets.

@kallepersson
Last active December 10, 2015 21:18
Show Gist options
  • Save kallepersson/4494149 to your computer and use it in GitHub Desktop.
Save kallepersson/4494149 to your computer and use it in GitHub Desktop.
#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