C++ const Keyword
Example
Create a variable that cannot be changed:
const int myNum = 15;
// myNum = 10; // Error, because myNum is const
cout << myNum;
Definition and Usage
The const keyword makes a variable constant, which means unchangeable and read-only. A const variable must get a value when it is created.
const can also be used on a function parameter, so the function cannot change it. After a class method, like void printName() const, it means that the method cannot change the attributes of the object.
Related Pages
Read more about constants in our C++ Constants Tutorial.