A virtual function is a special type of function that resolves to the most-derived version of the function with the same signature.for the virtual function , the derived class can provide self virtual function to instead base class or derive the virtual function from base class.
for example :
class Base{protected:public: virtual const char* GetName() { return "Base"; }};class Derived: public Base{public: virtual const char* GetName() { return "Derived"; }};int main(){ Derived cDerived; Base &rBase = &cDerived; cout << "rBase is a " << rBase.GetName() << endl; return 0;}
文章標籤
全站熱搜
