+ 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
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
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/
0
Thank you guys.
0
But my doubt is why it is not applicable in the previous version of c++