基類析造函數是純虛函數時,則必須在中實現其方法

#include <iostream>


#include <string>


using std::cout;


using std::endl;


class AAA


{


public:


 AAA(const char c = 'a'):n(c)


 { cout << "new AAA\n"; }


 virtual char print() = 0;


    char printa()


 { return n; }


 virtual ~AAA() = 0;


protected:


 char n;


};


//AAA::~AAA() 


//{ cout << "delete AAA\n"; }


class BBB : public AAA


{


public:


 BBB(const char c ) :n(c)


 { cout << "new BBB\n"; }


 char print()


 { return n; }


 virtual ~BBB()


 { cout << "delete BBB\n";}


protected:


 char n;


};


int main()


 {


  // 基類是抽象類,子類必須實現基類中的純虛函數,


  // 基類,可以也無須實現自己的純虛函數,但析造函數是純虛函數時,則必須在基類中實現其方法


    BBB *bbb= new BBB('b');


 


    AAA *aaa = new BBB('r');


 cout << "BBB class " << bbb->print() << endl; //b


 cout << "BBB class" << aaa->printa() << endl; // a


 delete bbb;


 delete aaa; 


    //AAA a; // 不能實例化抽象類


    


 system("pause");


 return 0;


 }
發佈了14 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章