+ 2

why we need const keyword...explain with exemple plzzzzz...

13th Sep 2016, 9:42 AM
Bint E Asif
Bint E Asif - avatar
5 odpowiedzi
+ 1
To make sure we don't erase the value by mistake.
13th Sep 2016, 10:06 AM
Zen
Zen - avatar
+ 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.
13th Sep 2016, 11:04 AM
stuart dowsett
stuart dowsett - avatar
+ 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
13th Sep 2016, 1:05 PM
Twinkle Baisane
Twinkle Baisane - avatar
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.
14th Aug 2017, 3:55 PM
Adam
Adam - avatar
- 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.
13th Sep 2016, 10:22 AM
MarcinZx