The
data members
,state or properties whose value can not be changed during execution time and require value at time upon intialization .
You can not leave const memmber uninitialized,otherwise it will cause error!
- Initializing at the time of declaration. But keep in mind that some compilers do not allow initialization of 'const' in this way.
2)Initializing in Initializer list
of constructors
#include<iostream>
using namespace std;
class Student
{
String name;
// 1) initializing const member at time of creation
const int Id=1;
const double CNIC;
public:
// 2)initializing const member in member initialization list
Student():CNIC(123-456789-123) {}
};