Applications of C++
   
• Real time systems 
• Simulation and modeling 
• Object-oriented databases 
• AI and Expert Systems 
• Neural networks and parallel programming 
• CAD/CIM system 

First C++ Program

Program Description 

C++ programs must contain a function called main(), from which execution of program starts. The function main() is designated as the starting point of the program execution and the user defines it. It cannot be overloaded and its syntax type is implementation dependent. Therefore, the number of arguments and their data-type is dependent on the compiler. Let’s have a close at a very simple C++
program which displays Hello World on the screen: -

The header file iostream.h supports streams programming features by including predefined stream
objects. The C++’s stream insertion operator, << sends the message 
“Welcome to C++” to the pre-defined console object, count, which in turn prints on the console.

The various components of the program hello.cpp are discussed in the following section:  

Comment Line    

The statement that starts with symbols // is treated as comment. Hence, the compiler ignores the complete line starting from the // character pair.

The character .cpp in hello. tells the compiler that it is a C++ program. (However, the extension is compiler dependent). 

Preprocessor Directive 

The preprocessor directive #include<iostream.h> includes all the statements of the header file iostream.h. It contains instructions and predefined constants that will be used in the program. It plays the same role as that of the stdio.h of C. The header file iostream.h contains declarations that are needed by the cout and cin objects. There are a number of such preprocessor directives provided by the C++ library, and they have to be included depending on the built-in functions used in the program. In effect, these directives are processed before any other executable statements in the source file of the program by the compiler. 

Function Decelerator 

The third line of the program is 

 void main()

The C++ program consists of a set of functions. Every C++ program must have one function with name main, from here the execution of the program begins. The name main is a special word (not a reserved word) and must not be invoked anywhere by the user. The names of the functions (except main) are coined by the programmer. A pair of parentheses, which may or may not contain arguments, follows the function name. In this case, there are no arguments, but still the parenthesis pair is mandatory. Every function is supposed to return a value, but the function in this example does not return any value. Such function names must be preceded by the reserved word void. 

Compilation Process 

The C++ program hello.cpp, can be entered into the system using any available text editor. Some of the most commonly available editors are Norton editor (ne), edline,edit,vi (most popular editor in UNIX environment). The program coded by the programmer is called the source code. This source code is supplied to the compiler for converting it into the machine code.

C++ programs make use of libraries. A library contains the object code of standard functions. The object codes of all functions used in the program have to be combined with the program written by the programmer. In addition, some start-up code is required to produce an executable version of the program. This process of combining all the required object codes and the start-up is called linking and the final product is called the executable code.

Most of the modern compilers support sophisticated features such as multiple window editing, mouse support, on-line help, project management support, etc. One such compiler is Borland C++. It can be invoked through command-line integrated development environment. 

Command – Line Compilation 

Most of the compilers support the command line compilation of a program. All the required arguments are passed to the compiler from the command line. For the purpose of discussion, consider the Borland C++ compiler. (however this process is implementation dependent.)
 
The command – line compiler is invoked by issuing the command:

 tcc filename.cpp (in the case of Turbo C++)
 bcc filename.cpp (in the case of Borland C++)

at the DOS prompt. It creates an object file filename.obj, and an executable file through the explicit issue of the linking command : 

 tlink filename1.obj filename2.obj <library name>

The library file can also be passed as a parameter to the linker for binding functions defined in it. To create the executable of hello.cpp, issue the command bcc hello.cpp at the MS-DOS prompt.