#include <iostream> #include <typeinfo> #include <string> #include <iomanip> using namespace std; class Simple { public: int s; public: Simple(){} setMem(int s_=0):s(s_){return s;} getMem(){return s;} Simple(Simple&s_):s(s_){} void print(){cout<<"Simple "<<s<<" is created."<<endl;} ~print(){cout<<"Simple "<<s<<" 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; }输出: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.请修改至正确
时间: 2024-03-29 20:41:40 浏览: 287
#include_iostream.h_
4星 · 用户满意度95%
#include <iostream>
#include <typeinfo>
#include <string>
#include <iomanip>
using namespace std;
class Simple {
public:
int s;
public:
Simple():s(0){} // 默认构造函数
Simple(int s_):s(s_){} // 带参构造函数
int setMem(int s_){ // 成员函数,设置 s 值
s = s_;
return s;
}
int getMem(){ // 成员函数,获取 s 值
return s;
}
Simple(const Simple& s_):s(s_.s){ // 拷贝构造函数
cout<<"Simple "<<s<<" is copied."<<endl;
}
void print(){ // 成员函数,输出信息
cout<<"Simple "<<s<<" is created."<<endl;
}
~Simple(){ // 析构函数
cout<<"Simple "<<s<<" is erased."<<endl;
}
};
int main() {
int m;
cin>>m;
Simple s1, s2(m), s3;
s3.setMem(m);
s3.print();
s3.setMem(m+1);
cout<<s3.getMem()<<endl;
Simple s4(s3);
s4.print();
return 0;
}
// 输出:
// Simple 0 is created.
// Simple 4 is created.
// Simple 0 is created.
// Simple 4 is copied.
// 5
// Simple 5 is created.
// Simple 5 is copied.
// Simple 5 is erased.
// Simple 4 is erased.
// Simple 0 is erased.
阅读全文