This document discusses the components of a C++ program structure and the process of using a compiler. It identifies that a C++ program consists of comments, preprocessor directives, functions, bodies, and return statements. It then explains each of these components and provides examples. The document also outlines the steps to build a C++ program using a compiler which are editing source code, compiling to object code, linking to an executable, and executing the program.
2. 8.3.1 Program Structure
• Learning Outcome
– Identify the component of C++
i. comment
ii. preprocessor directive
iii. function
iv. body
v. return statement
3. 8.3.1 Program Structure
• A computer program consists a list of
instructions written in a computer language.
//This is my first C++ program
//It prints a line of text i. comment
#include <iostream.h> ii. preprocessor
#include <stdlib.h> directive
int main()
iii. function
{
cout << "My first C++ program" ;
iv. body
system("PAUSE");
return 0; v. return
}
Fig. 1 : Example 1 statement
4. 8.3.1 Program Structure
// height.cpp i. comment
// Convert height in feet to inches
#include <iostream.h> ii. preprocessor
#include <stdlib.h> directive
int main()
iii. function
{
int feet, inches;
inches = feet * 12;
cout << “Enter feet value : ”;
cin >> feet;
cout << “Height is“ << inches ; iv. body
cout << “in.” ;
system("PAUSE");
return 0; v. return
} Fig. 2: Example 2 statement
5. 8.3.1 Program Structure
• comment
//This is my first C++ program
//It prints a line of text
/*This is my first C++ program
It prints a line of text */
• Style to insert a comment in C++.
– Text begin with two double slash (//) -
normally used for single line comment
– Text begin with /* and ends with */ - possibly
containing many lines
6. 8.3.1 Program Structure
i. comment
• The purpose to insert a comment
– To document a program
– To improve program readability – help other
people read and understand a program.
• Remember – comment do not cause the
computer to perform any action when the
program is run.
7. 8.3.1 Program Structure
i. preprocessor directive
- Preprocessor directive is a general instruction
to the C++ compiler
- # :are processed by preprocessor before
program is compiled.
- #include <iostream.h> :tells the
preprocessor to include in the program the
contents of the input/output stream header
file iostream.h
8. 8.3.1 Program Structure
i. preprocessor directive
- iostream.h
:specific file needed for programs that either
input data from keyboard or write output on the
screen.
:called as header file ( with the file extension .h)
:other examples
:#include <stdlib.h>
:#include <math.h>
9. 8.3.1 Program Structure
i. function
int main()
: the parentheses, () after main indicate that
main is a program building block called a
function.
: example given on Fig. 1; contain only one
function.
: C++ programs normally begin executing at
function main.
10. 8.3.1 Program Structure
i. Body
: the left brace, {, must begin the body of every
function.
: a corresponding right brace, }, must end the
body of each function.
: examples: Refer to Fig.1 and Fig.2 ( body
segment )
11. 8.3.1 Program Structure
i. Body
: The body of main may consist of:
i) variable declaration and reserve word
e.g. : int feet, inches;
reserved word variables
12. 8.3.1 Program Structure
i. Body
: The body of main may consist of:
ii) input / output console
input console: use standard input stream object -
cin and input operator, >>, to allow user type in
a value (or values)
e.g. : cin >> feet;
13. 8.3.1 Program Structure
i. Body
: The body of main may consist of:
ii) input / output console
output console: use use standard output stream
object - cout and the output operator, <<, to
output the message.
e.g. : cout << “Height is“ <<inches;
14. 8.3.1 Program Structure
i. Body
: The body of main may consist of:
iii) C++ statement
: Every statement must end must end with
semicolon (;).
e.g. :
i. cout <<"My first C++ program”;
ii. inches = feet * 12;
15. 8.3.1 Program Structure
i. return statement
return 0;
: is included at the end of main function.
: the C++ keyword return statement is used at
the end of main, the value 0 indicates that
the program has terminated successfully.
:the right brace,}, indicates the end of main.
17. 8.3.2 Using Compiler
program.cpp C++ program.obj
(C++ source code) compiler (object code)
Linker
C++ Library
program.exe
(executable program)
Fig. 1: Building a C++ Program
18. 8.3.2 Using Compiler
• Edit – enter the program statement
• To write a C++ program, you need to enter the
program statements by using:
– Text editor (e.g. Notepad, Microsoft Word etc.)
– Integrated Development Environment (IDE)(e.g.
Dev C++, Borland C++ etc.)
• The complete program statements called
“source code”.
code
19. 8.3.2 Using Compiler
• Compile – translating C++ into machine code
Compil
(also called “object code”)
– Compiler determines the syntax error.
• Link – Run the linker – combine the machine
code with code from C++ library; after
compiles is successful.
20. 8.3.2 Using Compiler
• Execute Program – an application can be run.
– Finally, the computer, under the control of its
CPU, executes the program one instruction at a
time.
Remember :
Compiler detects grammatical (syntax) error NOT
the program-logic error
21. 8.3.2 Using Compiler
Tool Step Product
Editor Edit
Source code
Compiler Compile
Object code
Linker Link
Executable image
Run
Result / Output
cout semicolon. (also known as the statement terminator).
semicolon. (also known as the statement terminator).
semicolon. (also known as the statement terminator).
semicolon. (also known as the statement terminator).
semicolon. (also known as the statement terminator).
IDE explanation Borland also provides several versions of C++Builder that contain graphical user interfaces (GUIs). These GUIs are formally called integrated development environments (IDEs) and enable the developer to edit, debug and test programs quickly and conveniently. Using an IDE, many of the tasks that involved tedious commands can now be executed via menus and buttons. IDE provides user-friendly menus and tools to perform all the functions
Example of syntax error int x missing semi colon. Cout<< x; standard output stream object – cout – must use small letter Example of program-logic error. Use wrong formula to calculate area of rectangle.