下面程序输出结果为 B() is called! D1() is called! D2() is called! D12() is called! 请将程序填写完整 #include <iostream> using namespace std; class B { public: B(){cout<<"B() is called!"<<endl;} }; class D1: (1) { public: D1(){cout<<"D1() is called!"<<endl;} }; class D2: (2) { public: D2(){cout<<"D2() is called!"<<endl;} }; class D12: (3) { public: D12(){cout<<"D12() is called!"<<endl;} }; int main() { D12 d12; return 0; }
时间: 2024-03-24 17:39:16 浏览: 307
华硕 Z9NR-D12驱动程序下载
下面是程序的完整代码:
```
#include <iostream>
using namespace std;
class B {
public:
B(){cout<<"B() is called!"<<endl;}
};
class D1: public B {
public:
D1(){cout<<"D1() is called!"<<endl;}
};
class D2: public B {
public:
D2(){cout<<"D2() is called!"<<endl;}
};
class D12: public D1, public D2 {
public:
D12(){cout<<"D12() is called!"<<endl;}
};
int main() {
D12 d12;
return 0;
}
```
程序中,类B是基类,类D1和D2是公共继承自B的派生类,类D12是多重继承自D1和D2的派生类。在派生类D12的构造函数中,先调用D1和D2的构造函数,再调用自己的构造函数,因此输出结果为 B() is called! D1() is called! D2() is called! D12() is called!
阅读全文