Why you do not initialize data members when you write a class?

(Last Updated On: July 12, 2015)

Answer: When we declare/write a class, there is no memory allocation happens for data members of a class, so, we cannot store data into data members.

The memory gets allocated for data members only when we create objects of a class. So, initialization of the variable happens once object of the class get created.


In the below example, initialization of data member int a=2; cannot be done at the time of declaration.