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