+ 1

Why can't we initialize the data members of the class with some initial values at the time of declaration

Initialization of data members of a class

13th Apr 2020, 4:42 PM
Digvijay Singh Rathore
Digvijay Singh Rathore - avatar
6 odpowiedzi
+ 1
Hi. Here is example of member initialization. Do you mean this or I misunderstand you? https://code.sololearn.com/chAecHy262JP/?ref=app
13th Apr 2020, 4:53 PM
Stephan
Stephan - avatar
0
use the constructor method. #include <iostream> using namespace std; class MyClass{ public: int value; //This is the constructor, it has the class name, is public and doesn't have a return type. MyClass(int val){ value=val; //or this->value=val; } }; int main() { MyClass object(5); cout<<object.value; return 0; } https://www.sololearn.com/learn/CPlusPlus/1715/
13th Apr 2020, 4:56 PM
John Robotane
John Robotane - avatar
0
Thank you guys.
14th Apr 2020, 2:24 AM
Digvijay Singh Rathore
Digvijay Singh Rathore - avatar
0
But my doubt is why it is not applicable in the previous version of c++
14th Apr 2020, 2:25 AM
Digvijay Singh Rathore
Digvijay Singh Rathore - avatar