2.2 Steps to Create and Execute a C++ Program
2.3 Flowchart for Creating a Source File, Compiling, Linking and Executing in C++
2.5 Typical C++ Environment (Borland C++)
2.6 Structure of a C++ Program
A programmer must know rules and steps to develop programs with C++. Programs given in this book are developed using Borland’s C++ IDE compiler version 3.0. Let us try to follow the steps for creating, compiling and running programs under Borland’s C++ IDE compiler.
For creating and executing a C++ program, one must follow various phases. The C++ program is created with an editor and saved with extension .cpp. Following this step, compilation and execution operations are to be performed. The following phases are essential in ‘C++’ when a program is to be executed in MS-DOS mode.
We would now see the steps in each phase.
The following steps are necessary for execution of a program in C++.
A flowchart for implementing the aforesaid steps is given in Figure 2.1
Figure 2.1 Flowchart for creating a C++ program, compiling and linking
The process of compiling, linking and running a program in C++ is elaborated in the following section.
C++ programs can be implemented on different operating systems such as UNIX, Linux, Windows, and DOS, etc. C++ programs executed under DOS environment can also run successfully under Windows, because we have to follow the same programming rules on both platforms. Only the C++ execution environments are different.
Various types of C++ compilers are described below.
In the following section, brief information for creating, compiling, and executing C++ programs are described using Borland C++ compiler.
In this section, brief information is given about the different platforms for C++ implementation, and execution of C++ programs using Borland C++ compiler under the Windows platform is described.
STEP 1: OPEN ANY TEXT EDITOR
Unix/Linux platform:
Use text editors such as Gedit, Kwrite, Medit, ed, VI, etc.
Windows platform:
Use text editors such as Notepad, Wordpad, etc.
Alternately, one can use integrated Borland C++ compiler, which supports its own text editor. Once Turbo C++ is installed, it automatically creates a directory turboc3 and subdirectory bin. Follow the path c: urboc3 and click the executable file tc. One can use desktop shortcut for turboc3. Turbo C++ screen appears, which is blank initially and has a menu bar at the top, which is shown as follows.
STEP 2: WRITE THE CODE FOR THE PROGRAM
Write the code to fulfill the requirements of the program, if possible using the proper syntax and indentation using an editor.
Alternatively, create a source program using integrated Turbo C++ editor. Go to the File menu and select New. The file name NONAME00.CPP appears on the screen at the centre of the window. Check whether your source program is typed correctly. If mistakes are found, correct them.
STEP 3: SAVE THE FILE WITH .CPP AS AN EXTENSION
Now press F2 to save your program or use Save option with .cpp extension. You can change the file name and save it using the Save option in File menu. In the following window the file is saved with ABC.CPP.
STEP 4: COMPILE THE PROGRAM
Unix/Linux platform:
Open the terminal window; change the working directory to the directory where the program file is stored.
Compile the program using the command: g++ filename.cpp
It will display the errors and warnings. If there are any errors and warnings, correct them and save the corrected file again.
After successful compilation, file a.out will be created in the same directory.
Windows platform:
Use IDE such as Turbo C++.
From the File menu, open the program file and compile it using ‘Compile’ option in Compile menu or use Alt+F9.
It will display the errors and warnings. If there are any errors or warnings, correct them and save the corrected file again, compile again. If program is correct, then the window shows:
After successful compilation, a file filename.obj will be created in the same directory. The following window shows the compilation of a program named ABC.CPP.
Unix/Linux platform:
Use command ./a.out to run the program.
Windows platform:
Use ‘Run’ option from Run menu of Turbo C++ IDE or use ctrl+F9 and see the output as per program.
The following window shows execution of ABC.CPP program.
C++ programs consist of objects, classes, functions, variables, and other statements. Figure 2.2 shows four different parts of a C++ program.
Preprocessor directives include header files |
Class declaration or definition |
Class function definitions |
The main() function and program |
Fig. 2.2 Parts of C++ program
The following is a preprocessor directive that can be used in C++ programs besides many more.
The #define directive
For example
#define PI 3.14
This statement defines macro templates. During preprocessing, the preprocessor replaces every occurrence of PI (identifier) with 3.14 (substitute value). Here, PI is a macro template and 3.14 is its macro expansion. The macro templates are generally declared with capital letters for quick identification. One can also define macros with small letters. The macro templates and its expansions must be separated with at least one blank space. It is not necessary to provide space between # and define. It is optional to the programmer. To increase readability, the programmer should provide space.
Include header files section
One can write modular programs by making use of the #include directives. It is used to read the content of file that is included at the beginning of the program. Another file can be included at the start of the program with #include name_of_the_file.cpp.
The C++ program also depends on some header files for function prototypes. They are used in the program. Each header file is to be extended with a .h extension. The file should be included using #include directive as per the format given below.
Example: #include<iostream.h> or #include“iostream.h”
In this example, <iostream.h> header file is included at the beginning by the programmer. This preprocessor directive is essential, which uses input/output statements such cin and cout. All the definitions and prototypes of the function defined in this file are available in the current program. This file also gets compiled with the original program.
Besides the <iostream.h> header file, numerous other header files are used in programs in different chapters. Details of such header files are not given, which is beyond the scope of this discussion.
A simple C++ program is as follows:
#include<iostream.h>
#include<conio.h>
// First Simple program
{
clrscr();
cout<<“ Hello! Welcome to the world of C++”;
return 0;
}
Comment symbols similar to those used in C++ can also be used. In C, comment symbols /* and */ are used in programs. These symbols are also permitted in C++ programs.
In C++, int main() returns an integer value to operating system. Operating system checks whether the program is executed successfully. The return value of the function int main() is used for successful execution of the program. If the returned value is zero (0), it means that the program is executed successfully. A non-zero value indicates that the program execution was unsuccessful.
The prototype of main() function in C++ is as follows. The return statement is used in the program at the end of the main() function to return a value. If there is no return statement, the compiler displays a warning message “Function should return a value”. The default int is to be returned to the main() in case int is not specified in the main().
int main();
int main(int argc, char *argv[]);
C++ programs are coupled with class. A class encapsulates data and associated member functions. A detailed description is given in the classes and objects chapter in this book.
This part describes library functions that are provided with the C++ compiler. The source code for these library functions are not provided, but they are in a compiled form. We have to link the code of the library functions with the compiled programmer’s code to form the entire program. The library functions perform operations such as managing memory, reading and writing disk files, input/output, mathematical operations, etc.
Numerous library functions are supported by different files. For example, math.h supports functions such as abs(), pow(), sqrt(), floor(), ceil(), etc. The conio.h supports functions such as getch(), getche(), clrscr(), gotoxy(), import(), etc. These files should be included while library functions are to be used in the programs. They are provided with C++ compiler. These files have the extension .h and they are called as header files. Table 2.1 gives a list of some of the header files and functions supported by them. The programmer is advised to refer library functions from the C++ compiler.
Table 2.1 The header files and library functions
Name of the header file |
Function of the header file |
List of the few functions supported by the header files |
---|---|---|
alloc.h |
Memory management functions |
calloc(), malloc(), free(), realloc(), etc. |
complex.h |
Complex math functions |
asin(), atan(), arg(), tanh(), etc. |
ctype.h |
Automatic type conversion functions |
toupper(), tolower(), islower(),isupper(), etc. |
dos.h |
Perform DOS functions |
getdate(), gatetime(), int 86(),sleep(),etc. |
graphics.h |
Graphics functions |
arc(), bar(), circle(), getx(), etc. |
process.h |
Process functions |
exit(), abort(), system(), etc. |
stdio.h |
Standatrd input/output functions |
puts(), gets(), fopen(), fclose(),printf(), getchar(), putchar(),etc. |
stdlibh.h |
Standard library functions |
ato()l, atoi(), time(), abort(), etc. |
string.h |
Manipulates various string operations with different functions |
strcpy(), strcat(), strlen(), etc. |
time.h |
Declare functions that manipulate time and date |
time(), stime(), difftime(), local-time(), etc. |
Step 1: Open any text editor.
Step 2: Write the code for the program.
Step 3: Save the file with .cpp as an extension.
Step 4: Compile the program.
Step 5: Run the program.
(A) Answer the following questions
(B) Answer the following by selecting the appropriate options