| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
 本帖最后由 moc 于 2018-9-9 17:39 编辑  
 
1、纯虚函数和抽象类概念 
纯虚函数:一个在基类中说明的虚函数,在基类中没有定义,要求任何派生类都有自己的版本。 
纯虚函数的定义: 
        virtual 类型  函数名(参数表) = 0; 
抽象类: 一个含有纯虚函数的基类。 
纯虚函数为各派生类提供了一个公共的界面。 
- #include "iostream"
 
 - using namespace std;
 
  
- class figure   // 抽象类
 
 - {
 
 - public:
 
 -         virtual void show_area() = 0;  // 纯虚函数
 
 - protected: 
 
 -         double x;
 
 -         double y;
 
 - private:
 
 - };
 
  
- class tri :public figure
 
 - {
 
 - public:
 
 -         tri(double x = 1, double y = 2)
 
 -         {
 
 -                 this->x = x;
 
 -                 this->y = y;
 
 -         }
 
 -         virtual void show_area()
 
 -         {
 
 -                 cout << "三角形的面积:" << x*y*0.5 << endl;  // 子类实现父类纯虚函数定义的模板
 
 -         }
 
 - private:
 
 - private:
 
 - };
 
  
- class square :public figure
 
 - {
 
 - public:
 
 -         square(double x = 1, double y = 2)
 
 -         {
 
 -                 this->x = x;
 
 -                 this->y = y;
 
 -         }
 
 -         virtual void show_area()
 
 -         {
 
 -                 cout << "四边形的面积:" << x*y << endl;
 
 -         }
 
 - private:
 
 - private:
 
 - };
 
  
- // 通过抽象类的纯虚函数做  接口约定 (公共界面的约定)=》来实现具体的业务模型的填充
 
 - void objShow(figure* pBase)
 
 - {
 
 -         pBase->show_area();     // 多态
 
 - }
 
  
- int main06()
 
 - {
 
 -         //figure f1; // 不能使用抽象类创建对象
 
 -         figure *pBase = NULL;
 
 -         tri t1;
 
 -         square s1;
 
  
-         objShow(&t1);
 
 -         objShow(&s1);
 
 -         system("pause");
 
 -         return 0;
 
 - }
 
  复制代码 
抽象类注意事项: 
        ①抽象类不能实例化,但可以声明指针和引用; 
        ②抽象类不能作为返回类型和参数类型; 
        ③如果一个子类继承抽象类,必须实现抽象类里面的所有纯虚函数,才能实例化; 
        ④抽象类中也可以定义普通的成员函数和成员。 
2、抽象类 与 接口和协议 
        前文继承中提到的多继承,基本已经被实际的开发经验所抛弃,在真正的工程开发中这种多继承几乎不被使用。 
原因: 
        ①多继承带来的代码的复杂性要远多于其带来的便利; 
        ②多继承对代码的维护性上的影响是灾难性的; 
        ③在设计上,任何多继承都可以用单继承来替代。 
C++中是否有Java中接口的概念? 
        C++中没有接口的概念,C++是靠抽象类来实现接口的。 
接口类:类中只有纯虚函数原型,没有任何数据的定义。 
- class Interface1    // 接口类1
 
 - {
 
 - public:
 
 -         virtual void print() = 0;
 
 -         virtual int add(int i, int j) = 0;
 
 - };
 
  
- class Interface2     // 接口类2
 
 - {
 
 - public:
 
 -         virtual int add(int i, int j) = 0;
 
 -         virtual int minus(int i, int j) = 0;
 
 - };
 
  
- class parent  
 
 - {
 
 - public:
 
 -         int i;
 
 - };
 
 - class Child : public parent, public Interface1, public Interface2
 
 - {
 
 - public:
 
 -         void print()    //接口函数的实现
 
 -         {
 
 -                 cout << "Child::print" << endl;
 
 -         }
 
  
-         int add(int i, int j)   //接口函数的实现
 
 -         {
 
 -                 return i + j;
 
 -         }
 
  
-         int minus(int i, int j)   //接口函数的实现
 
 -         {
 
 -                 return i - j;
 
 -         }
 
 - };
 
  
- int main(int argc, char *argv[])
 
 - {
 
 -         Child c;
 
 -         c.print();
 
  
-         cout << c.add(3, 5) << endl;
 
 -         cout << c.minus(4, 6) << endl;
 
  
-         Interface1* i1 = &c;
 
 -         Interface2* i2 = &c;
 
  
-         cout << i1->add(7, 8) << endl;
 
 -         cout << i2->add(7, 8) << endl;
 
 -         system("pause");
 
 -         return 0;
 
 - }
 
  复制代码 
上面的对接口类的多继承不会带来二义性和复杂性问题。这种多重继承也是可以通过单继承和接口来代替。 
类接口只是一个功能的说明,而不是功能的实现。子类需要根据功能说明定义实现。 
 |   
 
 
 
 |