Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
virtual function
//g++ 5.4.0 #include <iostream> using std::cout ; using std::string; struct Base{ public: // virtual function virtual void func() { cout<<"Hello World\n"; } // pure virtual function virtual void say(string message) = 0; }; class A : public Base{ public: // override Base::func, and mark "final"(can not be overrride) void func() final{ cout<<"A func\n"; } // override Base::say void say(string message) override{ cout<<message<<'\n'; } // base class haven't "test" to override //void test() override; }; class B : public A{ string name; public: B(const char* name):name(name){} // A::func is marked "final" cannot be override //void func() override; // override A::say void say(string message) override{ cout<<name<<" say "<<message<<'\n'; } }; int main() { //Base base; // error: Base is abstract class Base* a = new A(); Base* b = new B("B"); a->func(); a->say(string("Hi")); b->say(string("Hi")); a->Base::func(); b->Base::func(); }
run
|
edit
|
history
|
help
0
ProPriceTemp
Heap sort baaaad
kadane's algorithm
Q
codeforces - 545D
C5P20
Days in month database using unordered_map
23
Hello World
PriQHotel