Skip to content

Instantly share code, notes, and snippets.

@ybouhjira
Last active August 29, 2015 13:55
Show Gist options
  • Save ybouhjira/8773491 to your computer and use it in GitHub Desktop.
Save ybouhjira/8773491 to your computer and use it in GitHub Desktop.
Demo of virtual methods in cpp
#include <iostream>
#include <vector>
class Parent {
public:
virtual void say() {
std::cout << "I'm the parent" << std::endl;
}
};
class Child: public Parent {
public:
virtual void say() {
std::cout << "I'm the child" << std::endl;
}
};
int main() {
std::vector<Parent* > liste(2);
liste[0] = new Parent();
liste[1] = new Child();
for(int i = 0; i < liste.size(); i++)
liste[i]->say();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment