【问题描述】
改正下列程序的错误,使得程序能够正确运行。
#include <iostream>
#include <string>
using namespace std;
class MyStr
{
public:
MyStr()=default;
MyStr(const string s):s(s)
{}
MyStr(const MyStr& s):s(s.s)
{}
string str()
{
return s;
}
~MyStr()=delete;
private:
string s;
};
MyStr getString()
{
return MyStr("hello");
}
int main()
{
MyStr a("Hello");
MyStr b("World");
cout<<a.str()<<endl;
cout<<b.str()<<endl;
MyStr &c = getString();
cout<<c.str()<<endl;
MyStr &&d = b;
cout<<d.str()<<endl;
}
【输入形式】
无
【输出形式】
Hello
World
hello
World
【样例输入】
【样例输出】
【样例说明】
【评分标准】
改正后:
#include <iostream>
#include <string>
using namespace std;
class MyStr
{
public:
MyStr()=default;
MyStr(const string s):s(s)
{}
MyStr(const MyStr& s):s(s.s)
{}
string str()
{
return s;
}
private:
string s;
};
MyStr getString()
{
return MyStr("hello");
}
int main()
{
MyStr a("Hello");
MyStr b("World");
cout<<a.str()<<endl;
cout<<b.str()<<endl;
MyStr c = getString();
cout<<c.str()<<endl;
MyStr &d = b;
cout<<d.str()<<endl;
}