If you want to make a data member of class to be constant data member, then you can use the const keyword before its data member declaration. The const data members must be initialized by constructor in initialization List otherwise compiler throw an error. Once initialized, const data member value cannot be changed. Example:When we create a class of stack, we want to limit the number of entries for stack object. For that, we can use one const data member which can be used for validating the stack full scenario.…
Tag: C++ Const Keyword
const member function
const member function does not allow to modify nonstatic data member value of an object. Below is the prototype for const function: <return-value> function() const;const function with empty definition look like: <return-value> <className>::function() const {}From the above you can see that with the const keyword, we can make function a const function. const function makes this pointer as pointer to a const because of this reason, we cannot modify object data i.e. nonstatic data member of an object. const function can be called from const object as well as non-const…