SlideShare una empresa de Scribd logo
1 de 21
8.3 Introduction to C++

  8.3.1 Program Structure
    8.3.2 Using Compiler
8.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 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
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
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
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.
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.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>
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.
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 )
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
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;
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;
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;
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.
8.3.2 Using Compiler
• Learning Outcome
   – Edit
   – Compile
   – Link
   – Execute program
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
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
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.
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
8.3.2 Using Compiler
 Tool          Step          Product
Editor         Edit
                            Source code

Compiler      Compile
                           Object code
Linker         Link
                         Executable image
               Run
                          Result / Output

Más contenido relacionado

La actualidad más candente

Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2
hassaanciit
 
Win avr presentation_2006
Win avr presentation_2006Win avr presentation_2006
Win avr presentation_2006
sandler789
 
Chapter 13.1.4
Chapter 13.1.4Chapter 13.1.4
Chapter 13.1.4
patcha535
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
Deepak Singh
 

La actualidad más candente (18)

Introduction
IntroductionIntroduction
Introduction
 
Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2
 
Win avr presentation_2006
Win avr presentation_2006Win avr presentation_2006
Win avr presentation_2006
 
Assignment 3
Assignment 3Assignment 3
Assignment 3
 
Intro To C++ - Class 04 - An Introduction To C++ Programming, Part III
Intro To C++ - Class 04 - An Introduction To C++ Programming, Part IIIIntro To C++ - Class 04 - An Introduction To C++ Programming, Part III
Intro To C++ - Class 04 - An Introduction To C++ Programming, Part III
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
21csharp
21csharp21csharp
21csharp
 
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part II
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part IIIntro To C++ - Class 03 - An Introduction To C++ Programming, Part II
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part II
 
Chapter 13.1.4
Chapter 13.1.4Chapter 13.1.4
Chapter 13.1.4
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
 
C Programming Lab manual 18CPL17
C Programming Lab manual 18CPL17C Programming Lab manual 18CPL17
C Programming Lab manual 18CPL17
 
Common Programming Errors
Common Programming ErrorsCommon Programming Errors
Common Programming Errors
 
2621008 - C++ 1
2621008 -  C++ 12621008 -  C++ 1
2621008 - C++ 1
 
Std 10 computer chapter 10 introduction to c language (part1)
Std 10 computer chapter 10 introduction to c language (part1)Std 10 computer chapter 10 introduction to c language (part1)
Std 10 computer chapter 10 introduction to c language (part1)
 
[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++
 
Rails VUWIT workshop
Rails VUWIT workshopRails VUWIT workshop
Rails VUWIT workshop
 
CBCS 2018 Scheme I sem Lab Manual for 18CPL17
CBCS 2018 Scheme I sem Lab Manual for 18CPL17 CBCS 2018 Scheme I sem Lab Manual for 18CPL17
CBCS 2018 Scheme I sem Lab Manual for 18CPL17
 
First cpp program
First cpp programFirst cpp program
First cpp program
 

Destacado

Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
saryu2011
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
Neveen Reda
 

Destacado (20)

Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
 
Java programming lab assignments
Java programming lab assignments Java programming lab assignments
Java programming lab assignments
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]
 
Apclass
ApclassApclass
Apclass
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
Loop c++
Loop c++Loop c++
Loop c++
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
Array in c++
Array in c++Array in c++
Array in c++
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
Structure in c
Structure in cStructure in c
Structure in c
 
Structure in c
Structure in cStructure in c
Structure in c
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 

Similar a 8.3 program structure (1 hour)

Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
rohassanie
 
Introduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic conceptsIntroduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic concepts
ssuserf86fba
 

Similar a 8.3 program structure (1 hour) (20)

Introduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itIntroduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to it
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 
Basics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptxBasics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptx
 
Prog1-L1.pdf
Prog1-L1.pdfProg1-L1.pdf
Prog1-L1.pdf
 
Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
 
Workshop1
Workshop1Workshop1
Workshop1
 
General structure of c++
General structure of c++General structure of c++
General structure of c++
 
C++ basics
C++ basicsC++ basics
C++ basics
 
Introduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic conceptsIntroduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic concepts
 
C++ Chapter 3
C++ Chapter 3C++ Chapter 3
C++ Chapter 3
 
Labsheet1stud
Labsheet1studLabsheet1stud
Labsheet1stud
 
C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptx
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
 
basic program
basic programbasic program
basic program
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

8.3 program structure (1 hour)

  • 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> 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.
  • 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 Product Editor Edit Source code Compiler Compile Object code Linker Link Executable image Run Result / Output

Notas del editor

  1. Others example: string.h , conio.h, dos.h, time.h, graphics.h
  2. cout semicolon. (also known as the statement terminator).
  3. semicolon. (also known as the statement terminator).
  4. semicolon. (also known as the statement terminator).
  5. semicolon. (also known as the statement terminator).
  6. semicolon. (also known as the statement terminator).
  7. 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
  8. Example of syntax error int x  missing semi colon. Cout&lt;&lt; x;  standard output stream object – cout – must use small letter Example of program-logic error. Use wrong formula to calculate area of rectangle.