SlideShare a Scribd company logo
A First Book of C++ 4th Edition 1
Du`a for StudyDu`a for Study
A First Book of C++A First Book of C++
Chapter 1Chapter 1
Getting StartedGetting Started
∗ In this chapter, you will learn about:
∗ Introduction to Programming
∗ Function and Class Names
∗ The cout Object
∗ Programming Style
∗ Common Programming Errors
A First Book of C++ 4th Edition 3
ObjectivesObjectives
∗ Computer program (software)
∗ Data and instructions used to operate a computer
∗ Programming
∗ Writing computer program in a language that the
computer can respond to and that other programmers can
understand
∗ Programming language
∗ Set of instructions, data, and rules used to construct a
program
A First Book of C++ 4th Edition 4
Introduction to ProgrammingIntroduction to Programming
∗ Categories of Programming Language
∗ Level
∗ High-level languages (e.g., C, C++, C#, Visual Basic, Java,
Python)
∗ Low-level languages (e.g., Assembly language)
A First Book of C++ 4th Edition 5
Introduction to ProgrammingIntroduction to Programming
(cont’d.)(cont’d.)
Assembly vs Machine LanguageAssembly vs Machine Language
A First Book of C++ 4th Edition 6
• Early computers were programmed in assembly language
• To calculate wages = rates * hours in assembly language:
1) Load 2) Multiply 3) Store
010001
010011
010010
∗ Categories of Programming Language (cont)
∗ Orientation
∗ Procedural language
∗ Instructions are used to create self-contained units
(procedures)
∗ Procedures accept data as input and transform data to
produce a specific result as an output
∗ Initially, high-level programming languages were
predominately procedural
A First Book of C++ 4th Edition 7
Introduction to ProgrammingIntroduction to Programming
(cont’d.)(cont’d.)
Introduction to ProgrammingIntroduction to Programming
(cont’d.)(cont’d.)
A First Book of C++ 4th Edition 8
a + b = ?
a = 2 , b =3 5
∗ Object-oriented languages
∗ Program must define objects that it is manipulating
∗ Such definitions include:
∗ The general characteristics of objects (attribute/state)
∗ Specific operations to manipulate objects (behavior/method)
∗ C++ is an object-oriented language
∗ Has procedures and objects
∗ Supports code reuse
A First Book of C++ 4th Edition 9
Introduction to ProgrammingIntroduction to Programming
(cont’d.)(cont’d.)
∗ C++ began as extension to C
∗ C is a procedural language developed in the 1970s at
AT&T Bell Laboratories
∗ In early 1980s, Bjarne Stroustrup (“Bee-yarn-eh Strow-
strup”, also at AT&T) used his background in
simulation languages to develop C++
∗ Object-orientation and other procedural
improvements were combined with existing C
language features to form C++
A First Book of C++ 4th Edition 10
Introduction to ProgrammingIntroduction to Programming
(cont’d.)(cont’d.)
∗ Before writing a program, a programmer must clearly
understand:
∗ What data is to be used
∗ The expected result
∗ The procedure needed to produce this result
∗ The procedure is referred to as an algorithm
∗ Step-by-step sequence of instructions describing how to
perform a computation
A First Book of C++ 4th Edition 11
Algorithms and ProceduresAlgorithms and Procedures
∗ Computers think algorithmically (step by step, use rules)
NOT heuristically (based on experience, no rules)!
∗ Assume that a program must calculate the sum of all
whole numbers from 1 through 100 (1+2+3+4+5+…+100)
∗ A computer:
∗ Cannot respond to heuristic command: “Add the numbers
from 1 - 100”
∗ Is an algorithm-responding machine and not a heuristic-
responding machine
∗ Several methods or algorithms can be used to find the
required sum
A First Book of C++ 4th Edition 12
Algorithms and Procedures (cont’d.)
A First Book of C++ 4th Edition 13
Algorithms and Procedures (cont’d.)
A First Book of C++ 4th Edition 14
Calculate the result for 3 x 2
A First Book of C++ 4th Edition 15
Flow Chart
A First Book of C++ 4th Edition 16
∗ C++ source program
∗ Set of instructions written in C++ language
∗ Machine language
∗ Internal computer language
∗ Consists of a series of 1s and 0s (binaries)
∗ Source program cannot be executed until it is
translated into machine language
∗ Interpreted language translates one statement at a time
∗ Compiled language translates all statements together
A First Book of C++ 4th Edition 17
Program Translation
Program Translation (cont'd.)
A First Book of C++ 4th Edition 18
100100 010010int x = 8
Processing a Program
A First Book of C++ 4th Edition 19
∗ Modular programs
∗ Segments arranged in logical order to form an integrated unit
∗ Module
∗ Segments (small parts) of modular program
∗ Function: Name of a C++ procedure
∗ Composed of sequence of C++ instructions
∗ Function interface is its inputs and results (output)
∗ Method of converting input to results is encapsulated and
hidden within function
A First Book of C++ 4th Edition 20
Function and Class Names
A First Book of C++ 4th Edition 21
Function and Class Names (cont'd.)
Function and Class Names (cont'd.)
A First Book of C++ 4th Edition 22
Accepts 2
numbers as
input,
produce a
result
3 5
15
Input
∗ Identifiers (or variables)
∗ Names that convey an idea of the purpose of function or class
∗ Identifier composition rules
∗ First character must be a letter or underscore (A1, _1)
∗ Only letter, digit, or underscore may follow (AA, A2, A_)
∗ Blank spaces aren’t allowed (A B)
∗ Identify component words with initial capitalization (numPos)
∗ Cannot be C++ keyword (e.g., auto, break, int, switch …)
∗ Should be a mnemonic (short, easy to remember, meaningful)
A First Book of C++ 4th Edition 23
Function and Class Names (cont'd.)
Function and Class Names (cont'd.)
A First Book of C++ 4th Edition 24
∗ Examples of valid identifiers:
grosspay $cost (valid in C++)
addNums degToRad
multByTwo salesTax
netPay bessel
_name my$
**Note: When we combine two or more words to form an
identifier (or variables), it is a good practice to capitalize each of
the new word.
A First Book of C++ 4th Edition 25
Function and Class Names (cont'd.)
∗ Examples of invalid identifiers:
4ab3(begins with a number)
e*6 (contains a special character)
while (is a keyword)
#amount (begins with special character)
A First Book of C++ 4th Edition 26
Function and Class Names (cont'd.)
∗ Each C++ program must have one and only one key
function named main()
∗ Called a driver function because it drives the other
modules
A First Book of C++ 4th Edition 27
The main() Function
The main() Function (cont'd.)
A First Book of C++ 4th Edition 28
∗ First line of function is called header line
∗ What type of data, if any, is returned from function
∗ The name of function
∗ What type of data, if any, is sent into function
∗ Data transmitted into function at runtime are referred
to as arguments of function
arguments (input to function)
Example:
header line void sum(int a, int b) {
}A First Book of C++ 4th Edition 29
The main() Function (cont'd.)
Starting to Program with Dev C++
A First Book of C++ 4th Edition 30
Starting to Program with Dev C++
A First Book of C++ 4th Edition 31
The main() Function (cont'd.)
A First Book of C++ 4th Edition 32
beginning of program
end of program
∗ The cout object sends data to the standard output
display device
∗ The display device is usually a video screen
∗ Name derived from console output and pronounced
“see out”
∗ Data is passed to cout by the insertion symbol
∗ cout << “Hello there, World!”;
A First Book of C++ 4th Edition 33
The cout Object
The cout Object (cont’d.)
A First Book of C++ 4th Edition 34
//Pre-processor command <header file>
//File for pre-written classes/classes (a containet //that
provides contexts/scope for variables)
system(“pause”);
The cout Object (cont’d.)
A First Book of C++ 4th Edition 35
//Pre-processor command <header file>
system(“pause”);
std::cout << “Hello there world!”;
name for namespace
∗ Preprocessor command
∗ Performs an action before the compiler translates source
code to machine code
∗ Example: #include <iostream>
∗ Causes the iostream file to be inserted wherever the
#include command appears
∗ iostream is part of the C++ standard library
∗ Included in iostream are two important classes:
∗ istream: Declarations and methods for data input
∗ ostream: Declarations and methods for data output
A First Book of C++ 4th Edition 36
The cout Object (cont’d.)
∗ Namespace
∗ File accessed by compiler when looking for prewritten
classes or functions
∗ Sample namespace statement:
∗ using namespace std;
∗ iostream contained in a namespace called std
∗ Compiler uses iostream’s cout object from std
whenever cout is referenced
A First Book of C++ 4th Edition 37
The cout Object (cont’d.)
The cout Object (cont’d.)
A First Book of C++ 4th Edition 38
∗ Newline escape sequence (“n”)
∗ Instructs the display device to move to a new line
∗ Caused when the characters backslash  and n are used
together
∗ Backslash provides an “escape” from the normal
interpretation of the character that follows
∗ Newline escape sequences can be placed anywhere
within a message to cout
A First Book of C++ 4th Edition 39
The cout Object (cont’d.)
The cout Object (cont’d.)
A First Book of C++ 4th Edition 40
∗ Every C++ program must contain one and only one
main() function
∗ Statements included within braces { }
∗ C++ allows flexibility in format for the word main, the
parentheses ( ), and braces { }
∗ More than one statement can be put on line
∗ One statement can be written across lines
∗ Use formatting for clarity and ease of program
reading
A First Book of C++ 4th Edition 41
Programming StyleProgramming Style
∗ Function name starts in column 1
∗ Name and parentheses on their own line
∗ Opening brace of function body on next line
∗ Aligned with first letter of function name
∗ Closing brace is last line of function
∗ Aligned with opening brace
∗ Standard form highlights the function as a unit
A First Book of C++ 4th Edition 42
Programming Style (cont’d.)Programming Style (cont’d.)
∗ Within function, indent statements 2-3 spaces
∗ Creates uniform look for similar statement groups
∗ Good programming practice
∗ Final program form should be consistent
∗ Proper format improves program readability and
understandability
A First Book of C++ 4th Edition 43
Programming Style (cont’d.)Programming Style (cont’d.)
∗ Explanatory remarks written within program (//)
∗ Clarify purpose of the program
∗ Describe objective of a group of statements
∗ Explain function of a single line of code
∗ Computer ignores all comments
∗ Comments exist only for convenience of reader
∗ A well-constructed program should be readable and
understandable
∗ Comments help explain unclear components
A First Book of C++ 4th Edition 44
CommentsComments
∗ Line comment
∗ Begins with two slashes(//) and continues to the end of the line
∗ Can be written on line by itself or at the end of line that contains
program code
// this is a line comment
∗ Block comment
∗ Multiple-line comment begins with the symbols /* and ends with
the symbols */
/* This is a block comment that
spans
three lines */
A First Book of C++ 4th Edition 45
Comments (cont’d.)Comments (cont’d.)
∗ Omitting parentheses after main()
∗ Omitting or incorrectly typing the opening brace {
∗ Opening brace signifies start of function body
∗ Omitting or incorrectly typing the closing brace }
∗ Closing brace signifies end of function
∗ Omitting the semicolon at the end of each statement ;
∗ Adding a semicolon after the #include
<iostream> preprocessor command
#include <iostream> ; X (wrong, no need)
A First Book of C++ 4th Edition 46
Common Programming ErrorsCommon Programming Errors
∗ Misspelling the name of an object or function
∗ Example: Typing “cot” instead of “cout”
∗ Forgetting to close a string sent to cout with a
double-quote symbol
cout << “Hello world
∗ Forgetting n to indicate a new line
A First Book of C++ 4th Edition 47
Common Programming ErrorsCommon Programming Errors
(cont'd.)(cont'd.)
∗ A C++ program consists of one or more modules
∗ One module must be the function main()
∗ main() is starting point of C++ program
∗ The simplest C++ program has the form:
#include <iostream>
using namespaces std;
int main()
{
program statements;
return 0;
}
A First Book of C++ 4th Edition 48
SummarySummary
∗ C++ statements are terminated by a semicolon ;
∗ Standard library contains many functions and classes
∗ Standard Library provided with C++ compiler
∗ Includes <iostream> for input and output
∗ cout object displays text or numeric results
∗ Stream of characters is sent to cout by:
∗ Enclosing characters in double quotes “ ”
∗ Using the insertion (“put to”) operator, <<
A First Book of C++ 4th Edition 49
Summary (cont'd.)Summary (cont'd.)

More Related Content

PDF
L6
PPT
2. data, operators, io
PDF
Hands-on Introduction to the C Programming Language
PPTX
Introduction Of C++
PPT
2. data, operators, io
PPTX
C language
PDF
Solid C++ by Example
PPT
C language introduction
L6
2. data, operators, io
Hands-on Introduction to the C Programming Language
Introduction Of C++
2. data, operators, io
C language
Solid C++ by Example
C language introduction

What's hot (20)

PPTX
C vs c++
PPT
C and C++ Industrial Training Jalandhar
PPTX
C and C ++ Training in Ambala ! BATRA COMPUTER CENTRE
PPT
Introduction to c programming
PDF
Reduce course notes class xii
PDF
Differences between c and c++
PDF
C programming language
PPTX
C vs c++
PDF
C++ Concepts and Ranges - How to use them?
PDF
Advanced C Language for Engineering
PPTX
C Programming Language Tutorial for beginners - JavaTpoint
PPT
C language
PPT
Lecture#5 c lang new
PPTX
introduction of c langauge(I unit)
PPTX
Introduction to c programming language
PPT
Glimpses of C++0x
PPTX
Discussing Fundamentals of C
C vs c++
C and C++ Industrial Training Jalandhar
C and C ++ Training in Ambala ! BATRA COMPUTER CENTRE
Introduction to c programming
Reduce course notes class xii
Differences between c and c++
C programming language
C vs c++
C++ Concepts and Ranges - How to use them?
Advanced C Language for Engineering
C Programming Language Tutorial for beginners - JavaTpoint
C language
Lecture#5 c lang new
introduction of c langauge(I unit)
Introduction to c programming language
Glimpses of C++0x
Discussing Fundamentals of C
Ad

Similar to Csc1100 lecture01 ch01-pt1 (20)

PPT
Csc1100 lecture03 ch03-pt2-s14
PPT
Csc1100 lecture03 ch03-pt2-s14
PDF
The C++ Programming Language
DOCX
C language tutorial
DOCX
Report on c and c++
PPTX
Introduction to C Unit 1
PPS
C programming session 01
PPTX
Lab 1.pptx
PPT
Fp201 unit2 1
PPTX
Introduction to cpp language and all the required information relating to it
PPTX
C++ LectuNSVAHDVQwyfkyuQWVHGWQUDKFEre-14.pptx
PDF
c programming L-1.pdf43333333544444444444444444444
PPTX
Lecture 1
PPT
PPT
Csdfsadf
PPTX
C Programming ppt for beginners . Introduction
PPTX
c_pro_introduction.pptx
PPTX
Lecture 01 Programming C for Beginners 001
PDF
Learn c language Important topics ( Easy & Logical, & smart way of learning)
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
The C++ Programming Language
C language tutorial
Report on c and c++
Introduction to C Unit 1
C programming session 01
Lab 1.pptx
Fp201 unit2 1
Introduction to cpp language and all the required information relating to it
C++ LectuNSVAHDVQwyfkyuQWVHGWQUDKFEre-14.pptx
c programming L-1.pdf43333333544444444444444444444
Lecture 1
Csdfsadf
C Programming ppt for beginners . Introduction
c_pro_introduction.pptx
Lecture 01 Programming C for Beginners 001
Learn c language Important topics ( Easy & Logical, & smart way of learning)
Ad

More from IIUM (20)

PDF
How to use_000webhost
PDF
Chapter 2
PDF
Chapter 1
PDF
Kreydle internship-multimedia
PDF
03phpbldgblock
PDF
Chap2 practice key
PDF
Group p1
PDF
Tutorial import n auto pilot blogspot friendly seo
PDF
Visual sceneperception encycloperception-sage-oliva2009
PDF
03 the htm_lforms
PDF
Exercise on algo analysis answer
PDF
Redo midterm
PDF
Heaps
PDF
Report format
PDF
Edpuzzle guidelines
PDF
Final Exam Paper
PDF
Final Exam Paper
PDF
Group assignment 1 s21516
PDF
Avl tree-rotations
PDF
Week12 graph
How to use_000webhost
Chapter 2
Chapter 1
Kreydle internship-multimedia
03phpbldgblock
Chap2 practice key
Group p1
Tutorial import n auto pilot blogspot friendly seo
Visual sceneperception encycloperception-sage-oliva2009
03 the htm_lforms
Exercise on algo analysis answer
Redo midterm
Heaps
Report format
Edpuzzle guidelines
Final Exam Paper
Final Exam Paper
Group assignment 1 s21516
Avl tree-rotations
Week12 graph

Csc1100 lecture01 ch01-pt1

  • 1. A First Book of C++ 4th Edition 1 Du`a for StudyDu`a for Study
  • 2. A First Book of C++A First Book of C++ Chapter 1Chapter 1 Getting StartedGetting Started
  • 3. ∗ In this chapter, you will learn about: ∗ Introduction to Programming ∗ Function and Class Names ∗ The cout Object ∗ Programming Style ∗ Common Programming Errors A First Book of C++ 4th Edition 3 ObjectivesObjectives
  • 4. ∗ Computer program (software) ∗ Data and instructions used to operate a computer ∗ Programming ∗ Writing computer program in a language that the computer can respond to and that other programmers can understand ∗ Programming language ∗ Set of instructions, data, and rules used to construct a program A First Book of C++ 4th Edition 4 Introduction to ProgrammingIntroduction to Programming
  • 5. ∗ Categories of Programming Language ∗ Level ∗ High-level languages (e.g., C, C++, C#, Visual Basic, Java, Python) ∗ Low-level languages (e.g., Assembly language) A First Book of C++ 4th Edition 5 Introduction to ProgrammingIntroduction to Programming (cont’d.)(cont’d.)
  • 6. Assembly vs Machine LanguageAssembly vs Machine Language A First Book of C++ 4th Edition 6 • Early computers were programmed in assembly language • To calculate wages = rates * hours in assembly language: 1) Load 2) Multiply 3) Store 010001 010011 010010
  • 7. ∗ Categories of Programming Language (cont) ∗ Orientation ∗ Procedural language ∗ Instructions are used to create self-contained units (procedures) ∗ Procedures accept data as input and transform data to produce a specific result as an output ∗ Initially, high-level programming languages were predominately procedural A First Book of C++ 4th Edition 7 Introduction to ProgrammingIntroduction to Programming (cont’d.)(cont’d.)
  • 8. Introduction to ProgrammingIntroduction to Programming (cont’d.)(cont’d.) A First Book of C++ 4th Edition 8 a + b = ? a = 2 , b =3 5
  • 9. ∗ Object-oriented languages ∗ Program must define objects that it is manipulating ∗ Such definitions include: ∗ The general characteristics of objects (attribute/state) ∗ Specific operations to manipulate objects (behavior/method) ∗ C++ is an object-oriented language ∗ Has procedures and objects ∗ Supports code reuse A First Book of C++ 4th Edition 9 Introduction to ProgrammingIntroduction to Programming (cont’d.)(cont’d.)
  • 10. ∗ C++ began as extension to C ∗ C is a procedural language developed in the 1970s at AT&T Bell Laboratories ∗ In early 1980s, Bjarne Stroustrup (“Bee-yarn-eh Strow- strup”, also at AT&T) used his background in simulation languages to develop C++ ∗ Object-orientation and other procedural improvements were combined with existing C language features to form C++ A First Book of C++ 4th Edition 10 Introduction to ProgrammingIntroduction to Programming (cont’d.)(cont’d.)
  • 11. ∗ Before writing a program, a programmer must clearly understand: ∗ What data is to be used ∗ The expected result ∗ The procedure needed to produce this result ∗ The procedure is referred to as an algorithm ∗ Step-by-step sequence of instructions describing how to perform a computation A First Book of C++ 4th Edition 11 Algorithms and ProceduresAlgorithms and Procedures
  • 12. ∗ Computers think algorithmically (step by step, use rules) NOT heuristically (based on experience, no rules)! ∗ Assume that a program must calculate the sum of all whole numbers from 1 through 100 (1+2+3+4+5+…+100) ∗ A computer: ∗ Cannot respond to heuristic command: “Add the numbers from 1 - 100” ∗ Is an algorithm-responding machine and not a heuristic- responding machine ∗ Several methods or algorithms can be used to find the required sum A First Book of C++ 4th Edition 12 Algorithms and Procedures (cont’d.)
  • 13. A First Book of C++ 4th Edition 13
  • 14. Algorithms and Procedures (cont’d.) A First Book of C++ 4th Edition 14 Calculate the result for 3 x 2
  • 15. A First Book of C++ 4th Edition 15 Flow Chart
  • 16. A First Book of C++ 4th Edition 16
  • 17. ∗ C++ source program ∗ Set of instructions written in C++ language ∗ Machine language ∗ Internal computer language ∗ Consists of a series of 1s and 0s (binaries) ∗ Source program cannot be executed until it is translated into machine language ∗ Interpreted language translates one statement at a time ∗ Compiled language translates all statements together A First Book of C++ 4th Edition 17 Program Translation
  • 18. Program Translation (cont'd.) A First Book of C++ 4th Edition 18 100100 010010int x = 8
  • 19. Processing a Program A First Book of C++ 4th Edition 19
  • 20. ∗ Modular programs ∗ Segments arranged in logical order to form an integrated unit ∗ Module ∗ Segments (small parts) of modular program ∗ Function: Name of a C++ procedure ∗ Composed of sequence of C++ instructions ∗ Function interface is its inputs and results (output) ∗ Method of converting input to results is encapsulated and hidden within function A First Book of C++ 4th Edition 20 Function and Class Names
  • 21. A First Book of C++ 4th Edition 21 Function and Class Names (cont'd.)
  • 22. Function and Class Names (cont'd.) A First Book of C++ 4th Edition 22 Accepts 2 numbers as input, produce a result 3 5 15 Input
  • 23. ∗ Identifiers (or variables) ∗ Names that convey an idea of the purpose of function or class ∗ Identifier composition rules ∗ First character must be a letter or underscore (A1, _1) ∗ Only letter, digit, or underscore may follow (AA, A2, A_) ∗ Blank spaces aren’t allowed (A B) ∗ Identify component words with initial capitalization (numPos) ∗ Cannot be C++ keyword (e.g., auto, break, int, switch …) ∗ Should be a mnemonic (short, easy to remember, meaningful) A First Book of C++ 4th Edition 23 Function and Class Names (cont'd.)
  • 24. Function and Class Names (cont'd.) A First Book of C++ 4th Edition 24
  • 25. ∗ Examples of valid identifiers: grosspay $cost (valid in C++) addNums degToRad multByTwo salesTax netPay bessel _name my$ **Note: When we combine two or more words to form an identifier (or variables), it is a good practice to capitalize each of the new word. A First Book of C++ 4th Edition 25 Function and Class Names (cont'd.)
  • 26. ∗ Examples of invalid identifiers: 4ab3(begins with a number) e*6 (contains a special character) while (is a keyword) #amount (begins with special character) A First Book of C++ 4th Edition 26 Function and Class Names (cont'd.)
  • 27. ∗ Each C++ program must have one and only one key function named main() ∗ Called a driver function because it drives the other modules A First Book of C++ 4th Edition 27 The main() Function
  • 28. The main() Function (cont'd.) A First Book of C++ 4th Edition 28
  • 29. ∗ First line of function is called header line ∗ What type of data, if any, is returned from function ∗ The name of function ∗ What type of data, if any, is sent into function ∗ Data transmitted into function at runtime are referred to as arguments of function arguments (input to function) Example: header line void sum(int a, int b) { }A First Book of C++ 4th Edition 29 The main() Function (cont'd.)
  • 30. Starting to Program with Dev C++ A First Book of C++ 4th Edition 30
  • 31. Starting to Program with Dev C++ A First Book of C++ 4th Edition 31
  • 32. The main() Function (cont'd.) A First Book of C++ 4th Edition 32 beginning of program end of program
  • 33. ∗ The cout object sends data to the standard output display device ∗ The display device is usually a video screen ∗ Name derived from console output and pronounced “see out” ∗ Data is passed to cout by the insertion symbol ∗ cout << “Hello there, World!”; A First Book of C++ 4th Edition 33 The cout Object
  • 34. The cout Object (cont’d.) A First Book of C++ 4th Edition 34 //Pre-processor command <header file> //File for pre-written classes/classes (a containet //that provides contexts/scope for variables) system(“pause”);
  • 35. The cout Object (cont’d.) A First Book of C++ 4th Edition 35 //Pre-processor command <header file> system(“pause”); std::cout << “Hello there world!”; name for namespace
  • 36. ∗ Preprocessor command ∗ Performs an action before the compiler translates source code to machine code ∗ Example: #include <iostream> ∗ Causes the iostream file to be inserted wherever the #include command appears ∗ iostream is part of the C++ standard library ∗ Included in iostream are two important classes: ∗ istream: Declarations and methods for data input ∗ ostream: Declarations and methods for data output A First Book of C++ 4th Edition 36 The cout Object (cont’d.)
  • 37. ∗ Namespace ∗ File accessed by compiler when looking for prewritten classes or functions ∗ Sample namespace statement: ∗ using namespace std; ∗ iostream contained in a namespace called std ∗ Compiler uses iostream’s cout object from std whenever cout is referenced A First Book of C++ 4th Edition 37 The cout Object (cont’d.)
  • 38. The cout Object (cont’d.) A First Book of C++ 4th Edition 38
  • 39. ∗ Newline escape sequence (“n”) ∗ Instructs the display device to move to a new line ∗ Caused when the characters backslash and n are used together ∗ Backslash provides an “escape” from the normal interpretation of the character that follows ∗ Newline escape sequences can be placed anywhere within a message to cout A First Book of C++ 4th Edition 39 The cout Object (cont’d.)
  • 40. The cout Object (cont’d.) A First Book of C++ 4th Edition 40
  • 41. ∗ Every C++ program must contain one and only one main() function ∗ Statements included within braces { } ∗ C++ allows flexibility in format for the word main, the parentheses ( ), and braces { } ∗ More than one statement can be put on line ∗ One statement can be written across lines ∗ Use formatting for clarity and ease of program reading A First Book of C++ 4th Edition 41 Programming StyleProgramming Style
  • 42. ∗ Function name starts in column 1 ∗ Name and parentheses on their own line ∗ Opening brace of function body on next line ∗ Aligned with first letter of function name ∗ Closing brace is last line of function ∗ Aligned with opening brace ∗ Standard form highlights the function as a unit A First Book of C++ 4th Edition 42 Programming Style (cont’d.)Programming Style (cont’d.)
  • 43. ∗ Within function, indent statements 2-3 spaces ∗ Creates uniform look for similar statement groups ∗ Good programming practice ∗ Final program form should be consistent ∗ Proper format improves program readability and understandability A First Book of C++ 4th Edition 43 Programming Style (cont’d.)Programming Style (cont’d.)
  • 44. ∗ Explanatory remarks written within program (//) ∗ Clarify purpose of the program ∗ Describe objective of a group of statements ∗ Explain function of a single line of code ∗ Computer ignores all comments ∗ Comments exist only for convenience of reader ∗ A well-constructed program should be readable and understandable ∗ Comments help explain unclear components A First Book of C++ 4th Edition 44 CommentsComments
  • 45. ∗ Line comment ∗ Begins with two slashes(//) and continues to the end of the line ∗ Can be written on line by itself or at the end of line that contains program code // this is a line comment ∗ Block comment ∗ Multiple-line comment begins with the symbols /* and ends with the symbols */ /* This is a block comment that spans three lines */ A First Book of C++ 4th Edition 45 Comments (cont’d.)Comments (cont’d.)
  • 46. ∗ Omitting parentheses after main() ∗ Omitting or incorrectly typing the opening brace { ∗ Opening brace signifies start of function body ∗ Omitting or incorrectly typing the closing brace } ∗ Closing brace signifies end of function ∗ Omitting the semicolon at the end of each statement ; ∗ Adding a semicolon after the #include <iostream> preprocessor command #include <iostream> ; X (wrong, no need) A First Book of C++ 4th Edition 46 Common Programming ErrorsCommon Programming Errors
  • 47. ∗ Misspelling the name of an object or function ∗ Example: Typing “cot” instead of “cout” ∗ Forgetting to close a string sent to cout with a double-quote symbol cout << “Hello world ∗ Forgetting n to indicate a new line A First Book of C++ 4th Edition 47 Common Programming ErrorsCommon Programming Errors (cont'd.)(cont'd.)
  • 48. ∗ A C++ program consists of one or more modules ∗ One module must be the function main() ∗ main() is starting point of C++ program ∗ The simplest C++ program has the form: #include <iostream> using namespaces std; int main() { program statements; return 0; } A First Book of C++ 4th Edition 48 SummarySummary
  • 49. ∗ C++ statements are terminated by a semicolon ; ∗ Standard library contains many functions and classes ∗ Standard Library provided with C++ compiler ∗ Includes <iostream> for input and output ∗ cout object displays text or numeric results ∗ Stream of characters is sent to cout by: ∗ Enclosing characters in double quotes “ ” ∗ Using the insertion (“put to”) operator, << A First Book of C++ 4th Edition 49 Summary (cont'd.)Summary (cont'd.)