using namespace std;
/*
多繼承的優缺點
實際生活中,一些事物往往會擁有兩個或兩個以上事物的屬性,爲了解決這個問題,C++引入了多重繼承的概念。
多重繼承的優點是對象可以調用多個基類中的接口。
多重繼承的缺點是容易出現繼承向上的二義性*/
class A
{
public:
A(int a):m_a(a){ // 由子類進行初始化,提供一個帶參數的構造函數
cout << m_a << endl;
}
void show(){ cout << "A running " << endl; }
void show1(){ cout << "A running " << endl; }
private:
int m_a;
};
class B
{
public:
B(int b) :m_b(b){
cout << m_b << endl;
}
void show(){ cout << "B running " << endl; }
void show2(){ cout << "B running " << endl; }
private:
int m_b;
};
class C :public A, public B
{
public :
C(int c_1, int c_2,int c_3) :A(c_1), B(c_2),m_c(c_3){
cout << m_c << endl;
}
void show(){ cout << "A running " << endl; }
void show3(){ cout << "C running " << endl; }
private :
int m_c;
};
int main()
{
C c(1,2,3);
c.show(); //如果基類和派生類中都聲明瞭show函數,則調用第一個基類(聲明順序)中的函數,若派生類中沒有定義show函數,則產生錯誤,二義性
c.show1();
c.show2();
c.show3();
return 0;
}