1.源码
#include <iostream>
using namespace std;
class A{
public:
int a;
A(int a):a(a){}
};
class B:virtual public A{
public:
int b;
B(int a,int b):A(a),b(b){cout<<"666"<<endl;}
};
class C:virtual public A{
public:
int c;
C(int a,int c):A(a),c(c){cout<<"777"<<endl;}
};
class D:public B,public C{
public:
int d;
D(int a,int b,int c,int d):A(a),B(a,b),C(a,c),d(d){
cout<<"888"<<endl;
}
D():A(1),B(a,1),C(a,1),d(1){
cout<<"666"<<endl;
}
};
int main()
{
D(1,1,1,1);
return 0;
}
现象:
2.源码:
#include <iostream>
using namespace std;
class Father
{
int *p;
const string name;
public:
//无参构造
Father():p(new int),name("wwww"){cout<<"father无参构造"<<endl;}
//有参构造
Father(int *p,const string &name):p(p),name(name){
cout<<"father有参构造"<<endl;
}
//析构函数
~Father(){
delete p;
}
//拷贝构造函数
Father(const Father &other):p(new int(*(other.p))),name(other.name){
cout<<"father拷贝构造"<<endl;
}
//拷贝赋值函数
};
class Son:public Father
{
int *age;
public:
//无参构造
Son():Father(new int,"wwww"),age(age){cout<<"son五参构造"<<endl;}
//有参构造
Son(int *p,const string &name,int *age):Father(new int,name),age(new int (*age)){
cout<<"son有参构造"<<endl;
}
//析构函数
~Son(){
delete age;
}
//拷贝构造函数
Son(const Father &other,int *age):Father(other),age(new int(*age)){}
//拷贝赋值函数
};
int main()
{
int age1=100;
Father();
Father f1(&age1,"name");
Father f2=f1;
return 0;
}
现象: