+ 2
why we need const keyword...explain with exemple plzzzzz...
5 Respostas
+ 1
To make sure we don't erase the value by mistake.
+ 1
const int a =4
int b = 4
a = 5
b=5
cout<< a << ", " << b << endl
.....
4, 5
a will still be 4 whilst b will be 5.
const keyword keeps the integer a constant and therefore can't be changed.
+ 1
for example there r variable PI ,PI's value will be same 3.14 in full of program ,it will not change
,so we can declare PI as constant variable.
Constant means the value will fix in full of program
0
Its just a keyword, telling that the variable that is following is a constant and that means you can't change its value after you define it once.. Not like the Normal variable which you can change its value anytime you want.. So it's a keyword to differentiate constant variable from the normal variable.
- 1
There are some tricks with constant, const pointers, pointers which point at const value, you will get it in some time. When you have a class which have a const member you can set the value of it in initialization list which is call before constructor of a class, then value cannot be changed.