//子类在重写虚函数时,会覆盖父类的函数 #include <iostream> using namespace std; class B { public: B() { cout<<"Create B!"<<endl; } public: virtual void fun() { cout<<"B::fun()"<<endl; } virtual void show() { cout<<"B::show()"<<endl; } void print() { cout<<"B::print()"<<endl; } }; class D : public B { public: D() { cout<<"Create D!"<<endl; } public: void fun() { cout<<"D::fun()"<<endl; } void show() { cout<<"D::show()"<<endl; } virtual void list() { cout<<"D::list()"<<endl; } }; void main() { D d; B *p=&d; p->fun(); p->show(); } <img src="http://img.blog.csdn.net/20150512213921986?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZG91ZG91d2ExMjM0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
时间: 2024-10-29 19:09:56