Created
February 25, 2016 12:29
-
-
Save jaege/4beca299838f31a8ad3f to your computer and use it in GitHub Desktop.
Test key words override and final in virtual function declaration.
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 std::cout; using std::endl; | |
struct B { | |
virtual void f1() { cout << "B::f1() "; } | |
virtual void f2() { cout << "B::f2() "; } | |
virtual void f3() { cout << "B::f3() "; } | |
virtual void f6() final { cout << "B::f6() "; } | |
void f7() { cout << "B::f7() "; } | |
void f8() { cout << "B::f8() "; } | |
void f9() { cout << "B::f9() "; } | |
}; | |
struct D : B { | |
void f1() override { cout << "D::f1() "; } | |
void f2() final { cout << "D::f2() "; } | |
void f3() override final { cout << "D::f3() "; } // need not have override | |
// should have override, otherwise add new virtual function | |
virtual void f4() final { cout << "D::f4() "; } | |
//virtual void f5() override final; // Error, no virtual function in base class | |
//void f6(); // Error, override a final virtual function | |
void f7() { cout << "D::f7() "; } | |
virtual void f8() { cout << "D::f8() "; } | |
//void f9() override; // Error, override a nonvirtual function | |
}; | |
int main() { | |
B b; D d; | |
B *bp = &b, *bd = &d; D *dp = &d; | |
bp->f1(); bp->f2(); bp->f3(); bp->f6(); bp->f7(); bp->f8(); bp->f9(); cout << endl; | |
bd->f1(); bd->f2(); bd->f3(); bd->f6(); bd->f7(); bd->f8(); bd->f9(); cout << endl; | |
dp->f1(); dp->f2(); dp->f3(); dp->f6(); dp->f7(); dp->f8(); dp->f9(); cout << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment