#include <iostream>
using namespace std;
class X {
int n;
public:
X(): n(100) { }
int f() { return n; }
virtual void g()const 【】
};
class Y : public X {
public:
void g()const { cout << "Y" << endl; }
};
int main()
{
X* p(new Y);
p->g();
delete p;
return 0;
}
只需要写 {}就好了
基类定义了虚函数,子类可以重写该函数,当子类重新定义了父类的虚函数后,父类指针根据赋给它的不同的子类指针,动态地调用属于子类的该函数,