c++多态:一个继承两个父类的子类构造函数调用的过程的顺序是怎么样的,下面为什么是这个调用顺序
#include<iostream>usingnamespacestd;classA{public:A(){cout<<"A"<<endl;}A(inti){cout<<...
#include<iostream>
using namespace std;
class A{
public:
A(){
cout<<"A"<<endl;
}
A(int i){
cout<<"A&"<<i<<endl;
}
~A(){
cout<<"~A"<<endl;
}
};
class B{
public:
B(){
cout<<"B"<<endl;
}
B(int i){
cout<<"B&"<<i<<endl;
}
~B(){
cout<<"~B"<<endl;
}
};
class C:public A, public B
{
public:
C(int a, int b, int c):A1(a),B(b){/*在A1(a)是A(a)时输出是:A&1,B&1,A,B,A&3,C&3,换成A1(a)后输出是:A,B&2,A&1,B,A&3,C&3*/
A2 = new A(c);
cout<<"C&"<<c<<endl;
}
~C(){
delete A2;
cout<<"~C"<<endl;
}
private:
A A1;
B B1;
A *A2;
int t;
};
int main(){
C(1,2,3);
return 0;
} 展开
using namespace std;
class A{
public:
A(){
cout<<"A"<<endl;
}
A(int i){
cout<<"A&"<<i<<endl;
}
~A(){
cout<<"~A"<<endl;
}
};
class B{
public:
B(){
cout<<"B"<<endl;
}
B(int i){
cout<<"B&"<<i<<endl;
}
~B(){
cout<<"~B"<<endl;
}
};
class C:public A, public B
{
public:
C(int a, int b, int c):A1(a),B(b){/*在A1(a)是A(a)时输出是:A&1,B&1,A,B,A&3,C&3,换成A1(a)后输出是:A,B&2,A&1,B,A&3,C&3*/
A2 = new A(c);
cout<<"C&"<<c<<endl;
}
~C(){
delete A2;
cout<<"~C"<<endl;
}
private:
A A1;
B B1;
A *A2;
int t;
};
int main(){
C(1,2,3);
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询