Created
October 30, 2011 12:17
-
-
Save fhd/1325846 to your computer and use it in GitHub Desktop.
Duck typing in C++
This file contains 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
class Duck { | |
public: | |
void fly() | |
{ | |
... | |
} | |
void swim() | |
{ | |
... | |
} | |
}; | |
class Penguin { | |
public: | |
void swim() | |
{ | |
... | |
} | |
void slide() | |
{ | |
... | |
} | |
}; | |
template<T> | |
void do_stuff_to_a_bird(T bird) | |
{ | |
bird.fly(); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
Duck duck; | |
Penguin penguin; | |
do_stuff_to_a_bird(duck); | |
do_stuff_to_a_bird(penguin); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment