1. 程式人生 > >C++入門經典-例8.6-多重繼承的構造順序

C++入門經典-例8.6-多重繼承的構造順序

close splay alt com img pub 被調用 namespace lap

1:單一繼承是先調用基類的構造函數,然後調用派生類的構造函數,但多重繼承將如何調用構造函數呢?多重繼承中的基類構造函數被調用的順序以派生表中聲明的順序為準。派生表就是多重繼承定義中繼承方式後面的內容,調用順序就是按照基類名標識符的前後順序進行的。

2:代碼如下:

技術分享
// 8.6.cpp : 定義控制臺應用程序的入口點。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
class CBicycle
{
public:
    CBicycle()
    {
        cout << "Bicycle Construct
" << endl; } CBicycle(int iWeight) { m_iWeight=iWeight; } void Run() { cout << "Bicycle Run" << endl; } protected: int m_iWeight; }; class CAirplane { public: CAirplane() { cout << "Airplane Construct " << endl; }; CAirplane(
int iWeight) { m_iWeight=iWeight; } void Fly() { cout << "Airplane Fly " << endl; } protected: int m_iWeight; }; class CAirBicycle : public CBicycle, public CAirplane { public: CAirBicycle() { cout << "CAirBicycle Construct"
<< endl; } void RunFly() { cout << "Run and Fly" << endl; } }; void main() { CAirBicycle ab; ab.RunFly(); }
View Code

運行結果:

技術分享

C++入門經典-例8.6-多重繼承的構造順序