Created
November 21, 2016 13:51
-
-
Save ashwin/a7212fbf5fc0152dab11cf9a5460f694 to your computer and use it in GitHub Desktop.
Example of using override and final 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> | |
// Virtual method declarations | |
struct A | |
{ | |
virtual void Foo() const; | |
}; | |
struct B : public A | |
{ | |
void Foo() const override; | |
}; | |
struct C : public B | |
{ | |
void Foo() const final; | |
}; | |
// Virtual method definitions | |
void A::Foo() const | |
{ std::cout << "A:foo()\n"; } | |
void B::Foo() const | |
{ std::cout << "B::foo()\n"; } | |
void C::Foo() const | |
{ std::cout << "C::foo()\n"; } | |
int main() | |
{ | |
C c; | |
A *a = &c; | |
a->Foo(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment