山东科技大学OJ Problem G: Simple类

 

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;
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值