Unit II: Periods: 15
Introduction to Tokens
Keywords,
Identifiers & Constants,
Basic Data Types,
Variables
Operators in C++,
Decision Control & Loop Control Structures: If, If-else, Nested If, Else-if
ladder, switch, goto Statement, break statement, while, do-while, for loop.
TOKENS:
The smallest individual units in program are known as tokens. C++ has the following tokens.
i. Keywords
ii. Identifiers
iii. Constants
iv. Strings
v. Operators
KEYWORDS:
The keywords implement specific C++ language feature. They are explicitly reserved identifiers and
can’t be used as names for the program variables or other user defined program elements. The
keywords not found in ANSI C are shown in red letter.
C++ KEYWORDS:
Asm double new switch
Auto else operator template
Break enum private this
Case extern protected throw
Catch float public try
Char for register typedef
Class friend return union
Const goto short unsigned
Continue if signed virtual
Default inline sizeof void
Delete long struet while
IDENTIFIERS:
Identifiers refers to the name of variable , functions, array, class etc. created by programmer. Each
language has its own rule for naming the identifiers.
The following rules are common for both C and C++.
SYMBOLIC CONSTANT:
There are two ways of creating symbolic constants in c++.
1. using the qualifier const.
2. defining a set of integer constants using enum keywords.
In both C and C++, any value declared as const can’t be modified by the program in any way.
In C++, we can use const in a constant expression. Such as
const int size = 10 ;
char name (size) ;
This would be illegal in C. const allows us to create typed constants instead of having to use #defme to
create constants that have no type information.
const size=10;
Means
const int size =10;
C++ requires a const to be initialized. ANSI C does not require an initializer, if none is given, it
initializes the const to 0.
In C++ const values are local and in ANSI C const values are global .However they can be made
local made local by declaring them as static .In C++ if we want to make const value as global then declare
as extern storage class.
Ex: external const total=100;
Another method of naming integer constants is as follows:-
enum {x,y,z};
BASIC DATA TYPES IN C++
C ++ Data Types
Array
Function
pointer
Integral type void Floating point
int char float double
Both C and C++ compilers support all the built in types. With the exception of void the basic
datatypes may have several modifiers preceding them to serve the needs of various situations. The
modifiers signed, unsigned, long and short may applied to character and integer basic data types.
However the modifier long may also be applied to double.
Data types in C++ can be classified under various categories.
TYPE BYTES RANGE
char 1 -128 to – 127
usigned 1 0 to 265
sgned char 1 -128 to 127
int 2 -32768 to 32768
unsigned int 2 0 to 65535
singed int 2 -32768 to 32768
short int 2 -32768 to 32768
long int 4 -2147483648 to 2147483648
signed long int 4 -2147483648 to 2147483648
unsigned long int 4 0 to 4294967295
float 4 3.4E-38 to 3.4E+38
double 8 1.7E -308 to 1.7E +308
long double 10 3.4E-4932 to 1.1E+ 4932
The type void normally used for:
1) To specify the return type of function when it is not returning any value.
2) To indicate an empty argument list to a function.
Example:
Void function(void);
Another interesting use of void is in the declaration of genetic pointer
Example:
Void *gp;
Assigning any pointer type to a void pointer without using a cast is allowed in both C and ANSI C.
In ANSI C we can also assign a void pointer to a non-void pointer without using a cast to non void
pointer type. This is not allowed in C ++.
Example:
void *ptr1;
void *ptr2;
Are valid statement in ANSI C but not in C++. We need to use a cast operator.
ptr2=(char * ) ptr1;
2. Only alphabetic chars, digits and underscore are permitted.
3. The name can’t start with a digit.
4. Upper case and lower case letters are distinct.
5. A declared keyword can’t be used as a variable name.
In ANSI C the maximum length of a variable is 32 chars but in c++ there is no bar.
DECLARATION OF VARIABLES:
In ANSIC C all the variable which is to be used in programs must be declared at the beginning of
the program .But in C++ we can declare the variables any whose in the program where it requires .This
makes the program much easier to write and reduces the errors that may be caused by having to scan back
and forth. It also makes the program easier to understand because the variables are declared in the context
of their use.
Example:
main()
{
float x,average;
float sum=0
for(int i=1;i<5;i++)
{
cin>>x;
sum=sum+x
}
float average;
average=sum/x;
cout<<average;
REFERENCE VARIABLES:
C++interfaces a new kind of variable known as the reference variable. A references variable
provides an alias.(alternative name) for a previously defined variable. For example ,if we make the
variable sum a reference to the variable total, then sum and total can be used interchangeably to
represent the variable.
A reference variable is created as follows:
Syntax: Data type & reference –name=variable name;
Example:
float total=1500;
float &sum=total;
Here sum is the alternative name for variables total, both the variables refer to the same data object in the
memory .
A reference variable must be initialized at the time of declaration .
Note that C++ assigns additional meaning to the symbol & here & is not an address operator
.The notation float & means reference to float.
Example:
int n[10];
int &x=n[10];
char &a=’\n’;
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.
<< insertion operator
>> extraction operator
:: 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
SCOPE RESOLUTION OPERATOR:
Like C,C++ is also a block-structured language. Block -structured language. Blocks
and scopes can be used in constructing programs. We know same variables can be declared in different
blocks because the variables declared in blocks are local to that function.
Blocks in C++ are often
nested. Example:
{
Int x =10;
Int x=1; Block2
--------------- Block1
}
Block2 contained in block l .Note that declaration in an inner block hides a declaration of the
same variable in an outer block and therefore each declaration of x causes it to refer to a different data object
. With in the inner block the variable x will refer to the data object declared there in.
In C,the global version of a variable can't be accessed from with in the inner block. C++ resolves this
problem by introducing a new operator :: called the scope resolution operator .This can be used to uncover
a hidden variable.
Syntax: : : variable –name;
Example:
#include <iostrcam.h>
int m=10;
main()
{
int m=20;
int k=m;
int m=30;
cout<<”we are in inner
block”; cout<<"k="<<k<<endl;
cout<<"m="<<m<<endl;
cout<<":: m="<<:: m<<endl;
cout<<”\n we are in outer block \
n”; cout<<"m="<<m<<endl;
cout<<":: m="<<:: m<<endl;
Memory Management Operator
C uses malloc and calloc functions to allocate memory dynamically at run time . Similarly it uses the
functions Free( ) to free dynamically allocated memory. We use dynamic allocation techniques when it is
not known in advance how much of memory space as needed .
C++ also support those functions it also defines two unary operators new and delete that perform the task
of allocating and freeing the memory in a better and easier way.
The new operator can be used to create objects of any type.
Syntax:
pointer- variable =new datatype;
Example:
p=new int; q=new int;
Where p is a pointer of type int and q is a pointer of type float.
int *p=new int;
float
*p=newfloat;
Subsequently, the statements
*p=25;
*q=7.5;
Assign 25 to the newly created int object and 7.5 to the float object.We can also initialize the
memory using the new operator.
Syntax:
int *p=ne\v int(25);
float *q =new float(7.5);
new can be used to create a memory space for any data type including user defined such as
arrays,structures,and classes .The general form for a one-dimensional array is:
pointer-variable =new data types [size];
creates a memory space for an array of 10 integers.
If a data object is no longer needed, it is destroyed to release the memory space for reuse.
Syntax: delete pointer-variable;
Example:
delete p;
delete q;
If we want to free a dynamically allocated array ,we must use the following
form of
delete.
delete [size] pointer-variable;
or
delete [ ] pointer variable;
MANIPULATERS:
Manipulators are operator that is used to format the data display. The most commonly manipulators are
endl and setw.
The endl manipulator, when used in an output statement, causes a line feed to be insert.(just like \n)
Example:
cout<<”m=”<<m<<endl;
cout<<”n=”<<n<<endl;
cout<<”p=”<<p<<endl;
If we assume the values of the variables as 2597,14 and 175
respectively m=2597; n=14;
p=175
It was want to print all nos in right justified way use setw which specify a common field
width for all the nos.
Example: cout<<setw(5)<<sum<<endl;
cout<<setw(10)<<”basic”<<setw(10<<basic<<endl;
Cout<<setw(10)<<”allowance”<<setw(10<<allowance<<endl;
cout<<setw(10)<<”total=”<<setw(10)<<total;
CONTROL STRUCTURES:
Like c,c++, supports all the basic control structures and implements them various control statements.
The if statement:
The if statement is impklemented in two forms:
1. simple if statement
2. if… else statement
Simple if statement:
if (condition)
{
Action;
}
If.. else statement
If
(condition)
Statment1
Else
Statement2
The switch statement
This is a multiple-branching statement where, based on a condition, the control is transferred to one of the
many possible points;
Switch(expr)
{
case 1:
action1;
break;
case 2:
action2;
break;
..
..
default:
message
The while
statement: Syn:
While(condition)
Statements
}
The do-while statement:
Syn:
do
Statements
} while(condition);
The for loop:
for(expression1;expression2;expression3)
Statements;
Statements;