Tokens
Token is the smaller individual units in a program. C++ has the following tokens:
- Keywords
- Identifiers.
- Constants.
- Strings.
- Operators.
Keywords
They are explicitly reserved identifiers and cannot be used as names for the program variables or other user-defined program elements. Table below gives the complete set of 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
|
|
continuo
|
if
|
signed
|
virtual
|
|
default
|
inline
|
sizeof
|
void
|
|
delete
|
int
|
static
|
volatile
|
|
do
|
long
|
struct
|
while
|
Identifiers
Identifiers refer to the names of variables, functions, arrays, classes, etc. created by the programmer. Each language has its own rules for naming these identifiers. The following rules are common:
- Only alphabetic characters, digits and underscores are permitted.
- The name cannot start with a digit.
- Uppercase and lowercase letters are distinct.
- A declared keyword cannot be used as a variable name.
Basic Data Type
Data types in C++ can be classified under various categories as shown in figure bellow
|
Type
|
Byte
|
Range
|
|
char
|
1
|
-128 to 127
|
|
unsigned char
|
1
|
0 to 255
|
|
signed char
|
1
|
-128 to 127
|
|
int
|
2
|
-32768 to 32767
|
|
unsigned int
|
2
|
0 to 65535
|
|
signed int
|
2
|
-32768 to 32767
|
|
short int
|
2
|
-32768 to 32767
|
|
unsigned short int
|
2
|
0 to 65535
|
|
signed short int
|
2
|
-32768 to 32767
|
|
long int
|
4
|
-2147483648 to 2147483647
|
|
signed long int
|
4
|
-2147483648 to 2147483647
|
|
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
|
In addition to the type void. Two normal uses of void are (1) to specify the return type of a function when it is not returning any value, and (2) to indicate an empty argument list to a function. Example:
void funct1(void);
-----------------------------------------------------------------------------------------------------------------
User-Defined Data Type:
-Enumerated Data Type
Enumerated data type is a user-defined type which provides a way for attaching names to numbers, thereby increasing comprehensibility of the code. Example:
enum shape(circle, square, triangle);
enum colour(red, green, yellow);
enum position(off,on);
so we can declare new variables using these tag names. Example:
colour background=blue;
by default, the enumerators are assigned integer values starting with 0 for the first enumerator, 1 for the second, and so on.
We can over-ride the default by explicitly assigning integer values to the enumerators. For example,
enum colour(red, blue=4, green=8);
enum colour(red=5, blue, green);
in the first case, red=0 by default. In the second case, blue is 6 and green is 7.
-Arrays
-functions
-pointers
Symbolic Constant
There are two ways of creating symbolic constants in C++:
- Using the qualifier const.
- Defining a set of integer constant using enum keyword.
const int size=10;
char name[size];
Another method of naming integer constants is as follows:
enum(X,Y,Z);
this defines X,Y,Z as integer constants with values 0,1 and 2 respectively. This equivalent to
const X=0;
const Y=1;
const Z=2;
we can also assign values to X,Y, and Z explicitly.
enum( X=100, Y=50, Z=200);
Such values can be any integer values.
Declaration of Variables
C++ allows the declaration of variables anywhere in the scope. This means that a variable can be declared right at the place of its first use. This makes the program much easier to write and reduces the errors that may caused by having to scan back and forth. See the example bellow:
|
main ()
{
Float x; // declaration
Float sum=0;
For(int i=0;i<5; i++) // declaration
{
cin >>x;
sum=sum+x;
}
float average; //declaration
average=sum/i;
cout << average;
}
|
Dynamic Initialization of Variables
C++ permits initialization of variables at run time using expressions at the place of declaration. For example :
int n=strlen(string);
float area =3.14159 * rad * rad;
Reference Variables
A reference variable produces 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 that variable. A reference variable is created as follows
Data type & reference-name=variable-name
Example:
float total=100;
float & sum=total;
the statements
cout <<total;
cout<< sum;
both print the value 100. the statement
total=total+10;
will change the value of both total and sum to 110. likewise the assignment
sum=0;
will change the value of the variables to zero.
Questions:
- What is the role of keywords in programming languages?
- Define the identifier? What are the rules for naming the identifiers?
- define : enumerated data types, reference variables.
المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .