DEVELOPING YOUR FIRST C++ PROGRAM

The following three steps are required to create and translate a C++ program:

1. First, a text editor is used to save the C++ program in a text file. In other words, the source code is saved to a source file. In larger projects the programmer will normally use modular programming. This means that the source code will be stored in several source files that are edited and translated separately. 

2. The source file is put through a compiler for translation. If everything works as planned, an object file made up of machine code is created. The object file is also referred to as a module.

 3. Finally, the linker combines the object file with other modules to form an executable file. These further modules contain functions from standard libraries or parts of the program that have been compiled previously.

It is important to use the correct file extension for the source file’s name. Although the file extension depends on the compiler you use, the most commonly found file extensions are .cpp and .cc. Prior to compilation, header files, which are also referred to as include files, can be copied to the source file. Header files are text files containing information needed by various source files, for example, type definitions or declarations of variables and functions. Header files can have the file extension .h, but they may not have any file extension. The C++ standard library contains predefined and standardized functions that are available for any compiler. Modern compilers normally offer an integrated software development environment, which combines the steps mentioned previously into a single task. A graphical user interface is available for editing, compiling, linking, and running the application. Moreover, additional tools, such as a debugger, can be launched.

C++ Program Structure

Let us look at a simple code that would print the words Hello World.
Let us look at the various parts of the above program 
  • The C++ language defines several headers, which contain information that is either necessary or useful to your program. For this program, the header <iostream> is needed.
  • The line using namespace std; tells the compiler to use the std OR standard namespace. Namespaces are a relatively recent addition to C++.
  • The line int main() is the main function where program execution begins.
  • The next line cout << "Hello World."; causes the message "Hello World
  • " to be displayed on the screen.
  • The next line return 0; terminates main( )function and causes it to return the value 0 to the calling process.

Comments