13
Reserved Words – I 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 C++, we use this word with variables, pointers, functions and its return types, classes 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; Look at this: #include <iostream> using namespace std; int main() { For further reference : http://comsciguide.blogspot.com/

CONST-- once initialised, no one can change it

Embed Size (px)

Citation preview

Page 1: CONST-- once initialised, no one can change it

Reserved Words – I

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 C++, we use this word with

variables, pointers, functions and its return types, classes 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;

Look at this:

#include <iostream>

using namespace std;

int main()

{

For further reference : http://comsciguide.blogspot.com/

Page 2: CONST-- once initialised, no one can change it

const int x; // declaration

x = 1; // initialization

cout<<x;

return 0;

}

The above program produces an error, as the const variable x is not

initilaised at the time of declaration. Declaration and Initialization can't be done

seperately.

int main()

{

const int x = 1;

x++;

return 0;

}

It results in compilation error as the const variables values can't be

changed later.

➢ With Pointers :

In this section, there are two types.

1. Pointers to const

2. const pointers

For further reference : http://comsciguide.blogspot.com/

Page 3: CONST-- once initialised, no one can change it

➔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.

For example :

int x = 10; ( or ) int x = 10;

const int *p = &x; int const *p = &x;

Here x is a const integer and p is a pointer variable pointed to const

int x.

Ofcoure u can change x directly but, If u perform to change the value of x

using p (pointer), it gives an error.

Let's take with an example :

#include<iostream>

using namespace std;

int main()

{

For further reference : http://comsciguide.blogspot.com/

Page 4: CONST-- once initialised, no one can change it

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;

}

It gives an error as the pointer is assigned to read only location.

We can change pointer to point to any other integer variable

int x = 10;

int y = 11;

int const *p = &x;

*p = &y; // pointer is assigned to another variable

For further reference : http://comsciguide.blogspot.com/

Page 5: CONST-- once initialised, no one can change it

The non-const pointers can't point to const data types.

Example :

const int x=1;

int *p = &x; // non-const pointer can't

assigned to const int

It results in compilation error.

➔const pointers to variables:

If pointers are declared const as prefix, they are called as const

pointers. We can't change the pointer to point to another variable, but

can change the value that it points to.

For example:

int x = 10;

int * const a = &x;

Here, a is the pointer which is const that points to int x. Now we

can't change the pointer, but can change the value that it points to.

Take a look with an example :

#include<iostream>

using namespace std;

int main()

For further reference : http://comsciguide.blogspot.com/

Page 6: CONST-- once initialised, no one can change it

{

int x = 10;

int y = 11;

int *const p = &x;

(*p) = 12; // valid

*p = & y; // error

return 0;

}

It results in error as the pointer points to, can't be changed. But we

can change the value that it points to.

Note :

“const with pointers” - In a nutshell

char c;

char *const cp = &c;

cp is a pointer to a char. The const means that cp is not to be

modified, although whatever it points to can be--the pointer is constant, not the

thing that it points to. The other way is

const char *cp;

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.

For further reference : http://comsciguide.blogspot.com/

Page 7: CONST-- once initialised, no one can change it

➔Const pointers to const variables :

Here, We can't change pointer to point to any other integer variable

and also, the value of object (entity) it pointed.

Lets see with an example:

#include<iostream>

using namespace std;

int main()

{

int x = 10;

int y = 11;

const int *const p = &x;

(*p) = 12; // error

p = &y; // error

return 0;

}

➢ With Functions arguments and its Return type:

The const keyword in functions arguments is similiar to that of

const with variables

Example :

void fun (const int x) {}

By doing this, u can't modify the x value inside the function.

For further reference : http://comsciguide.blogspot.com/

Page 8: CONST-- once initialised, no one can change it

int const f() is equivilent to const int f(), which means that

return type is const.

void fun(const int x) // example for fun arg

{

x++; // error

}

const int fun() // example for return type

{

return 1; //any constant value

}

If a function has a non-const parameter, then we cannot make a const

argument call i.e. we can't pass the const argument at the function call.

Example :

#include<iostream>

using namespace std;

void fun(int *p) // non-const argument

{

cout<< *p;

}

int main()

{

const int x=10; // const parameter

fun(&x); // error

return 0;

}

For further reference : http://comsciguide.blogspot.com/

Page 9: CONST-- once initialised, no one can change it

Here, we are passing the const argument to the non – const

parameters. So it results in error.

But a, function with a const parameter, can make a const argument

call as well as non- const argument at the function call.

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); // non const argument

return 0;

}

4. With classes and its members :

• const class data members

These are data variables in class, which are made const. They

are not initialized during declaration. ButTheir initialization occur in the

constructor.

For further reference : http://comsciguide.blogspot.com/

Page 10: CONST-- once initialised, no one can change it

For Example:

#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.

For further reference : http://comsciguide.blogspot.com/

Page 11: CONST-- once initialised, no one can change it

2. The objects of class can be declared constant as follows :

const class_name obj_name ;

Making a member function const means that it cannot change any

member variable inside the function. It also means that the function can be

called via a const object of the class.

Look at the example below :

#include<iostream>

using namespace std;

class A

{

public:

void Const_No() {} // nonconst member function

void Const_Yes() const {} // const member function

};

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(); // const object can call const

function

obj_const.Const_No(); // ERROR-- const object cannot call

nonconst function

}

For further reference : http://comsciguide.blogspot.com/

Page 12: CONST-- once initialised, no one can change it

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); // constant object call

cls b(30); // non-constant object call

a.f();

a.g();

b.f();

b.g();

For further reference : http://comsciguide.blogspot.com/

Page 13: CONST-- once initialised, no one can change it

cout<<a.x;

cout<<b.x;

return 0;

}

For further reference : http://comsciguide.blogspot.com/