c++概述

来源:百度文库 编辑:神马文学网 时间:2024/06/12 22:31:53
1、虚函数
当程序发现虚函数名前的关键字virtual后,会自动将其作为动态联编处理,即在程序运行时动态地选择合适的成员函数。动态联编规定,只能通过指向基类的指针或基类对象的引用来调用虚函数,其格式:指向基类的指针变量名->虚函数名(实参表)或 基类对象的引用名. 虚函数名(实参表)

#include  class Cshape  { public: void SetColor( int color) { m_nColor=color;}  void virtual Display( void) { cout<<"Cshape"<void main()   {   Crectangle obRectangle;   Cshape * pShape[4]= & obRectangle ;   pShape[I]->Display( );   } }本程序运行结果:   Crectangle 2、纯虚函数  class <类名>  {  virtual <类型><函数名>(<参数表>)=0;  …  };  含有纯虚函数的类称为抽像类,抽像类不能定义对像。抽像类的派生类中必须要实现基类的纯虚函数,否则,派生类也会成为抽像类,也不能定义对像。class Graphic{   public:   }