Tokens ,Expressions and Control Structure





Tokens ,Expressions and Control Structure

Tokens are smallest unit of c++ program
C++  has following tokens.
1.      Keywords
2.      Identifiers
3.      Constants
4.      Strings
5.      Operators
Most  of C++ tokens are similar to  C language tokens.

Keywords :

The Keywords are reserved word whose meaning is predefine in compiler.
KEY WORDS ARE LIKE,
 Asm , friend,  private, catch, inline, protected, try, throw, class, new, delete,virtual, template, operator.

Identifiers and constants :

Identifiers are the name of variables ,function,array,class,etc.created by programmer
Each langauge has its own rulefor nameiing these identifires.

Following are some rule for identifiers in C & C++.
1.      Only alphabetic Character , digits and underscore are permitted.
2.      The name can not start with digits and any special characters except underscore (_) .
3.      Upper case and lower case letter are distinct.
4.      The keyword can not use as variable name.

Constants are the identifiers whose value is constants and not change  at the time of execution .



Reference variable :

This is new type of variable introduce in c++, that is reference variable,which provide an alternative name for previously define variable.

Syntax:
int a=100;
int &b=a;
cout<<a<<b;




here ‘b’ is an alternative name of ’ a’. and both prints same value 100.
Note: C++ assign additional meaning to symbol, ‘&’ operator here ‘&’ is not address operator.
 int & means reference to int.

Operators in C++  :
C++ has rich set of operator, all the C operator are valid in C++. And there are some new operators in C++.
1.      :: = Scope Resolution Operator.
2.      ::* = Pointer to Member Declaration.
3.      ->* = Pointer to Member Operator.
4.      new = Memory allocation operator.
5.      delete = Memory release  Operator.
6.      endl = New line Operator.
7.      set w = field width operator.




Memory management Operator :

we know that C use malloc() and calloc() function for allocating memory dynamically at run time. C++ also support this but it also has unary operator new and delete. Which is also used for allocate and release memory dynamically at runtime.


new operator :
syntax:   Pointer variable = new datatype(value);

       like
int *p;
 p=new int;

Delete Operator :
When data object no longer need it is destroyed and release the memory space for reuse.

Syntax : delete pointer-variable.


Lets take example of new and delete both together

int *p =new int[10];    //memory allocate
delete[10] p;  //memory release

Advantage of new operator over malloc

1.      it automatically compute the size of data object no need to use size of () operator.

2.      It automatically returns correct pointer type so there is no need to use type casting.

3.      It is possible to initialize the object while creating memory space.

4.      New and delete operator can overload like other operator.




Comments