Created
June 13, 2017 13:08
-
-
Save ashwin/970b18c10c885c0856b5beee1112050c to your computer and use it in GitHub Desktop.
Example program of virtual function 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
#include <iostream> | |
struct A | |
{ | |
virtual void do_something() {} | |
virtual void do_something2() { std::cerr << "In A\n"; } | |
}; | |
struct B : public A | |
{ | |
void do_something() {} | |
void do_something2() { std::cerr << "In B\n"; } | |
}; | |
int main() | |
{ | |
A* a = new B(); | |
a->do_something2(); | |
return 0; | |
} | |
// When executed, this program will print: | |
// $ ./a.out | |
// In B |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment