0
53.2 Practice - The Ninth Wave
Constructors 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 *The constructor should take one string as the argument and output it. My code is as below but the error message comes out c++ forbids painting⊠please help
4 Answers
+ 2
+ 1
Your constructor should be named after the class name, but in your class definition, you have 'painting' instead of 'Painting'. Notice the first letter is different.
And to have the constructor to print its only argument, you print the <nm> argument after the call to setName() method.
Painting( string nm )
{
setName( nm );
std::cout << nm << std::endl;
}
0
#include <iostream>
#include <string>
using namespace std;
class Painting {
public:
//define the constructor
painting(string nm) {
setName(nm);
}
void setName(string x) {
name = x;
}
string getName() {
return name;
}
private:
string name;
};
int main() {
string name;
cin >> name;
Painting painting(name);
return 0;
}
0
Thanks Ipang !