Upload
akmalfahmi
View
501
Download
1
Tags:
Embed Size (px)
Citation preview
8.3 Introduction to C++8.3 Introduction to C++
8.3.1 Program Structure8.3.1 Program Structure
8.3.2 Using Compiler8.3.2 Using Compiler
8.3.1 Program Structure8.3.1 Program Structure
• Learning Outcome– Identify the component of C++
i. comment
ii. preprocessor directive
iii. function
iv. body
v. return statement
8.3.1 Program Structure8.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
#include <iostream.h>#include <stdlib.h>
int main()
{ cout << "My first C++ program" ;
system("PAUSE"); return 0;}
i. comment
ii. preprocessor directive
iii. function
iv. body
v. return statement Fig. 1 : Example 1
8.3.1 Program Structure8.3.1 Program Structure// height.cpp // Convert height in feet to inches
#include <iostream.h>#include <stdlib.h>
int main()
{ int feet, inches;
inches = feet * 12;cout << “Enter feet value : ”;cin >> feet;cout << “Height is“ << inches ;cout << “in.” ;
system("PAUSE"); return 0;}
i. comment
ii. preprocessor directive
iii. function
iv. body
v. return statement Fig. 2: Example 2
8.3.1 Program Structure8.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
8.3.1 Program Structure8.3.1 Program Structurei. 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.
8.3.1 Program Structure8.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.3.1 Program Structure8.3.1 Program Structurei. 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>
8.3.1 Program Structure8.3.1 Program Structurei. 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.
8.3.1 Program Structure8.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 )
8.3.1 Program Structure8.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
8.3.1 Program Structure8.3.1 Program Structurei. Body
: The body of main may consist of:
ii) inputinput / output consoleconsole
input console: use standard input stream object - cin and input operator, >>, to allow user type in a value (or values)
e.g. : cin >> feet;
8.3.1 Program Structure8.3.1 Program Structurei. Body
: The body of main may consist of:
ii) input / output consoleoutput console
output console: use use standard output stream object - cout and the output operator, <<, to output the message.
e.g. : cout << “Height is“ <<inches;
8.3.1 Program Structure8.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;
8.3.1 Program Structure8.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.
8.3.2 Using Compiler8.3.2 Using Compiler
• Learning Outcome– Edit– Compile– Link– Execute program
8.3.2 Using Compiler8.3.2 Using Compiler
program.cpp (C++ source code)
C++ compiler
program.obj (object code)
C++ Library
Linker
program.exe(executable program)
Fig. 1: Building a C++ Program
8.3.2 Using Compiler8.3.2 Using Compiler
• EditEdit – enter the program statemententer 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 codesource code”.
8.3.2 Using Compiler8.3.2 Using Compiler
• CompilCompile – translating C++ into machine code translating C++ into machine code (also called “object code”) – Compiler determines the syntax error.
• Link Link – Run the Run the linkerlinker – combine the machine code with code from C++ library; after compiles is successful.
8.3.2 Using Compiler8.3.2 Using Compiler
• Execute Program Execute Program – an application can be run.an application can be run. – Finally, the computer, under the control of its
CPU, executes the program one instruction at a time.
Remember : Remember :
Compiler detects grammatical (syntax) error NOT the program-logic error
8.3.2 Using Compiler8.3.2 Using Compiler
Editor
Compiler
Linker
Run
Link
Compile
EditSource code
Object code
Executable image
Result / Output
ToolTool StepStep ProductProduct