TOKENS
The smallest individual units in a
program are known as tokens.
C++ has the following tokens:
1. key words
2. identifiers
3. constants
4. strings
5. operators
KEYWORDS
:
The keyword implement specific c++
The keyword implement specific c++
language features.
They cannot be used as names for the
program variables or other user –defined
program elements.
EG:
char
int
float
goto, for, etc;
IDENTIFIERS
• Identifiers refers to the name of
variables , functions, arrays, classes, etc
created by the programmer.
Each language has its own rules for
naming their identifiers.
The following rules are there for c++
Only alphabetic characters, digits &
underscores are permitted.
The name cannot start with digits.
Uppercase and lowercase are distinct.
Keyword cannot be used as a variable
name.
Constants refer to fixed values that do
not change during the execution of a
program
EG:
123, ‘A’, “SET”
BASIC DATA TYPES:
Data types in C++ can
be classified under various categories
C++DATATYPES
USER DEFINED DATATYPES
•STRUCTURE DERIVED DATATYPE
•UNION •Array
• CLASS BUILT –IN TYPE •Function
•ENUMERATION •Pointer
•Reference
INTEGRAL TYPE FLOATING TYPE
int char float double
VOID
Thebasic data types may have several modifiers. The
modifiers signed , unsigned, long & short may be
applied to character and integer basic data types.
TYPE BYTES RANGE
Char 1 -128to127
Unsigned 1 0 to 255
char
Int 2 -32,768 to
32767
Unsigned int 2 0 to 65535
VOID:
Two normal uses of void are
1. To specify the return type of a function when
it is not returning any value.
2. To indicate an empty argument list to a
function.
The third one is
void in the declaration of generic pointers
void * gp;
int * ip;
gp=ip; // legal
*ip = *gp; // illegal
Void *ptr1;
Char *ptr2;
Ptr2=ptr1;
Ptr2=(char*)ptr1;
USER –DEFINED DATA TYPES:
STRUCTURES AND CLASSES:
We have used user-defined data types such as
struct and union in C. while these data types are legal
in C++ , some more features have been added to make
them suitable for object oriented programming.
ENUMERATED DATA TYPE:
An enumerated data type is another user-defined
type which provides a way for attaching names to
numbers ,thereby increasing comprehensibility of the
code.
.
The enum key word automatically enumerates
a list of words
enum shape{ circle, square, triangle};
enum colour{ red, blue, green, yellow};
enum position{ off, on};
Now the tag names shape, colour , position
become new data types names. By using this
tag name we can declare new variable
shape ellipse;
Colour background;
It permits the creation of anonymous
enums( i.e., enums without tag names).
enum{ off, on};
Enumeration is used to define symbolic
constants for a switch statement.
Example:
enum shape
{
circle;
triangle;
rectangle;
};
int main()
{
cout<<“enter shape cod:”;
int code;
cin>>code;
While(code>= circle && code <= triangle)
{
switch( code)
{
case circle:
……….
……….
break;
case rectangle:
……….
……….
break;
case triangle:
………...
…………
break;
}
cout<< “Enter shape code:”;
cin>> code;
}
cout<< “\n”;
return 0;
}
DERIVED DATA TYPES:
ARRAYS:
The application of arrays in C++ is similar to
that in C . In C
char string[3]=“xyz”;
C compiler will allow us to declare the array
size as
the exact length of the string constant. In C++
char string[4]=“xyz”;
But in C++, the size should be one larger
than the number of characters in the string.
FUNCTIONS:
A function is a self-contained block of
statements that perform a coherent task of some kind.
Every C++ program can be thought of as a collection of
these functions.
- Any C++ program contains at least one
function.
- If a program contains only one function , it must
be main().
POINTERS:
Pointers are declared and initialized as in
C.
Example:
int *ip; // int pointer
ip = &x; // address of x assigned to ip
ip = 10; // 10 assigned to x through indirection
C++ adds the concept of constant pointer and
pointer to a constant.
char * const ptr1 = “Good”; // constant pointer
int const * ptr2 = &m; // pointer to a constant
const char * const cp =“xyz”;
Here we declare the pointer and variable as
constants.
SYMBOLIC CONSTANTS:
There are two way of creating symbolic
constants in C++
- using the qualifier const, and
- defining a set of integer constants using
enum keyword
const size =10;
means,
const int size =10;
C++ required constant to be initialized, if none is given, it
initialize the const to zero.
A const in C++ defaults to the internal linkage . So it
is local to the file. To give a const value an external linkage
we must explicitly define it as an extern in C++
extern const total =100;
Another method of naming constants by enum
enum = { x,y,z};
It means,
const x = 0;
const y = 1;
const z = 2;
We can also assign values to x, y, z explicitly .
enum = { x = 100 , y= 50 , z= 200};
DECLARATION OF VARIABLES:
• In C we can declare the variables at the beginning
of the program.
• C++ allows us to declare the variable anywhere in
the scope .
• It reduces the error in program and also easy to
understand the program.
Example:
int main()
{
float x; //declaration
float sun=0;
for( int i = 1; i<5; i++) //declaration
{
cin >> x;
sum= sum+x;
}
float average;
average =sum/ i ;
cout <<average;
return 0;
DYNAMIC INITIALIZATION OF VARIABLES:
C++ permits initialization of variables at runtime. This is
referred to as dynamic initialization. variable can be initialized
at runtime using expression at the place of declaration.
……….
……….
float area= 3.14159 * rad*rad;
float average;
average= sum/I;
It can be combined into a single statement as
float average= sum/I;
REFERENCE VARIABLES IN C++:
A reference variable provide an alias( alternative name) for
a previously define variable.
Syntax:
data-type & reference-name = variable-name
Example:
float total= 100;
float & sum= total;
cout<<total; and
cout<<sum;
Now the output is 100.
If we give
total= total + 10;
The both value of total and sum can be assigned as 110.
The reference value must be initialized at the time of declaration. This
establishes the correspondence between the reference and the data
object.
The notation float & means reference to float.
Example:
int n[10];
int &x=n[10]; // x is alias for n[10]
The following reference are also allowed :
1) int x;
int *p=&x;
int &m=*p;
2) int & n=50;
void f(int &x) // uses reference
{
x=x+10; // x is incremented;
}
int main()
{
int m=10;
f( m); // function call
………..
}
OPERATORS IN C++:
C++ has a rich set of operators. All C
operators are valid in C++ also. In addition , C++
introduces some new operators such as;
:: scope resolution operator
::* Pointer-to-member
declarator
->* Pointer-to-member
operator
.* Pointer-to-member
operator
delete Memory release operator
endl Line feed operator
new Memory allocation
operator
setw Field width operator