Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0a973cf

Browse files
Add files via upload
1 parent 22f5819 commit 0a973cf

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Test
6+
{
7+
public:
8+
int x = 20;
9+
int y = 28;
10+
void Display() const
11+
{
12+
cout<<x<<y<<endl;
13+
/* 4th usage of the const access modifier */
14+
//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+
void myFunction(const int &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+
int main(void)
25+
{
26+
/* 1st usage of const access modifier */
27+
const int x = 1978;
28+
//x++;
29+
30+
/* 2nd usage of const access modifier*/
31+
int age = 45;
32+
const int *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"
54+
cout<<"Height is "<<*pHeight<<"\t"<<height<<endl;
55+
56+
Test t;
57+
t.Display();
58+
59+
int a = 45, b = 12;
60+
myFunction(a,b);
61+
cout<<"Printing from main()\t"<<a<<"\t"<<b<<endl;
62+
63+
return 0;
64+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /