源程序:
#include <iostream>
using namespace std;
class A
{
public:
virtual void printMe()
{
cout<<"This is class A printing."<<endl;
}
} ;
class B:public A
{
public:
void printMe()
{
cout << "This is class B printing."<<endl;
}
} ;
class C:public B
{
public:
void printMe()
{
cout <<"This is class C printing."<<endl;
}
} ;
void print(A a)
{
a.printMe();
}
void main()
{
B b;
C c;
print(b);
print(c);
}
运行结果 :