Mutable keywords in C++:17 Mar 2025 | 4 min read 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++ |
Password Validation is an essential aspect of Cybersecurity that is often overlooked. Passwords are the entry line of defense against unknown access to an account or system, and ensuring their strength can ent a multitude of cyber attacks. In this article, we will explore and...
5 min read
In this article, we will discuss the munmap_chunk invalid pointer in C++ with its syntax, programs, and several methods. An issue known as munmap_chunk():incorrect pointer occurs when a pointer that has been altered or rendered invalid is supplied to free(). It should be noted that the pointer...
5 min read
In this article, you will learn how to print all permutations in sorted order in C++ with its example. But before going to its implementation, you must know about the permutation and lexicographic order in C++. What are Permutations? A fundamental idea in computer science and combinatory is...
4 min read
Introduction: One of the key components of object-oriented programming in C++ is data hiding, which enables us to conceal internal object characteristics, such as data members, and prohibits program functions from directly accessing an object's internal representation, data members, and member functions. Access modifiers define the limitations on...
11 min read
Overview of Sequence Alignment in Bioinformatics Sequence alignment, a fundamental task in bioinformatics, involves the comparison of biological sequences such as DNA, RNA, or proteins to identify similarities and differences. This process is crucial for understanding the evolutionary relationships between different species, annotating genes, and deciphering the...
13 min read
This C++ project for a canteen management system includes functions like customer and product search, display, change, and deletion. This program does a search on the client information kept in the file before allowing the user to submit an order. The software is made for small...
19 min read
In this article, we are going find the Determinant of a matrix using different approaches. Before finding the value of a determinant, we must know about a matrix determinant. A matrix's determinant is a particular integer specified only for square matrices (matrices having the same number...
6 min read
What is Assertion? Assertions are the set of codes where we put some expression or condition to check whether the condition is true or not or to check whether the expression exists or not. If the condition is true or an expression exists then we get the true...
4 min read
? The C++ ios base class includes aspects for formatting cout objects that display various formatting characteristics. Using class scope static constants, the following ios base class can style cout object to display trailing decimal points, add + before positive integers, and various other formatting capabilities. Static Constants...
3 min read
Advantages and Disadvantages of Friend Function in C++ We have created the friend function to access the different modifiers in C++ Object Oriented Programming System such as Protected, Private and Public. The friend function or the friend class will usually be defined outside the course but still...
3 min read
We request you to subscribe our newsletter for upcoming updates.
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