chapter one introduction to programming
DESCRIPTION
Chapter One Introduction to Programming. Objectives. You should be able to describe: Introduction to Programming Function and Class Names The cout object Programming Style Common Programming Errors. History of C++. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/1.jpg)
1
Chapter One
Introduction to Programming
![Page 2: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/2.jpg)
2
Objectives
You should be able to describe:• Introduction to Programming• Function and Class Names• The cout object• Programming Style• Common Programming Errors
![Page 3: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/3.jpg)
3
History of C++
• C++ began as extension to C, which is procedural language developed in the 1970s at AT&T Bell Laboratories
• In early 1980s, Bjarne Stroustrup (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++
![Page 4: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/4.jpg)
4
Introduction to Programming
• Computer program: 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– High-level languages use human language type
instructions– Low-level languages use instructions tied to a
computer type
![Page 5: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/5.jpg)
5
Procedural Programming Languages
• 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
![Page 6: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/6.jpg)
6
Procedure-Oriented Programs
• Most high-level programs process data to produce one or more results
• Procedural programs are constructed from sets of instructions, with each set called a procedure
• Each procedure moves the data one step closer to the final desired output
![Page 7: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/7.jpg)
7
Procedure-Oriented Programs (continued)
![Page 8: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/8.jpg)
8
Object-Oriented Languages
• Allow for procedural instructions and for definitions of objects to be manipulated
• Such definitions include:– The general characteristics of objects
– Specific operations to manipulate objects
• C++ is an object-oriented language– Has procedures and objects
– Supports code reuse
![Page 9: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/9.jpg)
Programming
• Programming=represent information + process information
9
![Page 10: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/10.jpg)
Information
• Data classified as different types– Numeric data
– Logic data
– Characters and sentences
– …
10
![Page 11: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/11.jpg)
Information representation
• Factorize information– Some are constants, some are variables.
• Information decomposition• Information type identification
11
![Page 12: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/12.jpg)
Information processing
• Operations– simple statements using supported operators
– Compound statements by combining supported operations
–
12
![Page 13: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/13.jpg)
13
Algorithms and Procedures
• Before writing a program, a programmer must clearly understand– What data is to be used
– Desired result
– Procedure needed to produce this result
• The procedure is referred to as an algorithm• Algorithm: Step-by-step sequence of instructions
describing how to perform a computation
![Page 14: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/14.jpg)
14
Example of an Algorithm
• Assume that a program must calculate sum of all whole numbers from 1 through 100
• A computer can not respond to heuristic command: “Add the numbers from 1 - 100”
• A computer is algorithm-responding machine and not intuition-responding machine
• Several methods or algorithms can be used to find the required sum
![Page 15: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/15.jpg)
15
Example of an Algorithm (continued)
Sum = n(a + b)/2
Where
n = number of terms to be added (100)
a = first number added (1)
b = last number to be added (100)
Sum = 100(1 + 100)/2 = 5050
Figure 1.2: Summing the Numbers from 1 through 100 Method 3. Formula - Use the formula Sum = n(a + b)/2 = 5050
![Page 16: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/16.jpg)
16
Flowcharting
![Page 17: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/17.jpg)
17
Flowcharting
![Page 18: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/18.jpg)
18
Flowchart Example
![Page 19: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/19.jpg)
19
Classes and Objects
• Data Object: Set of values packaged as single unit
• Class: Set of objects with similar attributes• General concept of object-oriented programming
is difference between an object and the larger set of which it is a member (class)
• A red, Ford Taurus sedan is an instance, or object, of general class of automobiles
![Page 20: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/20.jpg)
20
Program Translation
• C++ source program: Set of instructions written in C++ language
• Machine language: Internal computer language– Consists of a series of 1s and 0s
• 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
![Page 21: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/21.jpg)
21
Program Translation (continued)
![Page 22: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/22.jpg)
22
Function and Class Names
• Modular programs: Segments arranged in logical order to form an integrated unit
• Module: Segments of modular program• Function: Name of a C++ procedure
– Composed of sequence of C++ instructions
– Function interface is its inputs and outputs
– Method of converting input to results is encapsulated and hidden within function
![Page 23: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/23.jpg)
23
Function and Class Names (continued)
![Page 24: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/24.jpg)
24
Function and Class Names (continued)
![Page 25: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/25.jpg)
25
Function and Class Naming Conventions
• Identifiers: Names that convey an idea of the purpose of function or class
• Identifier composition rules: (read page 15)– First character must be a letter or underscore
– Only letter, digit or underscore may follow
– Blank spaces NOT allowed
– Identify component words with initial capitalization
– Cannot be C++ keyword
– Should be a mnemonic
![Page 26: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/26.jpg)
• Tokens are basic building blocks of C/C++ programming.– In a C/C++ source program, the basic element
recognized by the compiler is the "token.“.
– A token is source-program text that the compiler does not further break down into component elements.
![Page 27: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/27.jpg)
tokens
– keyword
– identifier
– Constant number
– string-literal
– operator
– Punctuator (special symbol)• Punctuation characters such as brackets ([ ]),
braces ({ }), parentheses ( ( ) ), and commas (,) are also tokens.
![Page 28: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/28.jpg)
![Page 29: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/29.jpg)
Literals (constants)
• A literal is a notation for representing a value within source code.
• Literal constants (often referred to as literals or constants as apposed to symbolic constant identifiers) are invariants whose values are implied by their representations.
• A letter or symbol that stands for itself as opposed to a feature, function, or entity associated with it in a programming language: – + can be a symbol that refers to add operator, but as
a literal, it is a ‘+’ character, as it is.
29
![Page 30: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/30.jpg)
• Literal numbers: Literal strings: “Obama”, “Bush”, "hello”
• Literal characters 'b', 'c' (character)• Literal integers: 14• Literal doubles: 3.1 (default double)• 3.1l(double)• 3.1f (float)• 18.46e1 (scientific notation)• We call the f in 3.1f the suffix modifier. The type of a
literal is thus determined from its syntactic form.
![Page 31: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/31.jpg)
operators
• C/C++ operators can be used to manipulate Variables and constants using complex expressions.
• In C++, operators can be overloaded and their meanings can be user-defined. However, their precedence and the number of operands they take cannot be modified.
• At minimum we need to be aware of the syntax and semantics of operators as they are supplied with the language, not overloaded.
31
![Page 32: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/32.jpg)
Punctuators (special symbols)
• Punctuators in C++ have syntactic and semantic meaning to the compiler but do not, of themselves, specify an operation that yields a value.
• Some punctuators, either alone or in combination, can also be C++ operators or be significant to the preprocessor.
• Any of the following characters are considered punctuators:
• ! % ^ & * ( ) – + = { } | ~ [ ] \ ; ' : " < > ? , . / #
• The punctuators [ ], ( ), and { } must appear in pairs
32
![Page 33: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/33.jpg)
33
C++ Keywords
![Page 34: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/34.jpg)
Fundamental data types
34
When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way.
A data type or datatype defines kind of data, the size of memory occupied, a set of possible values, the range, as well as basic operations on those values.
The memory in our computers is organized in bytes. A byte is the basic unit, the minimum amount, that can be
manipulated in memory. A byte can store a relatively small amount of data: one single character or a small integer (generally an integer between 0 and 255). In addition, the computer can manipulate more complex data types that come from grouping several bytes, such as long numbers or non-integer numbers.
![Page 35: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/35.jpg)
Built-in data types as well as the range of values that can be represented
35
name description Size Range
char Character or small integer 1 byte Signed: -128 to 127Unsigned: 0 to 255
Short int Short integer 2 bytes Signed: -32768 to 32767Unsigned: 0 to 65535
int Integer 4 bytes Signed: -2147483648 to 2147483647Unsigned: 0 to 4294967295
bool Boolean value. It can take one of two values: true or false
1 byte (though only 1 bit is needed)
Ture or false (1 or 0)
float Floating point number 4 bytes +/- 3.4e+/-38 (~7 digits)
double Double precision floating point number
8 bytes +/-1.7e+/-308(~15 digits)
![Page 36: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/36.jpg)
36
C++ Identifiers
• Examples of valid identifiers:grosspay taxCalc
addNums degToRad
multByTwo salesTax
netPay bessel
Usually identifiers are used to name variables, constant symbols, functions, labels etc.
![Page 37: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/37.jpg)
37
C++ Identifiers (continued)
• Examples of invalid identifiers:4ab3 (begins with a number)
e*6 (contains a special character)
while (is a keyword)
![Page 38: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/38.jpg)
variables
• A variable in computer source code is a data storage space located somewhere in memory.
• It is identified by a name.• A variable’s name won’t change, so we can
program with respect to the name.• But the content in the space represented by the
name is changeable.– Generally the value change during the course of
program execution.
38
![Page 39: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/39.jpg)
variables
• A variable in C++ must be declared (the type of variable) and defined (values assigned to a variable) before it can be used in a program
39
![Page 40: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/40.jpg)
Expressions
• Expressions are sequences of operators and operands that are used for one or more of these purposes: – Computing a value from the operands.
– Designating objects or functions.
• types of expressions • semantics of expressions
40
![Page 41: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/41.jpg)
Soul of your program
• Input and output support allow your program interactive
• Variables make your program reusable without recompiling your program
• Expressions make your program intelligent.
• Variables +expressions = soul of your program
41
![Page 42: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/42.jpg)
Types of expressions
• C++ expressions are divided into several categories:
• Primary expressions. These are the building blocks from which all other expressions are formed.
• Postfix expressions. These are primary expressions followed by an operator — for example, the array subscript or postfix increment operator.
• Expressions formed with unary operators. Unary operators act on only one operand in an expression.
• Expressions formed with binary operators. Binary operators act on two operands in an expression.
• Expressions with the conditional operator. The conditional operator is a ternary operator — the only such operator in the C++ language — and takes three operands.
• Constant expressions. Constant expressions are formed entirely of constant data.
• Expressions with explicit type conversions. Explicit type conversions, or "casts," can be used in expressions.
• Expressions with pointer-to-member operators.
• Casting. Type-safe "casts" can be used in expressions.
• Run-Time Type Information. Determine the type of an object during program execution.
42
![Page 43: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/43.jpg)
Semantics of Expressions
• Order of evaluation• Sequence points• Ambiguous expressions• Notation in expressions• Side effects
43
![Page 44: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/44.jpg)
44
The main Function
• Each C+ program must have one and only one function named main
• Called a driver function because it drives the other modules
![Page 45: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/45.jpg)
45
The main Function (continued)
![Page 46: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/46.jpg)
46
The main Function (continued)
• 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 run time are referred to as arguments of function
![Page 47: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/47.jpg)
47
main Function Composition
![Page 48: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/48.jpg)
48
The cout Object
• 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!”;
![Page 49: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/49.jpg)
49
C++ Sample Code using cout
![Page 50: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/50.jpg)
50
Newline Escape Sequence
• Instructs the display device to move to a new line– A newline 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
![Page 51: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/51.jpg)
51
Preprocessor Command
• Performs an action before the compiler translates source code to machine code– An example is: #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
![Page 52: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/52.jpg)
52
Namespaces
• Files 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
![Page 53: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/53.jpg)
53
More C++ Sample Code
![Page 54: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/54.jpg)
54
More C++ Sample Code (continued)
![Page 55: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/55.jpg)
55
Syntax
• The set of rules for formulating grammatically correct C++ language statements– Compiler accepts statements with correct syntax
without generating error message
• A program statement can syntactically correct and logically incorrect– Compiler will accept statement
– Program will produce incorrect results
![Page 56: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/56.jpg)
56
Programming Style
• 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
![Page 57: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/57.jpg)
57
Standard C++ Program Form
• 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
![Page 58: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/58.jpg)
58
Standard C++ Program Form (continued)
• 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
![Page 59: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/59.jpg)
59
Poor Program Format
![Page 60: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/60.jpg)
60
Proper Program Format
![Page 61: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/61.jpg)
Statements
• C/C++ Program is made up of statements. • A statement consists of zero or more tokens, and always end with a ;. • Every statement in your program alone or in combination specifies an action to performed by your
program. • C/C++ provides variety of types of statements to help you attain any function with maximum flexibility and
efficiency. – One of the reason for popularity of C/C++ is because of the extreme power provided to programmer in C
due to it rich and diverse set of statements define in C/C++.
• declaration statement• Assignment statement• if - else Statement • switch Statement• For statment• while Statement • do Statement • return Statment • goto Statement • break Statement• Continue statement• Expressions Statement • Block Statement• …
![Page 62: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/62.jpg)
62
Comments
• 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
![Page 63: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/63.jpg)
63
Comment structure
• Line comment: Begins with 2 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 across three lines */
![Page 64: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/64.jpg)
64
Common Programming Errors
• 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
• Misspelling the name of an object or function– Example: Typing cot instead of cout
![Page 65: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/65.jpg)
65
Common Programming Errors (continued)
• Forgetting to close a string sent to cout with a double-quote symbol
• Omitting the semicolon at the end of each statement
• Forgetting \n to indicate a new line
![Page 66: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/66.jpg)
Sequential program flow
• Without flow control statements, the program you wrote executes sequentially, one statement after another.
66
![Page 67: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/67.jpg)
Case sensitive
• ‘A’ and ‘a’ are different.
67
![Page 68: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/68.jpg)
68
Summary
• 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;
}
![Page 69: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/69.jpg)
69
Summary (continued)
• 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, <<
![Page 70: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/70.jpg)
Simplest program and Data Output
Sen Zhang
![Page 71: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/71.jpg)
The simplest program looks like a single person company!
void main()
{
// this starts a comment line
// here is where you put your code…
}
One function, with a head and the empty body
![Page 72: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/72.jpg)
The simplest program looks like a single person company!
int main(){
// this starts a comment line// here is where you put your code…return 0;
}
One function, with a head and the empty body
![Page 73: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/73.jpg)
Why main needs to return a value ?
• DOS command to determine the return code is ERRORLEVEL, most people use the name errorlevel.
• Errorlevels are not a standard feature of every command. A certain errorlevel may mean anything the programmer wanted it to. Most programmers agree that an errorlevel 0 means the command executed succesfully, and an errorlevel 1 or higher usually spells trouble. But there are many exceptions to this general rule.
• int main() {return 11.9;
• }• • c:\sen\t1222\Debug>echo %errorlevel%
0• c:\sen\t1222\Debug>t1222• c:\sen\t1222\Debug>echo %errorlevel%
11•
![Page 74: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/74.jpg)
74
The cout Object
• The cout object represents the standard output display device, so anything you insert into cout will reflect on 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!”;
![Page 75: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/75.jpg)
75
C++ Sample Code using cout
![Page 76: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/76.jpg)
76
Preprocessor Command
• Performs an action before the compiler translates source code to machine code– An example is: #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
![Page 77: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/77.jpg)
77
Namespaces
• Files 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
![Page 78: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/78.jpg)
78
More C++ Sample Code
![Page 79: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/79.jpg)
79
More C++ Sample Code (continued)
![Page 80: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/80.jpg)
#include <iostream>using namespace std;
int main(){cout<<"hi!";cout<<endl;cout<<3+5;cout<<endl;cout<<"3+5";return 0;}
![Page 81: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/81.jpg)
81
Formatted Output
• Read book, page 53 to 66• Besides displaying correct information, a
program must present results attractively.• Field width manipulators: control format of
numbers displayed by cout– Manipulators included in the output stream
![Page 82: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/82.jpg)
Output manipulators
• Output manipulators are items used to manipulate how the output stream of characters is displayed.
![Page 83: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/83.jpg)
Manipulators for columnizng styles
• setw(n)• setfill(‘c’)
![Page 84: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/84.jpg)
• When a manipulator requiring an argument is used, the iomanip header file must be included as part of the program.
• This is accomplished by the preprocessor command #include <iomanip>.
![Page 85: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/85.jpg)
Setw(width)
• The setw(width) manipulator included in the stream of data passed to cout is used to set the displayed field width.
• The width in this manipulator should be replaced by a concrete integer number say 10. It will reserves a field with a width of 10 for any subsequent number in the stream.
![Page 86: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/86.jpg)
Setfill(symbol)
• Set the default fill character to x. (The default fill character is a space)
• It will use the symbol to fill out the space that has not been used by the data.
![Page 87: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/87.jpg)
Special manipulators for numbers
• fixed• setprecision(n)
![Page 88: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/88.jpg)
fixed
• Always show a decimal point and use a default of 6 digits after the decimal point. Fill with trailing zeros, if necessary.
![Page 89: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/89.jpg)
setprecision(number)
• Set the floating-point precision to the places of number. If the fixed manipulator is designated, number specifies the total number of displayed digits after the decimal point; otherwise, it specifies the total number of significant digits displayed (both integer portion and fractional portion)
![Page 90: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/90.jpg)
90
Formatted Output (continued)
![Page 91: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/91.jpg)
91
Formatted Output (continued)
![Page 92: Chapter One Introduction to Programming](https://reader036.vdocuments.mx/reader036/viewer/2022062408/5681306f550346895d964f3b/html5/thumbnails/92.jpg)
Links to several lists of basic C++ constructs
• A list of keywords• A list of operators, with associativity of each and
precedence among them.• A list of io flags and format manipulators• A c++ reference website• Literals• Tokens• identifiers
92