定义三个类A,B,C,其中B继承于A,C继承于B,都包含f(),用于表明当前执行的是哪个类的f()
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class A
{
public:
A(){cout<<"create A"<<endl;}
virtual ~A(){cout<<"conustructing A"<<endl;}
virtual void f(){cout<<"it's A "<<endl;}
};
class B:public A
{
public:
B(){cout<<"Create B"<<endl;}
virtual~B(){cout<<"constructing B"<<endl;}
virtual void f(){cout<<"it's B"<<endl;}
};
class C:public B
{
public:
C(){cout<<"Create C"<<endl;}
~C(){cout<<"constructing C"<<endl;}
void f(){cout<<"it's C"<<endl;}
};
int main()
{
A a, *p;
B b ,*q;
C c ,*r;
p=&a;
q=&b;
r=&c;
p->f();
q->f();
r->f();
return 0;
}