-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConst.cpp
More file actions
41 lines (29 loc) · 1.08 KB
/
Const.cpp
File metadata and controls
41 lines (29 loc) · 1.08 KB
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
#include<iostream>
using namespace std;
class Test{
int num1;
const int val;
public:
Test():num1(10),val(50){} //constuctor member initializer list (shortcut method for member initalization)
/*
Test(){ //not allowed //error
num1=10; //if we have constant data member inside the class then we must always initialize
val=50; //constant data member using constructor member initializer list
}
Test():val(50){ allowed //partial constructor initializer list
num1=10;
}
*/
void disp(){
cout<<"Num1 = "<<num1<<endl;
cout<<"Val = "<<val<<endl;
this->num1+=10; //allowed
//this->val+=10; //error //constant values cannot modified
cout<<"After modification, Num1 = "<<num1<<endl;
}
};
int main(){
Test t;
t.disp();
return 0;
}