Last active
August 29, 2015 13:55
-
-
Save ybouhjira/8773491 to your computer and use it in GitHub Desktop.
Demo of virtual methods in cpp
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
#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