I am beginning a series of articles about C++ programming language. So lets begin with a simple C++ program as shown below.
#include
int main()
{
cout<<"My first C++ code"<<"\t"; // This is a C++ comment
return 0;
}
This piece of code when executed, outputs a line of text " My first C++ code" on your computer screen. Lets analyse this piece of code in detail.
- iostream.h - is called a header file
- main() - is called a function
- cout - is called an identifier
- << - is an output operator called as "insertion" or "put to" operator
- #include - is called a preprocessor directive
- int - is called a data type that represents an integer data
- return - is a keyword in C++ , used to return values to a particular function
Lets begin with main() function:-
Every C++ program must contain a main() function. We all know that a program is read by the compiler -line by line, as part of compilation. The function named as main() is the point of entry to a C++ program. The compiler enters the program through main() function and then it reads each line of code. The program terminates at the return 0 statement written at the last line of main function code block.
The code block of main function is:-
int main(){function code blocks;return 0;}
In C++ the main function by default is of the type integer (denoted by keyword int). This means that at the end of the code block that forms the main() function, there should be a line of code that returns an integer value to main() function. This is achieved with the return 0 statement. This statement sends the value '0' to the main function and hence denotes the "end of program"
Note:- The function code blocks and statements of main() function are placed inside 2 parentheses { }
Note:- Each line of statement inside a C++ program must end with a semicolon. This is to notify the compiler about "end of statement" or "end of line".
Analyzing the statement beginning with cout<<
cout<<"My first C++ code"<<"\t"; // This is a C++ comment
cout is an identifier (pronounced as C out) that represents the standard output stream. cout is a predefined object in C++, which represents the standard output stream and it's declaration is given inside the header file iostream.h. Usually the output stream will be your computer monitor.
<< is known as the insertion operator (or put to operator). It inserts contents in its right side to the object in its left side.
\t - is an escape sequence which prints a tab space on the output monitor. Similalry \n is another escape sequence which prints a new line on the output monitor.You can read more about escape seuqnces here:-Escape seuqnces in C.
\\ (double slash) is a symbol for representing comments in a C++ program. \\ is usually used to represent a single line of comment. For representing multiple line of comments we may use the comment symbol of C programming language /* */
# is a preprocessor and include is a preprocessor directive. iostream.h is the header file that contains declarations of identifiers like cout, cin and operators like << and >>
After reading the above article, try running this code:-
#include
#include // Header file to use library function getch() in the program
int main()
{
cout<<"My first C++ code"<<"\t"; // This is a C++ comment
getch(); /* Function to hold the ouput screen "PAUSED" until we press a key*/
return 0;
}
Okay! That's enough for an introduction to C++ programming, I guess. Now lets talk a little bit about C++ in general. It is an object oriented programming (OOP) language developed by a scientist named Bjarne Stroustrup (AT&T Bell Laboratories). He made the language by combining the features of classic C (developed by Dennies Ritchie) with the some other features (mainly object oriented programming featureclasses) of another language Simula67. So C++ is basically an extension of C programming language with the addition of class construct of Simula67. Stroustrup called the new language as 'C with classes'. Later in 1983, its name was changed to C++
0 commentaires:
Enregistrer un commentaire