Const Data in C++

Once the const data members are initialized they can not be changed.Hence the known constants such as value of PI etc.that never change should be declared as const.

How to Initialize a const data member:
class sample
     const int t;
              sample(int i):t(i);
void main()
      sample s(25);

Here, t is the const data member of the class sample.When an object s is created,25 is passed to the one-argument constructor.It gets collected in parameter i which is then assigned to t.It is necessary to initialize the const data member in the definition of the constructor as shown in the program.

No comments:

Post a Comment