抽象类通常用定义方法就是该类中定义纯虚函数。  
  使用私有类型构造函数太常见了,其不是所谓抽象类。任何希望所有的对象都必须在堆中生成都会这样作。  
   
  抽象类主要用于接口,也可以理解为类的一种通讯交互方式。接口的概念在COM等高级运用中运用广泛。也是C++所谓的第二道门槛。  
  class   interfacehavefun  
  {  
        virtural   havefun()=0;  
  }  
  interfacehavefun就是一个纯virtural   类,你不能生成它的对象。但使用它的指针就可以指向它的子类。  
  也就是说这样是错误的。  
  interfacehavefun   tmp;//定义错误  
  interfacehavefun   *tmp   =   new   interfacehavefun();   //定义错误  
  class   A:public   interfacehavefun  
  {  
        havefun()       //不定义virtural   其也是virtural   函数  
        {  
                std::cout<<"A   have   fun"<<std::endl;  
        };  
  }  
   
  class   B:public   interfacehavefun  
  {  
        havefun()  
        {  
                std::cout<<"B   have   fun"<<std::endl;  
        };  
  }  
  你可以这样使用接口:  
  interfacehavefun   *tmp   =   new   A()  
  tmp->havefun()  
  结果:  
  A   have   fun  
   
  interfacehavefun   *tmp   =   new   B();  
  tmp->havefun();  
  B   have   fun  
   
  不要小看这种方法,熟练的使用它可以写出优美的程序。

相关文章:

  • 2022-01-03
  • 2021-12-11
  • 2021-11-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2022-12-23
猜你喜欢
  • 2021-09-25
  • 2022-12-23
  • 2021-08-28
  • 2022-12-23
  • 2021-09-30
  • 2021-08-19
  • 2021-04-22
相关资源
相似解决方案