Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example program of virtual function in C++
#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
You can’t perform that action at this time.