Simple C++ Program

Let us begin with a simple example of a C++ program that prints a string on the screen.


This simple program demonstrates several C++ features.
Program feature
Like C, the C++ program is a collection of function. The above example contain only one function main(). As usual execution begins at main(). Every C++ program must have a main(). C++ is a free form language. With a few exception, the compiler ignore carriage return and white spaces. Like C, the C++ statements terminate with semicolons.

Comments
C++ introduces a new comment symbol // (double slash). Comment start with a double slash symbol and terminate at the end of the line. A comment may start anywhere in the line, and whatever follows till the end of the line is ignored. Note that there is no closing symbol.
The double slash comment is basically a single line comment. Multiline comments can be written as follows:
// This is an example of
// C++ program to illustrate
// some of its features
The C comment symbols /*,*/ are still valid and are more suitable for multiline comments. The following comment is allowed:
/* This is an example of
C++ program to illustrate
some of its features

*/

Output operator
The only statement in program is an output statement. The statement
Cout<<”C++ is better than C.”;

Causes the string in quotation marks to be displayed on the screen. This statement introduces two new C++ features, cout and <<. The identifier cout(pronounced as C out) is a predefined object that represents the standard output stream in C++. Here, the standard output stream represents the screen. It is also possible to redirect the output to other output devices. The operator << is called the insertion or put to operator.

The iostream File
We have used the following #include directive in the program:
#include <iostream>

The #include directive instructs the compiler to include the contents of the file enclosed within angular brackets into the source file. The header file iostream.h should be included at the beginning of all programs that use input/output statements.

Namespace
Namespace is a new concept introduced by the ANSI C++ standards committee. This defines a scope for the identifiers that are used in a program. For using the identifier defined in the namespace scope we must include the using directive, like
Using namespace std;

Here, std is the namespace where ANSI C++ standard class libraries are defined. All ANSI C++ programs must include this directive. This will bring all the identifiers defined in std to the current global scope. Using and namespace are the new keyword of C++.

Return Type of main()
In C++, main () returns an integer value to the operating system. Therefore, every main () in C++ should end with a return (0) statement; otherwise a warning an error might occur. Since main () returns an integer type for main () is explicitly specified as int. Note that the default return type for all function in C++ is int. The following main without type and return will run with a warning:




Comments

Popular posts from this blog

Application of OOP

Benefits of OOP (object oriented programming)