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

19th Dec 2021, 12:27 AM
Chin Eu
Chin Eu - avatar
4 Answers
+ 2
29th Mar 2022, 7:37 AM
Kshitiz Prabhakar Gurudu
Kshitiz Prabhakar Gurudu - avatar
+ 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; }
19th Dec 2021, 12:55 AM
Ipang
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; }
19th Dec 2021, 12:27 AM
Chin Eu
Chin Eu - avatar
0
Thanks Ipang !
19th Dec 2021, 6:50 AM
Chin Eu
Chin Eu - avatar