B. 对象是怎样构造的(拷贝构造函数)

题目描述

某个类包含一个整型数据成员.程序运行时若输入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

按照题目意思写即可 

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include <iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<queue>
#include<set>
using namespace std;

class def
{
	int v = 0;
public:
	def()
	{
		v = 0;
	}
	def(int can)
	{
		cout << "Constructed using one argument constructor, value = " << can << endl;
		v = can;
	}
};

int main()
{
	int t,num,can;
	cin >> t;
	while (t--)
	{
		cin >> num;
		if (num == 0)
		{
			def a();
			cout << "Constructed by default, value = 0" << endl;
		}
		else if (num == 1)
		{
			cin >> can;
			def a(can);
		}
		else if(num==2)
		{
			cin >> can;
			def a(can);
			def f = a;
			cout << "Constructed using copy constructor, value = " << can << endl;
		}
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ZZZWWWFFF_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值