【10分】A. 对象是怎样构造的(拷贝构造函数)

题目描述
某个类包含一个整型数据成员.程序运行时若输入0表示用缺省方式定义一个类对象;输入1及一个整数表示用带一个参数的构造函数构造一个类对象;输入2及一个整数表示构造2个类对象,一个用输入的整数构造,另一个用前一个对象构造。试完成该类的定义和实现。

输入
测试数据的组数 t
第一组数
第二组数

输出
第一个对象构造输出
第二个对象构造输出

输入样例1

3
0
1 10
2 20

输出样例1

Constructed by default, value = 0
Constructed using one argument constructor, value = 10
Constructed using one argument constructor, value = 20
Constructed using copy constructor, value = 20

代码

#include <iostream>
using namespace std;

class Construction
{
private:
    int x;

public:
    Construction()
    {
        x = 0;
        cout << "Constructed by default, value = 0" << endl;
    }

    Construction(int num)
    {
        x = num;
        cout << "Constructed using one argument constructor, value = " << x << endl;
    }

    Construction(const Construction &con)
    {
        x = con.x;
        cout << "Constructed using copy constructor, value = " << x << endl;
    }

};



int main()
{
    int t;
    cin >> t;
    while(t --)
    {
        int temp;
        cin >> temp;
        if(temp == 0)  
        {
            Construction mycon;
        }
        else if(temp == 1)
        {
            int num;
            cin >> num;
            Construction mycon(num);
        }
        else
        {
            int num;
            cin >> num;
            Construction mycon1(num);
            Construction mycon2(mycon1);
        }
    }
    return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叫我胡萝北

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值