Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 628 Solved: 305
[Submit][Status]
Description
定义Simple类,只有一个int类型的数据成员,有构造函数,析构函数和拷贝构造函数以及如下成员函数:
1.setMem(int):设置数据成员的值,并返回当前对象。
2.getMem():返回数据成员的值。
3.print():输出数据成员的值。
实现该类,使得程序能正确运行且输出所要求的结果。
Input
一个整数。
Output
见样例。
Sample Input
4
Sample Output
Simple 0 is created.
Simple 4 is created.
Simple 0 is created.
4
5
Simple 5 is copied.
5
Simple 5 is erased.
Simple 5 is erased.
Simple 4 is erased.
Simple 0 is erased.
HINT
Append Code
append.cc,
[Submit][Status]
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Simple{
public:
int a;
Simple(int n=0):a(n){cout<<"Simple "<<a<<" is created."<<endl;}
Simple &setMem(int n)
{
a=n;
return *this;
}
Simple(const Simple &p){a=p.a;cout<<"Simple "<<a<<" is copied."<<endl;}
int getMem(){return a;}
void print(){cout<<a<<endl;}
~Simple(){cout<<"Simple "<<a<<" is erased."<<endl;}
};
int main()
{
int m;
cin>>m;
Simple s1, s2(m);
Simple s3;
s3.setMem(m).print();
s3.setMem(m+1);
cout<<s3.getMem()<<endl;
Simple s4(s3);
s4.print();
return 0;
}