Jan 9, 2015

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:

                  
                   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.





              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

  • 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 :
       
Output :
                      12

But

                  It gives an error as the pointer is assigned to read only location.
  • We can change pointer to point to any other integer variable
  •   The non-const pointers can't point to const data types. 
  • Example :  
        • 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:
     

 
                  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 :


 
                      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.

  • 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:



  • 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.
  • int const f() is equivilent to const int f(), which means that return type is const.


      •   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 :


                 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 :




    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 Example:
     
                  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.

    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 :

     

                   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 :


    Filed under  |