64,685
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class Father
{
public:
void f()
{
beCalled();
}
virtual ~Father()
{
}
private:
virtual void beCalled()
{
cout<<"Father called"<<endl;
}
};
class Child : public Father
{
public:
virtual ~Child()
{
}
private:
virtual void beCalled()
{
cout<<"Child called"<<endl;
}
};
int main()
{
Father *p=new Child();
p->f(); //Child
Child a;
a.f(); //Child
delete p;
//////////////////////////////////////////////////////////////////////////
p=new Father;
p->f(); //Father
Father b;
b.f(); //Father
delete p;
return 0;
}
class Father
{
public:
virtual void beCalled() = 0;
virtual f(){
beCalled();
}
};
class Child : public Father
{
public:
/*reimpl*/
virtual void beCalled() {
//do some stuff.
}
};