Mutable keywords in C++:Mutable keywords are very useful keywords in C++. It is used to change or modify the class data member even if the object is a const function. Let's take an example if there is a class containing five non-static variables. All these were assigned a value with the help of a class constructor. In the main function, you created an object and decided not to change that particular object, so you used const for that object. However, after some time, you wanted to change only two particular variables. Now, you have a problem: if you want to change only two variables, you cannot remove the const before the object. Now, the mutable keyword is used in this situation. Suppose you make those two non-static variables mutable in class. In that case, you can make changes for only that particular variable, and other variables remain in const and cannot be changed. Example:A simple program to demonstrate the usage of mutable keywords: Output: Explanation:In this example, this simple program contains a main function and a class named MyClass again. In MyClass, there are two variables named variable1 and variable2, those two belong to int datatype, and variable1 is not mutable as the variable2 is mutable. This class contains two methods or functions named setVariable2, and other is the simple function display method. The setVariable2 method takes an argument and assigns that argument to the variable2. Another function display method is used to display both variable1 and variable2. Now, in the main class, an object is created, and the const keyword is used to make it constant. Therefore, the variables in the object cannot be modified, but the second variable, variable2 can be modified or changed by using the setVariable2 method because the variable2 is mutable. Here, the keyword mutable is used for changing only a particular variable in the whole const object. Example:Now, we will bind a daily life application for the above concept: Output: Explanation:The above program contains a class and a main function. In the class, there is a constructor and other methods like deposit, withdraw, and display methods. These all together make the class work as a bank functionality. This class is named as BankAccount. It contains four variables: accountHolderName, which is of string datatype, accountNumber, which is of int datatype, balance, which is of double datatype, and lastTransactionDate, which is of string datatype. Here, the two variables, balance and lastTransactionDate are mutable. The deposit method takes an argument, which is the amount to be added to the balance and updates the time of the last transaction. It makes the mutable variable to be changed. The withdraw method also takes an argument, which is the amount to be subtracted from the balance, and again updates the time of the last transaction. If the sufficient amount is not present in the bank, it displays "Insufficient funds". After that, the display method is used to display all the four variables. In the main function, an object is created and named an account by taking four arguments: name, number, intitalbalance. The first display method is called. Now, some amount is deposited, and again, the display method is called. After that, the withdraw method is called again, and the display method is called. After every display method, we can see the changes in balance and lastTranscationDate, which are mutable and can be modified. Next TopicMove all zeroes to end of array in C++ |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India