【TOJ 5246】C++实验:赋值运算符函数

描述

实现一个C++类X,并编写构造函数、拷贝构造函数(Copy Constructor)和赋值运算符函数,使其能够输出样例信息。

主函数里的代码已经给出,请补充完整,提交时请勿包含已经给出的代码。

int main()
{
    int a;
    while(cin>>a)
    {
        X x1(a), x2(x1), x3;
        x3 = x2 = x1;
    }
    return 0;
}

输入

每组输入一个整数。

输出

每组输出调用相应函数后的结果。

样例输入

 1

样例输出

Constructor 1
Copy Constructor 1
Constructor 0
Assignment operator 1
Assignment operator 1

#include<iostream>
using namespace std;
class X{
public:
    int a;
    X(int a=0):a(a)
    {
        cout<<"Constructor "<<a<<endl;
    }
    X(const X&x)
    {
        a=x.a;
        cout<<"Copy Constructor "<<x.a<<endl;
    }
    X &operator=(const X&x)
    {
        a=x.a;
        cout<<"Assignment operator "<<a<<endl;
        return *this;
    }
};
int main()
{
    int a;
    while(cin>>a)
    {
        X x1(a), x2(x1), x3;
        x3 = x2 = x1;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/kannyi/p/9051364.html
今日推荐