You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//x++; // it is not allowed to change a member data of the class because the function had been marked with const
15
+
}
16
+
};
17
+
18
+
voidmyFunction(constint &a, int &b)
19
+
{
20
+
cout<<"Printing from myFunction()\t"<<a<<"\t"<<b<<endl;
21
+
//a++; //it is not allowed to change the value of a parameter marked as const
22
+
}
23
+
24
+
intmain(void)
25
+
{
26
+
/* 1st usage of const access modifier */
27
+
constint x = 1978;
28
+
//x++;
29
+
30
+
/* 2nd usage of const access modifier*/
31
+
int age = 45;
32
+
constint *pAge = &age;
33
+
34
+
cout<<"Age is "<<*pAge<<"\t"<<age<<endl;
35
+
36
+
age = 70;
37
+
cout<<"Age is "<<*pAge<<"\t"<<age<<endl;
38
+
39
+
//(*pAge)++; // It is not allowed to change the address the pointer is pointing to -> pointer to an integer constant (read from right to left in the declaration of the variable)
40
+
cout<<"Age is "<<*pAge<<"\t"<<age<<endl;
41
+
42
+
int height = 178;
43
+
pAge = &height;
44
+
cout<<"Age is "<<*pAge<<"\t"<<age<<endl;
45
+
46
+
/* 3rd of the const access modifier*/
47
+
int * const pHeight = &height;
48
+
cout<<"Height is "<<*pHeight<<"\t"<<height<<endl;
49
+
50
+
++(*pHeight);
51
+
cout<<"Height is "<<*pHeight<<"\t"<<height<<endl;
52
+
53
+
//pHeight = &age; // this is not allowed as we are dealing with a constant pointer , that does not change its value ->read from left to right "constant pointer"
0 commit comments