Created
May 11, 2016 20:23
-
-
Save csullivan/d0ff81666fcfdf56390178019bfd9c59 to your computer and use it in GitHub Desktop.
Illustrating the difference between virtual function overrides and non-virtual derived class methods with the same name and function signature
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> | |
using namespace std; | |
class Base { | |
public: | |
Base(){;} | |
virtual ~Base(){;} | |
void Func() { | |
cout << "Base::Func" << endl; | |
return; | |
} | |
virtual void vFunc() { | |
cout << "Base::vFunc" << endl; | |
return; | |
} | |
}; | |
class Derived : public Base { | |
public: | |
Derived(){;} | |
~Derived() override {;} | |
void Func() { | |
cout << "Derived::Func" <<endl; | |
return; | |
} | |
virtual void vFunc() override { | |
cout << "Derived::vFunc" <<endl; | |
return; | |
} | |
}; | |
int main() { | |
Derived* d = new Derived; | |
d->Func(); | |
d->vFunc(); | |
cout << endl; | |
Base* b = static_cast<Derived*>(d); | |
b->Func(); | |
b->vFunc(); | |
b->Base::vFunc(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment