Skip to content

Instantly share code, notes, and snippets.

@ashwin
Created June 13, 2017 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashwin/970b18c10c885c0856b5beee1112050c to your computer and use it in GitHub Desktop.
Save ashwin/970b18c10c885c0856b5beee1112050c to your computer and use it in GitHub Desktop.
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