The given program defines a Painting class and creates an object using the constructor.
Fix the code so that the constructor takes the name as the argument and outputs it.
Sample Input
Ocean
Sample Output
Ocean
//my code cpp
#include <iostream>
#include <string>
using namespace std;
class Painting {
public:
Painting(string nm) {
setName(nm);
}
void setName(string x) {
name = x;
cout<<name;
}
string getName() {
return name;
}
private:
string name;
};
int main() {
string name;
cin >> name;
Painting painting(name);
return 0;
}

这段代码定义了一个名为Painting的类,并通过构造函数接收输入的名字。当创建对象并设置名字时,程序会直接输出这个名字。在main函数中,从标准输入读取名字,然后创建一个Painting对象。
1445

被折叠的 条评论
为什么被折叠?



