-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathConstant_Data_Members.cpp
54 lines (41 loc) · 1.18 KB
/
Constant_Data_Members.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
/*
constant data members in class
- A constant data member cannot be changed even in a non-constant object. Therefore, it must be set at the time of initialization
of the object
*/
class MyClass {
public:
const int const_private_var;
int private_var;
public:
const int const_public_var;
int public_var;
//constructor
MyClass(int cPri, int pri, int cPub, int pub):
const_private_var(cPri),private_var(pri), const_public_var(cPub),public_var()pub {}
int getConstantPriVar() {
return const_private_var;
}
void setConstantPriVar(int i) {
const_private_var = i; //Error 1: Assignment to constant data member
}
int getPri() {
return private_var;
}
void setPri(int i) {
private_var = i;
}
};
int main() {
MyClass myObj(1, 2, 3, 4);
cout << myObj.getConstantPriVar() << endl;
myObj.setConstantPriVar();
cout << myObj.const_public_var << endl;
myObj.const_public_var = 3; //Error 2: Assignment to constant data member
cout << myObj.private_var << endl;
myObj.public_var = 3;
return 0;
return 0;
}