Coupon Accepted Successfully!


The Keyword mutable

We learned that a member function or object can be declared as constant using the keyword const. The constant data elements are not modified, and the constant member function can alter the value of any variable. The mutable is used when we need to declare a constant object but partly, that is, a specific data variable can be modified. Thus, the data variable that is to be modified is declared as mutable. The declaration is as follows:

mutable int k;

Here, the variable k can be modified though its class or object is declared as constant.


21.12 Write a program to use mutable keyword and modify the value of variable.


using namespace std;

class XYZ



mutable int k;


explicit XYZ (int d) {k=d;}

void value() const {k+=10;}

void show() const {cout<<“\n k=”<<k;}


int main()


const XYZ x(75);




return 0;




k= 75

k= 85



Explanation: In the above program, the class XYZ is a one-integer mutable member. The class also has value() and show() constant member functions. In the function main(), the x is a constant object of class XYZ. When x is declared, the mutable member k is initialized with 75. When the value() function is invoked, it is incremented with 10. The show() function displays the values of the variables.

Test Your Skills Now!
Take a Quiz now
Reviewer Name