What is an example of the constructor | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is an example of the constructor

I'm not sure what a constructor looks like can you please point it out here class myClass { public: myClass(string nm) { setName(nm); } void setName(string x) { name = x; } string getName() { return name; } private: string name; }; int main() { myClass ob1("David"); myClass ob2("Amy"); cout << ob1.getName(); } //Outputs "David"

3rd Sep 2016, 11:50 PM
Jasmine
Jasmine - avatar
1 Answer
0
here myClass(string) is a constructor name of class and constructor is same and constructor do not have any return type.
6th Sep 2016, 7:24 PM
Shaurya Agnihotri