Jan 17, 2015

ENUM - Make u r names as Data types


Keywords – II
 

Enum :
         An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. 
 
The syntax is :

          enum Tag_name {Variables_names}

For example:
  enum tag_name{ value1, value2,...,valueN };
            
                  Here, tag_name is the name of enumerated data type. And value1, value2,....,valueN are values of type tag_name.
  • If the first enumerator does not have an initializer (if it is not initialized by the user), the associated value is zero. For any other enumerator whose definition does not have an initializer, the associated value is the value of the previous enumerator plus one.
  • Enum variables are the same size as an int variable.This is because each enumerator is automatically
     assigned an integer value based on it’s position in the enumeration list
           In the above exapmle, By default, value1 will be equal to 0, value2 will be 1 and so on... But, the programmer can change the default values to their own values.




 By creating the enum months, you have also created a new data type called months.
 You can use this new data type as you might any other data type.   
For Example:
       months jan ;
    jan = january;

  • At the declaration time, we can also initialize the values with the primitive operators(only a few).

G = -8;
  • The enum constants can be assigned to other enumerators.

  • Already declared enumerators can't be reassigned explicitly.
 enum color { red = 0,yellow, green, blue };
      color red = 11;            // compilation error 
                  However, the compiler will not implicitly cast an integer to an enumeratedvalue. The following will  
produce a  compiler error:
             enum color { red = 0, yellow, green, blue };
  color red1 = 11;           // compilation  error
  • But  there is one way to change enumerators values. This can be done using  
    the static_cast operator by forcing the compiler to put the value in the  
    enumerated data types.
  • Assigning between enumerators can be done but between integers and
    enumerators results in compilation error. And enum also should be of the 
    same type.
  

  • Enumerated types are incredibly useful for code documentation and  readability purposes when 
    you need to represent a specific number of states.

              This will be much easier to read than returning the number values.
Enumerators with class :
                      In gerenal, Enumerators are implicitly converted  into the int data type. It is also possible to
 have char, float etc, thuspreserving data type safety. They are declared with enum class (or enum struct)
 instead of just enum.
For example  :
                      enum class biodata{ name, age, place, dob };
  • Each enum value is scoped with the name of the enum class. In other words,to access the enum
     values, you must write:
    

  Enumerated types declared with enum class also have more control over their underlying 
type. Itmay be any integral data type, such as charshort or unsigned int, which essentially serves 
to determine the size of the type. This is specified by a colon and the underlying type following the 
enumerated type. 
For example:

         enum class biodata : int { dob, age};
         enum class biodata : char{ name, place};

Try urself :

1.



2.
 
 
Filed under  | 

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  |