Const is used for:
-
Constant Variables Ex: const int x=5; • The value of x won’t change throughout the whole program • Any trial to change this variable will lead to an error
-
constant function Declaration const void push(){ // function } • Push() can’t modify the object in which it is called.
-
const return type const void push(){ //code of the function } int main (){ push(5) }
-
A pointer that points to a const value Ex: int var=5; const int *ptr=&var; • Ptr cannot change the value of var.
-
const pointer to the data int *const ptr=&var; • ptr cannot change the address it’s holding.
-
constant pointer to const data Const int *const ptr= &var; • ptr can neither change the address it’s pointing to nor change the value of var.
-
Const function parameters void print(const int y){ cout<<y<<endl; }
& Used for:
-
Address operator yields a pointer to its operand. Ptr=&y;
-
A reference declarator in addition to being the address operator.
-
Logical AND int main (){ int x=7,y=4; cout<<x&&y<<endl; return 0; } • Output will be 1 (true) • while if x=0 and y=4, the output will be 0 (false)
-
Bitwise AND int main (){ int x=1,y=5; cout<< x&y <<endl; } • The output will be 1