我正在嘗試使用C++繼承構造函數。我編譯並在gcc中運行以下程序,並且工作正常。C++中的繼承構造函數
#include<iostream>
using namespace std;
class Base1
{
public:
Base1()
{
cout<<"Base1 Default Constructor\n";
}
Base1(int i)
{
cout<<"Base1 parametrized Constructor\n";
}
};
class Base2
{
public:
Base2()
{
cout<<"Base2 Default Constructor\n";
}
Base2(const string& s)
{
cout<<"Base2 parametrized Constructor\n";
}
};
class Derived :public Base1, public Base2
{
public:
using Base1::Base1;
using Base2::Base2;
};
int main()
{
Derived d1(3); // OK
Derived d2("hello"); // OK
}
輸出:
Base1 parametrized Constructor
Base2 Default Constructor
Base1 Default Constructor
Base2 parametrized Constructor
但是,我想知道,爲什麼叫默認構造函數?