Pages

2010年12月31日 星期五

C++多重繼承

下面為C++多重繼承的網路經典範例。

  1. //程序作者:管寧
  2. //站點:www.cndev-lab.com
  3. //所有稿件均有版權,如要轉載,請務必著名出處和作者
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. class Vehicle {
  9. public:
  10.     Vehicle(int weight = 0) {
  11. Vehicle::weight = weight;
  12.         cout << "載入Vehicle類構造函數" << endl;
  13.     }
  14.     void SetWeight(int weight) {
  15.         cout << "重新設置重量" << endl;
  16. Vehicle::weight = weight;
  17.     }
  18.     virtual void ShowMe() = 0;
  19. protected:
  20.     int weight;
  21. };
  22.  
  23. class Car: virtual public Vehicle//汽車,這裡是虛擬繼承
  24. {
  25. public:
  26.     Car(int weight = 0, int aird = 0) : Vehicle(weight) {
  27.         Car::aird = aird;
  28.         cout << "載入Car類構造函數" << endl;
  29.     }
  30.     void ShowMe() {
  31.         cout << "我是汽車!" << endl;
  32.     }
  33. protected:
  34.     int aird;
  35. };
  36.  
  37. class Boat: virtual public Vehicle//船,這裡是虛擬繼承
  38. {
  39. public:
  40.     Boat(int weight = 0, float tonnage = 0) : Vehicle(weight) {
  41. Boat::tonnage = tonnage;
  42.         cout << "載入Boat類構造函數" << endl;
  43.     }
  44.     void ShowMe() {
  45.         cout << "我是船!" << endl;
  46.     }
  47. protected:
  48.     float tonnage;
  49. };
  50.  
  51.  
  52.  
  53. class AmphibianCar: public Car, public Boat//水陸兩用汽車,多重繼承的體現
  54. {
  55. public:
  56.     AmphibianCar(int weight, int aird, float tonnage) : Vehicle(weight), Car(weight, aird), Boat(weight, tonnage)
  57.     //多重繼承要注意調用基類構造函數
  58.     {
  59.         cout << "載入AmphibianCar類構造函數" << endl;
  60.     }
  61.     void ShowMe() {
  62.         cout << "我是水陸兩用汽車!" << endl;
  63.     }
  64.     void ShowMembers() {
  65.     cout << " 重量:" << weight << "頓," << "空氣排量:" << aird << "CC," << "排水量:" << tonnage << " 頓" << endl;
  66.     }
  67. };
  68. int main() {
  69.     AmphibianCar a(4, 200, 1.35f);
  70.     a.ShowMe();
  71.     a.ShowMembers();
  72.     a.SetWeight(3);
  73.     a.ShowMembers();
  74.     system("pause");
  75.     return 0;
  76. }


  1. 載入Vehicle類構造函數
  2. 載入Car類構造函數
  3. 載入Boat類構造函數
  4. 載入AmphibianCar類構造函數
  5. 我是水陸兩用汽車!
  6.  重量:4頓,空氣排量:200CC,排水量:1.35
  7. 重新設置重量
  8.  重量:3頓,空氣排量:200CC,排水量:1.35
  9. 請按任意鍵繼續 . . .
但是C++多重繼承有個問題是當繼承的方法或變數有相同的時候,到底要用哪種呢?

是否有方法可以直接指定要用哪個繼承的方法?

  1. #include <iostream>
  2. using namespace std;
  3. class AA {
  4. public:
  5.     AA() {
  6.     }
  7.     void fat() {      
  8. printf("Addidas is Fat\n");
  9.     }
  10. };
  11.  
  12. class BB {
  13. public:
  14.     BB() {
  15.     }
  16.     void fat() {
  17. printf("Bill is Fat\n");
  18.     }
  19. };
  20.  
  21.  
  22.  
  23. class CC: public AA, public BB {
  24. public:
  25.     using AA::fat;
  26.     CC() {
  27.  
  28.     }
  29. };
  30.  
  31. class DD: public AA, public BB {
  32. public:
  33.     DD() {
  34.     }
  35. };
  36.  
  37. int main() {
  38.     CC ca;
  39.     printf("C is call..: ");
  40.     ca.fat();
  41.     DD da;
  42.     printf("D is call..: \n");
  43.     da.AA::fat();
  44.     da.BB::fat();
  45.     return 0;
  46. }


  1. C is call..: Addidas is Fat
  2. D is call..:
  3. Addidas is Fat
  4. Bill is Fat
  5.  
由此可知,AA::fat() using AA::fat 皆可指定繼承的哪個方法會被使用。

0 意見: