• Like
8.3 program structure (1 hour)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

8.3 program structure (1 hour)

  • 439 views
Published

Program structure

Program structure

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
439
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
4
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • Others example: string.h , conio.h, dos.h, time.h, graphics.h
  • 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.

Transcript

  • 1. 8.3 Introduction to C++ 8.3.1 Program Structure 8.3.2 Using Compiler
  • 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> directiveint 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> directiveint 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 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.
  • 7. 8.3.1 Program Structurei. 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 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>
  • 9. 8.3.1 Program Structurei. functionint 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 Structurei. 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 Structurei. 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 Structurei. 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 Structurei. Body: The body of main may consist of:ii) input / output consoleoutput 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 Structurei. 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 Structurei. return statementreturn 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.
  • 16. 8.3.2 Using Compiler• Learning Outcome – Edit – Compile – Link – Execute program
  • 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 ProductEditor Edit Source codeCompiler Compile Object codeLinker Link Executable image Run Result / Output