DATA HANDLING
BASIC DATA TYPES
C++ supports a large number of data types. The built in or basic data types supported by C++ are integer, floating point and character. These are summarized in table along with description and memory requirement
Type | Byte | Range | Description |
Int | 2 | -32768 to +32767 | Small whole number |
long int | 4 | -2147483648 to +2147483647 | Large whole number |
Float | 4 | 3.4x10-38 to 3.4x10+38 | Small real number |
Double | 8 | 1.7x10-308 to 1.7x10+308 | Large real number |
long double | 10 | 3.4x10-4932 to 3.4x10+4932 | Very Large real number |
Char | 1 | 0 to 255 | A Single Character |
Variables
It is a location in the computer memory which can store data and is given a symbolic name for easy reference. The variables can be used to hold different values at different times during the execution of a program.
To understand more clearly we should study the following statements:
Total = 20.00;
In statement a value 20.00 has been stored in a memory location Total.
Before a variable is used in a program, it has to be defined. This activity enables the compiler to make available the appropriate type of location in the memory. The definition of a variable consists of the type name followed by the name of the variable. For example, a variable Total of type float can be declared as shown below:
float Total;
Similarly the variable Net can also be defined as shown below:
float Net;
To understand more clearly we should study the following statements:
Total = 20.00;
In statement a value 20.00 has been stored in a memory location Total.
Before a variable is used in a program, it has to be defined. This activity enables the compiler to make available the appropriate type of location in the memory. The definition of a variable consists of the type name followed by the name of the variable. For example, a variable Total of type float can be declared as shown below:
float Total;
Similarly the variable Net can also be defined as shown below:
float Net;
Input/Output (I/O)
C++ supports input/output statements which can be used to feed new data into the computer or obtain output on an output device such as: VDU, printer etc. It provides both formatted and unformatted stream I/O statements. The following C + + streams can be used for the input/output purpose.
Stream Description
cin console input
cout console output
In addition to the above I/O streams, two operators <<and >>are also used. The operator << is known as put to or bit wise shift operator. The operator>> is known as extraction or get from operator.
cout << “My first computer";
Once the above statement is carried out by the computer, the message "My first computer" will appear on the screen.
cin is the standard input stream (keyboard) and it can be used to input a value entered by the user from the keyboard. However, the get from operator>> is also required to get the typed value from cin and store it in the memory location.
Let us consider the following program segment:
int marks;
cin >> marks;
In the above segment, the user has defined a variable marks of integer type in the first statement and in the second statement he is trying to read a value from the keyboard.
cin console input
cout console output
In addition to the above I/O streams, two operators <<and >>are also used. The operator << is known as put to or bit wise shift operator. The operator>> is known as extraction or get from operator.
cout << “My first computer";
Once the above statement is carried out by the computer, the message "My first computer" will appear on the screen.
cin is the standard input stream (keyboard) and it can be used to input a value entered by the user from the keyboard. However, the get from operator>> is also required to get the typed value from cin and store it in the memory location.
Let us consider the following program segment:
int marks;
cin >> marks;
In the above segment, the user has defined a variable marks of integer type in the first statement and in the second statement he is trying to read a value from the keyboard.
Type conversion
The process in which one pre-defined type of expression is converted into another type is called conversion. There are two types of conversion in C++.
1. Implicit conversion
2. Explicit conversion
Implicit conversion
Data type can be mixed in the expression. For example
double a;
int b = 5;
float c = 8.5;
a = b * c;
1. Implicit conversion
2. Explicit conversion
Implicit conversion
Data type can be mixed in the expression. For example
double a;
int b = 5;
float c = 8.5;
a = b * c;
When two operands of different type are encountered in the same expression, the lower type variable is converted to the higher type variable. The following table shows the order of data types.
Order of data types | |
Data type long double double float long int char | order (highest)
To
(lowest)
|
The int value of b is converted to type float and stored in a temporary variable before being multiplied by the float variable c. The result is then converted to double so that it can be assigned to the double variable a.
Explicit conversion
It is also called type casting. It temporarily changes a variable data type from its declared data type to a new one. It may be noted here that type casting can only be done on the right hand side the assignment statement.
T_Pay = double (salary) + bonus;
Initially variable salary is defined as float but for the above calculation it is first converted to double data type and then added to the variable bonus.
It is also called type casting. It temporarily changes a variable data type from its declared data type to a new one. It may be noted here that type casting can only be done on the right hand side the assignment statement.
T_Pay = double (salary) + bonus;
Initially variable salary is defined as float but for the above calculation it is first converted to double data type and then added to the variable bonus.
Constants
A number which does not change its value during execution of a program is known as a constant. Any attempt to change the value of a constant will result in an error message. A constant in C++ can be of any of the basic data types, const qualifier can be used to declare constant as shown below:
const float pi = 3.1415;
The above declaration means that Pi is a constant of float types having a value 3.1415.
Examples of valid constant declarations are:
const int rate = 50;
const float pi = 3.1415;
const char ch = 'A';
const float pi = 3.1415;
The above declaration means that Pi is a constant of float types having a value 3.1415.
Examples of valid constant declarations are:
const int rate = 50;
const float pi = 3.1415;
const char ch = 'A';
Structure of C++ Program
The structure of a C++ program is given below:
#include<header file>
main ()
{
……
……
}
#include<header file>
main ()
{
……
……
}
A C++ program starts with function called main ( ). The body of the function is enclosed between curly braces. The program statements are written within the braces. Each statement must end by a semicolon;(statement terminator).
A C++ program may contain as many functions as required. However, when the program is loaded in the memory, the control is handed over to function main ( ) and it is the first function to be executed.
// This is my first program is C++
/* this program will illustrate different components
of a simple program in C++ */
# include <iostream.h>
void main ( )
{
cout <<"This is my first program in C++";
cout << "\n";
}
When the above program is compiled, linked and executed, the following output is displayed on the VDU screen.
This is my first program in C++
/* this program will illustrate different components
of a simple program in C++ */
# include <iostream.h>
void main ( )
{
cout <<"This is my first program in C++";
cout << "\n";
}
When the above program is compiled, linked and executed, the following output is displayed on the VDU screen.
This is my first program in C++
Various components of this program are discussed below:
Comments
First three lines of the above program are comments and are ignored by the compiler. Comments are included in a program to make it more readable. If a comment is short and can be accommodated in a single line, then it is started with double slash sequence in the first line of the program. However, if there are multiple lines in a comment, it is enclosed between the two symbols /* and */
include <iostream.h>
The lines in the above program that start with symbol are called directives and are instructions to the compiler. The word include with '#' tells the compiler to include the file iostream.n into the file of the above program. File iostream.h is a header file needed for input/ output requirements of the program. Therefore, this file has been included at the top of the program.
void main ( )
The word main is a function name. The brackets ( ) with main tells that main ( ) is a function. The word void before main ( ) indicates that no value is being returned by the function main (). When program is loaded in the memory, the control is handed over to function main ( ) and it is the first function to be executed.
The curly brackets and body of the function main ( )
A C++ program starts with function called main(). The body of the function is enclosed between curly braces. The program statements are written within the brackets. Each statement must end by a semicolon, without which an error message in generated.
Comments
First three lines of the above program are comments and are ignored by the compiler. Comments are included in a program to make it more readable. If a comment is short and can be accommodated in a single line, then it is started with double slash sequence in the first line of the program. However, if there are multiple lines in a comment, it is enclosed between the two symbols /* and */
include <iostream.h>
The lines in the above program that start with symbol are called directives and are instructions to the compiler. The word include with '#' tells the compiler to include the file iostream.n into the file of the above program. File iostream.h is a header file needed for input/ output requirements of the program. Therefore, this file has been included at the top of the program.
void main ( )
The word main is a function name. The brackets ( ) with main tells that main ( ) is a function. The word void before main ( ) indicates that no value is being returned by the function main (). When program is loaded in the memory, the control is handed over to function main ( ) and it is the first function to be executed.
The curly brackets and body of the function main ( )
A C++ program starts with function called main(). The body of the function is enclosed between curly braces. The program statements are written within the brackets. Each statement must end by a semicolon, without which an error message in generated.
No comments:
Post a Comment