2.0 The Basic Element of C++
There are some basic elements of C++ language. Those basic elements include names, variable, binding, built-in and user-defined data types, scope, lifetime of variables or objects, and referencing environments. First of all, the names of variables in the C++ language are referred to as identifiers. Identifiers are names that are given to various program elements such as variables, functions and arrays. It consists of letters , digits, and underscore character ( _ ), in any order, but the first character must follow the rules. It only can start with alphabet, digit and underscore. It cannot start with a digit. The identifiers are case sensitive. For example, the upper case and lower case letters are distinct. In C++, there is no limit on length of the identifier, and at least the first 1024 characters are significant. The keyword of an identifier cannot be the same as C or C++ keyword. It should not have the same names as functions that are in the C or C++ library. The example of keyword are int, float, char and others. As for reserved word, they are the essential part of language definition. The meaning of the words has already been explained to the compiler, which mean that the reserved word cannot use as a variable name.( Deka, H. (2014). Introduction To Elements Of C.).
A variable is an abstraction of a memory cell. The declaration of variable is associate with a memory location to the variable name (Diwan, A. (2014). C Tutorial.). This mean that whenever user declare a variable, a definite amount of memory location are reserved for it. Variable can be declared in multiple ways each with different memory requirements and functioning. Each variable while declaration must be a given datatype, on which the memory assigned to the variable depends. The basic variables types are bool, char, int and so on. Variable must be declared before they are used. Example:
int i;
char a;
int a, b, c;
Initialization and declaration can be done in one step too! Example:
int a = 11;
int a = 11, b == 11;
And if a variable is declared and not initialized by default, it will hold a garbage value. Beside that, if a variable is declared and try to declare it again, it will occur compile time error. Example:
int a, b;
a = 11;
b = 12;
int a = a + b;
This can be compile time error because it cannot redeclare a variable in same scope.
A binding in a program is an association of an attribute with a program component such as an identifier or a symbol. The binding time for an attribute is the time at which the binding occurs. The most common binding times for attribute are:
Basic Data Type: int, double, float, char…..
Derived Data Type: function, array, pointer, reference….
User Defined Types: Structure, Union, Class….
For address, C++ have different addresses at different time during execution. It may also have different addresses at different places in a program. If two variable names can be used to access the same memory location, they are called aliases. These aliases are created via pointer and references variables. One disadvantage of using aliases is that they are harmful to readability because the programmers must remember all of them. For value, it is an expression which cannot be evaluated any further. The member of a type are the values of that type. In C++, it use the idea of l-values and r-values. The l-values of a variable is its address while the r-value of a variable is its value. A value can be virtually any kind of data by given data type such as a string, a digit or a single letter.
Lifetime is the period of time during program execution when an identifier has memory allocated to it. A local variable’s lifetime starts when entering the block and ends when the block is exited. A global variable’s lifetime is the duration of the entire program.
Example:
while(…){ //a is allocated memory.
char a // a is in scope and has memory.
} // a’s memory is deallocated..
The scope of variable is the range of statements over which it is visible. There are two categories: Local scope and Global scope. Local scope is the scope of an identifier declared inside a block extends from the point of declaration to the end of the block. Global scope is the scope of an identifier declared outside of all blocks extends from point of declaration to the end of the entire program.
Example:
Local scope
int i;
while ( i < 0){
int x; //The scope of x is local to the body of while loop.
cin>> x;
i = i + x; // The scope of I is nonlocal to the body of while loop.
cout << i;
}
Global scope
double a; //The identifier a has a global scope.
int main(){
int x = 0;
cin>>a; //It can be referenced anywhere in the program including
if( a < 0){ // inside main and in the body of the if-else statement.
a = x;
}
else
return 0;
}
The referencing environment of a statement is the collection of all names that are visible in the statement. In static-scoped language, it is the local variables which are visible in the enclosing scope. The subprogram is active when it is execute but not yet terminated. In dynamic-scoped language, the variable is visible in all active subprograms.
Example of referencing environment in C++:
void fun1(){
int a, b, c; // int a, b, c is visible inside the fun 1.(static scope)
}
void fun2(){
int c, d, e; // int c, d, e is visible inside the fun2.(static scope)
fun1();
}
void main(){
int d, e, f; // int d, e, f is visible inside main. (static scope)
fun2();
}
The dynamic scope with calling sequence: main-> fun2 -> fun1
Variable Function Declared
There are some basic elements of C++ language. Those basic elements include names, variable, binding, built-in and user-defined data types, scope, lifetime of variables or objects, and referencing environments. First of all, the names of variables in the C++ language are referred to as identifiers. Identifiers are names that are given to various program elements such as variables, functions and arrays. It consists of letters , digits, and underscore character ( _ ), in any order, but the first character must follow the rules. It only can start with alphabet, digit and underscore. It cannot start with a digit. The identifiers are case sensitive. For example, the upper case and lower case letters are distinct. In C++, there is no limit on length of the identifier, and at least the first 1024 characters are significant. The keyword of an identifier cannot be the same as C or C++ keyword. It should not have the same names as functions that are in the C or C++ library. The example of keyword are int, float, char and others. As for reserved word, they are the essential part of language definition. The meaning of the words has already been explained to the compiler, which mean that the reserved word cannot use as a variable name.( Deka, H. (2014). Introduction To Elements Of C.).
A variable is an abstraction of a memory cell. The declaration of variable is associate with a memory location to the variable name (Diwan, A. (2014). C Tutorial.). This mean that whenever user declare a variable, a definite amount of memory location are reserved for it. Variable can be declared in multiple ways each with different memory requirements and functioning. Each variable while declaration must be a given datatype, on which the memory assigned to the variable depends. The basic variables types are bool, char, int and so on. Variable must be declared before they are used. Example:
int i;
char a;
int a, b, c;
Initialization and declaration can be done in one step too! Example:
int a = 11;
int a = 11, b == 11;
And if a variable is declared and not initialized by default, it will hold a garbage value. Beside that, if a variable is declared and try to declare it again, it will occur compile time error. Example:
int a, b;
a = 11;
b = 12;
int a = a + b;
This can be compile time error because it cannot redeclare a variable in same scope.
A binding in a program is an association of an attribute with a program component such as an identifier or a symbol. The binding time for an attribute is the time at which the binding occurs. The most common binding times for attribute are:
- Language design time
- Language implementation time
- Compile time
- Load time
- Runtime
Basic Data Type: int, double, float, char…..
Derived Data Type: function, array, pointer, reference….
User Defined Types: Structure, Union, Class….
For address, C++ have different addresses at different time during execution. It may also have different addresses at different places in a program. If two variable names can be used to access the same memory location, they are called aliases. These aliases are created via pointer and references variables. One disadvantage of using aliases is that they are harmful to readability because the programmers must remember all of them. For value, it is an expression which cannot be evaluated any further. The member of a type are the values of that type. In C++, it use the idea of l-values and r-values. The l-values of a variable is its address while the r-value of a variable is its value. A value can be virtually any kind of data by given data type such as a string, a digit or a single letter.
Lifetime is the period of time during program execution when an identifier has memory allocated to it. A local variable’s lifetime starts when entering the block and ends when the block is exited. A global variable’s lifetime is the duration of the entire program.
Example:
while(…){ //a is allocated memory.
char a // a is in scope and has memory.
} // a’s memory is deallocated..
The scope of variable is the range of statements over which it is visible. There are two categories: Local scope and Global scope. Local scope is the scope of an identifier declared inside a block extends from the point of declaration to the end of the block. Global scope is the scope of an identifier declared outside of all blocks extends from point of declaration to the end of the entire program.
Example:
Local scope
int i;
while ( i < 0){
int x; //The scope of x is local to the body of while loop.
cin>> x;
i = i + x; // The scope of I is nonlocal to the body of while loop.
cout << i;
}
Global scope
double a; //The identifier a has a global scope.
int main(){
int x = 0;
cin>>a; //It can be referenced anywhere in the program including
if( a < 0){ // inside main and in the body of the if-else statement.
a = x;
}
else
return 0;
}
The referencing environment of a statement is the collection of all names that are visible in the statement. In static-scoped language, it is the local variables which are visible in the enclosing scope. The subprogram is active when it is execute but not yet terminated. In dynamic-scoped language, the variable is visible in all active subprograms.
Example of referencing environment in C++:
void fun1(){
int a, b, c; // int a, b, c is visible inside the fun 1.(static scope)
}
void fun2(){
int c, d, e; // int c, d, e is visible inside the fun2.(static scope)
fun1();
}
void main(){
int d, e, f; // int d, e, f is visible inside main. (static scope)
fun2();
}
The dynamic scope with calling sequence: main-> fun2 -> fun1
Variable Function Declared
- a, b, c fun1
- d,e fun2
- f main