cpp第五次作业

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;
}

现象:

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值