Mutable keywords are incredibly valuable in C++. They allow for the modification of class data members even within a const member function. For instance, consider a class with five non-static variables initialized through a class constructor. In a scenario where you create a const object in the main function but later need to alter only two specific variables, the mutable keyword becomes essential. By marking those two variables as mutable within the class, you gain the flexibility to modify them while keeping the other variables constant and unchangeable.
Example:
A basic script showcasing the application of mutable keywords:
#include<iostream>
using namespace std;
class MyClass {
public:
int variable1;
mutable int variable2;
MyClass(int x,int y){
variable1 = x;
variable2 = y;
}
void setVariable2(int z) const{
variable2 =z;
}
void display() const {
cout<< "variable1 is "<< variable1 << endl;
cout << "Variable2 is "<< variable2 << endl;
}
};
int main(){
const MyClass my(10,20);
cout<< "Before changing the variable2 "<< endl;
my.display();
my.setVariable2(2000);
cout<< "After changing the variable2 "<< endl;
my.display();
}
Output:
Explanation:
In this instance, a basic program consists of a primary function and a MyClass class. Within MyClass, there exist two variables - variable1 and variable2. These variables are both of the int data type; however, variable1 is immutable while variable2 is mutable. The class is equipped with two functions: setVariable2 and a straightforward display method. The setVariable2 function accepts an argument and assigns it to variable2. The display method, on the other hand, is employed to showcase both variable1 and variable2.
Now, within the primary class, an instance is instantiated, and the const keyword is applied to render it immutable. As a result, the properties within this instance are not alterable. However, the second property, referred to as variable2, remains modifiable through the utilization of the setVariable2 function since it is considered mutable. The term "mutable" is employed to allow modifications to specific variables within an otherwise constant object.
Example:
Next, we will connect this concept to a practical daily life scenario:
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
class BankAccount {
string accountHolderName;
int accountNumber;
mutable double balance;
mutable string lastTransactionDate;
public:
BankAccount(string name, int number, double initialBalance){
accountHolderName = name;
accountNumber = number;
balance = initialBalance;
time_t now = time(0);
lastTransactionDate = ctime(&now);
}
void deposit(double amount) const {
balance += amount;
time_t now = time(0);
lastTransactionDate = ctime(&now);
}
void withdraw(double amount) const {
if (balance >= amount) {
balance -= amount;
time_t now = time(0);
lastTransactionDate = ctime(&now);
} else {
cout << "Insufficient funds!" << endl;
}
}
void display() const {
cout << "Account Holder: " << accountHolderName << endl;
cout << "Account Number: " << accountNumber << endl;
cout << "Current Balance: " << balance << endl;
cout << "Last Transaction Date: " << lastTransactionDate;
cout << " " << endl;
}
};
int main() {
const BankAccount account("SriRam", 12345, 1000.0);
account.display();
account.deposit(500.0);
account.display();
account.withdraw(200.0);
account.display();
return 0;
}
Output:
Explanation:
The program above includes a class with a main function. Within the class, there are various methods such as deposit, withdraw, and display, in addition to a constructor. Collectively, these methods enable the class to function as a representation of a bank account. The class is denoted as BankAccount and comprises four variables: accountHolderName (string data type), accountNumber (integer data type), balance (double data type), and lastTransactionDate (string data type). Notably, the variables balance and lastTransactionDate are modifiable.
The deposit function requires an input parameter specifying the amount to increase the balance by and then updates the timestamp of the last transaction. This operation modifies the mutable variable. Similarly, the withdraw function requires an argument indicating the amount to decrease the balance by and updates the last transaction time. If the bank account does not have enough funds, it will show "Insufficient funds". Following these operations, the display function is employed to showcase all four variables.
In the primary function, an instance is instantiated and labeled as an account with four parameters: name, number, intitalbalance. The initial invocation of the first display procedure is executed. Subsequently, a certain sum is added to the account, followed by another invocation of the display procedure. Following this, the withdrawal procedure is triggered, and the display procedure is invoked once more. With each invocation of the display procedure, alterations in the balance and lastTranscationDate are observable, as these values are mutable and subject to modification.