-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutable.cpp
33 lines (30 loc) · 861 Bytes
/
mutable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;
class Contoh
{
public:
mutable int a; // anggota kelas dengan kelas penyimpanan "mutable"
mutable int b; // anggota kelas dengan non mmutable
Contoh(int a, int b)
{
this->a = a;
this->b = b;
}
};
int main()
{
// membuat object konstanta dari kelas Contoh
const Contoh obj(10, 20);
// menampilkan nilai a dan b di dalam obj
cout << "sebelum diubah" << endl;
cout << "Nilai a = " << obj.a << endl;
cout << "Nilai b = " << obj.b << endl;
// mengubah nilai a
obj.a = 30; // obj.a bisa di ubah karena a adalah mutable
// mengubah nilai b
obj.b = 40; // obj.b tidak bisa di ubah karena b adalah non mutable
cout << "setelah diubah" << endl;
cout << "Nilai a = " << obj.a << endl;
cout << "Nilai b = " << obj.b << endl;
return 0;
}