#include<iostream> using namespace std; class Cshape { public: void SetColor(int color) { m_nColor=color;} virtual void Display(void) {cout<<"Cshape"<<endl; } private: int m_nColor; }; class Crectangle:public Cshape { public: virtual void Display(void) {cout<<"Crectangle"<<endl; } }; class Ctriangle:public Cshape { public: virtual void Display(void) {cout<<"Ctriangle"<<endl; } }; class Cellipse :public Cshape { public: virtual void Display(void) {cout<<"Cellipse"<<endl ;} }; void main() { Cshape obShape; Cellipse obEllipse; Ctriangle obTriangle; Crectangle obRectangle; Cshape * pShape[4]= { &obShape,&obEllipse,&obTriangle,& obRectangle }; for(int i= 0; i< 4; i++) pShape[i]->Display( ); } 本程序运行结果: Cshape Cellipse Ctriangle Crectangle 如果把Cshape类里面virtual void Display(void) 中的virtual去掉的话 运行结果就不一样了: Cshape Cshape Cshape Cshape