How to initialize data members using a constructor in a c++ program? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to initialize data members using a constructor in a c++ program?

if the constructor is initialized within public of a class and the input of data members of the class is being done at the main function. what is the right way to do it? an example with a program would be helpful.

11th May 2017, 4:12 AM
Soham Biswas
Soham Biswas - avatar
4 Answers
+ 8
myClass(string newName) : name(newName) { } is also acceptable.. (Using everything else from the example provided by luka)
11th May 2017, 4:48 AM
jay
jay - avatar
+ 5
naww i like the () way it seems alot cleaner... dang standards lol
11th May 2017, 8:49 AM
jay
jay - avatar
+ 4
in c++11 we use these {} for initialising, but it's also okay to use (), tho it's not standard anymore
11th May 2017, 8:48 AM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
+ 3
thanks everyone for helping out.
11th May 2017, 3:59 PM
Soham Biswas
Soham Biswas - avatar