The document discusses writing and compiling C++ programs. It explains that C++ source code is written in a text editor and saved with a .cpp extension. The source code is then compiled, which translates it into machine-readable object code saved as .obj. This object code is then linked with libraries to create an executable .exe file that can be run on a computer. It provides examples of code structure, using preprocessor directives like #include to add header files, and defines the main function where program statements are written.
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
29 views
Structure of C++ Program
The document discusses writing and compiling C++ programs. It explains that C++ source code is written in a text editor and saved with a .cpp extension. The source code is then compiled, which translates it into machine-readable object code saved as .obj. This object code is then linked with libraries to create an executable .exe file that can be run on a computer. It provides examples of code structure, using preprocessor directives like #include to add header files, and defines the main function where program statements are written.
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17
c/c/++ parogram
BY. Haris malik
Writing C++ Programs The source code of a c++ program is stored on the disk with file extension cpp (c plus plus). Any text editor can be used to write and edit C++ source code. the Borland C++ and Turbo C++ have their own editors for writing the source code. The source code is written in the editor and then it is compiled. Contd… The C++ compiler translates a C++ source program into the machine code. The machine code is called object code. It is stored in a new file with extension obj. The object code is then linked to the libraries. After linking the object code to the libraries, an executable file with extension exe is created. Contd… For example, a source code of program in C++ is written and stored in file first.cpp. After compilation, the object code is saved in the file first.obj and the executable code is stored in file first.exe. Structure of a C++ program A C++ program consists of three main parts. Preprocessor Directives The main() Function C++ Statements Preprocessor Directives The instruction that are given to the compiler before the beginning of the actual program are called preprocessor directives. Also known as compiler directives. The compiler adds special code from these directives into the program, at the time of compilation. Contd… These preprocessor directives normally starts with a number sign (#) and the keyword include. For example, preprocessor directives are used to include header files in the program. Let us consider a simple program example. Example # include <iostream.h> # include <conio.h> void main () { cout<<“hello world”; getch(); } What are Header Files? Header file is a C++ source file that contains definitions of library functions/objects. Header files are added into the main program at the compilation of the program. A header file is added if the function/object defined in it is to be used in the program. Contd… The preprocessor directives include is used to add a header file into the program. The name of the file is written in angle brackets (< >) after #include directive. C++ has a large number of header files in which library functions are defined. The header file must be included in the program before calling its function in the program. Contd… A single header file may contain a large number of built-in functions. For example, the header file iostream.h has definitions of different input and output objects and functions. It is included in the example program because its object cout is used in the program. Contd… The syntax to include a header file is: # include <name of the header file> The main() Function The main() function indicates the beginning of a C++ program. The main() must be included in every C++ program. When a C++ program is executed, it is the first function that is executed. If main() function is not included, the program is not compiled and an error message is generated. C++ Statements The statements of the program are written inside the main() function between the curly braces ({}). Each statement in a C++ program must end with a semi colon (;) Program Examples on Data Types Program No. 1: Write a program which will ask to enter the distance in miles. The program should convert the distance entered in mile to kilometers. One mile is equal to 1.607 kilometers. Program No. 2: Write a program which will calculate the sum and average marks of all students of BCS-1st in the subject of programming. Contd… Program No 2: Write a program which will calculate the area of a circle. The formulae for the calculation of circle area is: A = π r2 Quantities required are given as under. π = 3.1416 r = 10.8 What is Compiler? A compiler is a program that translates a source program written in some high-level programming language (such as C++) into machine code.
Instant download Android for Absolute Beginners - Getting Started with Mobile Apps Development Using the Android Java SDK 1st Edition Grant Allen pdf all chapter
Instant download Android for Absolute Beginners - Getting Started with Mobile Apps Development Using the Android Java SDK 1st Edition Grant Allen pdf all chapter