Professional Documents
Culture Documents
CONST - Once Initialised, No One Can Change It
CONST - Once Initialised, No One Can Change It
1. Const :
Constant, it is something fixed and can't be changed later. In
programming languages we used the word const to make the variables constant,
so that it's value can't be changed later. In
members and
objects.
With variables :
If the variables are declared using const keyword as prefix, they are
called as Constant variables.
For example :
const int x ;
The constant variables are initialised at the time of delcaration only.
const int x = 1;
we can also rewrite const int x = 1; as
int const x = 1;
Lookatthis:
#include <iostream>
using namespace std;
int main()
{
const int x;
// declaration
x = 1;
// initialization
cout<<x;
return 0;
}
int main()
{
const int x = 1;
x++;
return 0;
}
With Pointers :
Pointers to const :
Generally we called this as Pointers to constant variables. Here,
pointers point to constant data types like int, float, char etc. We can change
pointer to point to any other integer variable, but we cannot change the value of
object(entity) pointed using pointer. Pointer is stored in read-write area (stack
in present case). Object pointed may be in read only or read write area.
Forexample:
int x = 10;
( or )
int x = 10;
int const *p = &x;
int x = 10;
int const *p = &x;
x=11;
x++;
cout<<x;
return 0;
}
Output :
12
But
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int const *p = &x;
(*p)++;
cout<<x;
return 0;
}
{
int x = 10;
int y = 11;
int *const p = &x;
(*p) = 12;
// valid
*p = & y;
// error
return 0;
}
Note :
const with pointers - In a nutshell
char c;
char *const cp = &c;
which means that now cp is an ordinary, modifiable pointer, but the thing that it
points to must not be modified. So, depending on what you choose to do, both
the pointer and the thing it points to, may be modifiable.
// error
p = &y;
// error
return 0;
}
int const f() is equivilent to const int f(), which means that
return type is const.
{
x++;
// error
}
const int fun()
{
return 1;
Example :
#include<iostream>
using namespace std;
void fun(int *p)
// non-const argument
{
cout<< *p;
}
int main()
{
const int x=10;
fun(&x);
// const parameter
// error
return 0;
}
Example :
#include<iostream>
using namespace std;
void fun(const int *p)
// non-const parameter
{
cout<< *p;
}
int main()
{
const int x = 10;
fun(&x);
// const agrument
int y = 11;
fun(&y);
return 0;
}
ForExample:
#include<iostream>
using namespace std;
class cls
{
int const x;
public:
cls () : x(1)
// constructor
{ }
void disp()
{
cout<< x;
}
};
int main()
{
cls a;
a.disp();
return 0;
output : 1
Notice the syntax : x(1) after the constructor. This tells C++ to
initialize the variable y to have value 1. More generally, you can use this syntax
to initialize data members of the class.
const member funcions & objects :
1. The member functions of class are declared constant as follows :
return_type fun_name () const ;
This makes the function itself as constant.
{}
};
int main()
{
A
obj_nonconst;
// nonconst object
obj_nonconst.Const_No();
// works fine
obj_nonconst.Const_Yes();
// works fine
const A obj_const ;
// const object
obj_const.Const_Yes();
obj_const.Const_No();
A const member function can be used with both objects (const and
non-const) while, A non - const function can be used only with non const
objects, but not wtih const objects.
Try it urself :
#include<iostream>
using namespace std;
class cls
{
public:
int
x;
cls(int i)
// constructor initialization
{
x = i;
}
void f() const
// constant function
{
x++;
}
void g()
// non-constant function
{
x++;
}
};
int main()
{
const cls a(20);
cls b(30);
a.f();
a.g();
b.f();
b.g();
cout<<a.x;
cout<<b.x;
return 0;
}