c++ chapter 02 diploma

Upload: shir-ley

Post on 03-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 C++ Chapter 02 Diploma

    1/79

    Copyright 2012 Pearson Education, Inc.

    Chapter 2:

    Introduction to

    C++

  • 8/12/2019 C++ Chapter 02 Diploma

    2/79

    Copyright 2012 Pearson Education, Inc.

    2.1The Part of a C++ Program

  • 8/12/2019 C++ Chapter 02 Diploma

    3/79

    Copyright 2012 Pearson Education, Inc.

    The Parts of a C++ Program

    // sample C++ program

    #include

    using namespace std;

    int main(){

    cout

  • 8/12/2019 C++ Chapter 02 Diploma

    4/79

    Copyright 2012 Pearson Education, Inc.

    Special Characters

    Character Name Meaning

    // Double slash Beginning of a comment

    # Pound sign Beginning of preprocessordirective

    < > Open/close brackets Enclose filename in #include

    ( ) Open/closeparentheses

    Used when naming afunction

    { } Open/close brace Encloses a group of

    statements" " Open/close

    quotation marksEncloses string ofcharacters

    ; Semicolon End of a programmingstatement

  • 8/12/2019 C++ Chapter 02 Diploma

    5/79

    Copyright 2012 Pearson Education, Inc.

    2.2The coutObject

  • 8/12/2019 C++ Chapter 02 Diploma

    6/79

    Copyright 2012 Pearson Education, Inc.

    The coutObject

    Displays output on the computer screen

    You use the stream insertion operator

  • 8/12/2019 C++ Chapter 02 Diploma

    7/79Copyright 2012 Pearson Education, Inc.

    The coutObject

    Can be used to send more than one itemto cout:

    cout

  • 8/12/2019 C++ Chapter 02 Diploma

    8/79Copyright 2012 Pearson Education, Inc.

    The coutObject

    This produces one line of output:

    cout

  • 8/12/2019 C++ Chapter 02 Diploma

    9/79Copyright 2012 Pearson Education, Inc.

    The endlManipulator

    You can use the endlmanipulator to start

    a new line of output. This will produce twolines of output:

    cout

  • 8/12/2019 C++ Chapter 02 Diploma

    10/79Copyright 2012 Pearson Education, Inc.

    The endlManipulator

    cout

  • 8/12/2019 C++ Chapter 02 Diploma

    11/79Copyright 2012 Pearson Education, Inc.

    The endlManipulator

    You do NOT put quotation marks aroundendl

    The last character in endlis a lowercase

    L, not the number 1.

    endl This is a lowercase L

  • 8/12/2019 C++ Chapter 02 Diploma

    12/79Copyright 2012 Pearson Education, Inc.

    The \n Escape Sequence

    You can also use the \nescape sequence

    to start a new line of output. This willproduce two lines of output:

    cout

  • 8/12/2019 C++ Chapter 02 Diploma

    13/79Copyright 2012 Pearson Education, Inc.

    The \n Escape Sequence

    cout

  • 8/12/2019 C++ Chapter 02 Diploma

    14/79Copyright 2012 Pearson Education, Inc.

    2.3The #includeDirective

  • 8/12/2019 C++ Chapter 02 Diploma

    15/79

    Copyright 2012 Pearson Education, Inc.

    The #includeDirective

    Inserts the contents of another file into theprogram

    This is a preprocessor directive, not part ofC++ language

    #includelines not seen by compiler

    Do not place a semicolon at end of#includeline

  • 8/12/2019 C++ Chapter 02 Diploma

    16/79

    Copyright 2012 Pearson Education, Inc.

    2.4Variables and Literals

  • 8/12/2019 C++ Chapter 02 Diploma

    17/79

    Copyright 2012 Pearson Education, Inc.

    Variables and Literals

    Variable: a storage location in memory

    Has a name and a type of data it can hold

    Must be defined before it can be used:

    int item;

  • 8/12/2019 C++ Chapter 02 Diploma

    18/79

    Copyright 2012 Pearson Education, Inc.

    Variable Definition in Program 2-7

    Variable Definition

  • 8/12/2019 C++ Chapter 02 Diploma

    19/79

    Copyright 2012 Pearson Education, Inc.

    Literals

    Literal: a value that is written into aprograms code.

    "hello, there"(string literal)

    12 (integer literal)

  • 8/12/2019 C++ Chapter 02 Diploma

    20/79

    Copyright 2012 Pearson Education, Inc.

    Integer Literal in Program 2-9

    20 is an integer literal

  • 8/12/2019 C++ Chapter 02 Diploma

    21/79

    Copyright 2012 Pearson Education, Inc.

    String Literals in Program 2-9

    These are string literals

  • 8/12/2019 C++ Chapter 02 Diploma

    22/79

    Copyright 2012 Pearson Education, Inc.

    2.5Identifiers

  • 8/12/2019 C++ Chapter 02 Diploma

    23/79

    Copyright 2012 Pearson Education, Inc.

    Identifiers

    An identifier is a programmer-definedname for some part of a program:variables, functions, etc.

  • 8/12/2019 C++ Chapter 02 Diploma

    24/79

    Copyright 2012 Pearson Education, Inc.

    C++ Key Words

    You cannot use any of the C++ key words as anidentifier. These words have reserved meaning.

  • 8/12/2019 C++ Chapter 02 Diploma

    25/79

    Copyright 2012 Pearson Education, Inc.

    Variable Names

    A variable name should represent thepurpose of the variable. For example:

    itemsOrdered

    The purpose of this variable is to hold the

    number of items ordered.

  • 8/12/2019 C++ Chapter 02 Diploma

    26/79

    Copyright 2012 Pearson Education, Inc.

    Identifier Rules

    The first character of an identifier must bean alphabetic character or and underscore( _ ),

    After the first character you may usealphabetic characters, numbers, orunderscore characters.

    Upper- and lowercase characters aredistinct

  • 8/12/2019 C++ Chapter 02 Diploma

    27/79

  • 8/12/2019 C++ Chapter 02 Diploma

    28/79

    Copyright 2012 Pearson Education, Inc.

    2.6Integer Data Types

  • 8/12/2019 C++ Chapter 02 Diploma

    29/79

    Copyright 2012 Pearson Education, Inc.

    Integer Data Types

    Integer variables can hold whole numbers suchas 12, 7, and -99.

  • 8/12/2019 C++ Chapter 02 Diploma

    30/79

    Copyright 2012 Pearson Education, Inc.

    Defining Variables

    Variables of the same type can be defined- On separate lines:

    int length;

    int width;

    unsigned int area;- On the same line:

    int length, width;

    unsigned int area;

    Variables of different types must be in different

    definitions

  • 8/12/2019 C++ Chapter 02 Diploma

    31/79

    Copyright 2012 Pearson Education, Inc.

    Integer Types in Program 2-10

    This program has three variables: checking,

    miles, and days

  • 8/12/2019 C++ Chapter 02 Diploma

    32/79

    Copyright 2012 Pearson Education, Inc.

    Integer Literals

    An integer literal is an integer value that istyped into a programs code. For example:

    itemsOrdered = 15;

    In this code, 15 is an integer literal.

  • 8/12/2019 C++ Chapter 02 Diploma

    33/79

    Copyright 2012 Pearson Education, Inc.

    Integer Literals in Program 2-10

    Integer Literals

  • 8/12/2019 C++ Chapter 02 Diploma

    34/79

    Copyright 2012 Pearson Education, Inc.

    Integer Literals

    Integer literals are stored in memory asints by default

    To store an integer constant in a long

    memory location, put L at the end of thenumber: 1234L

    Constants that begin with 0 (zero) are

    base 8: 075 Constants that begin with 0x are base

    16: 0x75A

  • 8/12/2019 C++ Chapter 02 Diploma

    35/79

    Copyright 2012 Pearson Education, Inc.

    2.7The charData Type

  • 8/12/2019 C++ Chapter 02 Diploma

    36/79

    Copyright 2012 Pearson Education, Inc.

    The charData Type

    Used to hold characters or very smallinteger values

    Usually 1 byte of memory

    Numeric value of character from thecharacter set is stored in memory:

    CODE:char letter;

    letter = 'C';

    MEMORY:letter

    67

  • 8/12/2019 C++ Chapter 02 Diploma

    37/79

    Copyright 2012 Pearson Education, Inc.

    Character Literals

    Character literals must be enclosed insingle quote marks. Example:

    'A'

  • 8/12/2019 C++ Chapter 02 Diploma

    38/79

    Copyright 2012 Pearson Education, Inc.

    Character Literals in Program 2-13

  • 8/12/2019 C++ Chapter 02 Diploma

    39/79

    Copyright 2012 Pearson Education, Inc.

    Character Strings

    A series of characters in consecutive memorylocations:

    "Hello"

    Stored with the null terminator,\0

    , at the end:

    Comprised of the characters between the " "

    H e l l o \0

  • 8/12/2019 C++ Chapter 02 Diploma

    40/79

    Copyright 2012 Pearson Education, Inc.

    2.8The C++ stringClass

  • 8/12/2019 C++ Chapter 02 Diploma

    41/79

    Copyright 2012 Pearson Education, Inc.

    The C++ stringClass

    Special data type supports working with strings #include

    Can define stringvariables in programs:

    string firstName, lastName; Can receive values with assignment operator:firstName = "George";

    lastName = "Washington";

    Can be displayed via coutcout

  • 8/12/2019 C++ Chapter 02 Diploma

    42/79

    Copyright 2012 Pearson Education, Inc.

    The stringclass in Program 2-15

  • 8/12/2019 C++ Chapter 02 Diploma

    43/79

    Copyright 2012 Pearson Education, Inc.

    2.9Floating-Point Data Types

  • 8/12/2019 C++ Chapter 02 Diploma

    44/79

  • 8/12/2019 C++ Chapter 02 Diploma

    45/79

    Copyright 2012 Pearson Education, Inc.

    Floating-Point Data Types

  • 8/12/2019 C++ Chapter 02 Diploma

    46/79

    Copyright 2012 Pearson Education, Inc.

    Floating-Point Literals

    Can be represented in Fixed point (decimal) notation:

    31.4159 0.0000625

    E notation:3.14159E1 6.25e-5

    Are doubleby default

    Can be forced to be float (3.14159f) orlong double (0.0000625L)

  • 8/12/2019 C++ Chapter 02 Diploma

    47/79

  • 8/12/2019 C++ Chapter 02 Diploma

    48/79

    Copyright 2012 Pearson Education, Inc.

    2.10The boolData Type

  • 8/12/2019 C++ Chapter 02 Diploma

    49/79

    Copyright 2012 Pearson Education, Inc.

    The boolData Type

    Represents values that are trueorfalse

    boolvariables are stored as smallintegers

    false is represented by 0, trueby 1:

    bool allDone = true;

    bool finished = false;

    allDone finished

    1 0

  • 8/12/2019 C++ Chapter 02 Diploma

    50/79

    Copyright 2012 Pearson Education, Inc.

    Boolean Variables in Program 2-17

  • 8/12/2019 C++ Chapter 02 Diploma

    51/79

    Copyright 2012 Pearson Education, Inc.

    2.11Determining the Size of a Data

    Type

    D t i i th Si f D t

  • 8/12/2019 C++ Chapter 02 Diploma

    52/79

    Copyright 2012 Pearson Education, Inc.

    Determining the Size of a DataType

    The sizeofoperator gives the size of anydata type or variable:double amount;cout

  • 8/12/2019 C++ Chapter 02 Diploma

    53/79

    Copyright 2012 Pearson Education, Inc.

    2.12Variable Assignments and

    Initialization

    V i bl A i t d

  • 8/12/2019 C++ Chapter 02 Diploma

    54/79

    Copyright 2012 Pearson Education, Inc.

    Variable Assignments andInitialization

    An assignment statement uses the =operator to store a value in a variable.

    item = 12;

    This statement assigns the value 12 to theitemvariable.

  • 8/12/2019 C++ Chapter 02 Diploma

    55/79

    Copyright 2012 Pearson Education, Inc.

    Assignment

    The variable receiving the value mustappear on the left side of the = operator.

    This will NOT work:

    // ERROR!12 = item;

  • 8/12/2019 C++ Chapter 02 Diploma

    56/79

    Copyright 2012 Pearson Education, Inc.

    Variable Initialization

    To initialize a variable means to assign it avalue when it is defined:

    int length = 12;

    Can initialize some or all variables:

    int length = 12, width = 5, area;

  • 8/12/2019 C++ Chapter 02 Diploma

    57/79

  • 8/12/2019 C++ Chapter 02 Diploma

    58/79

    Copyright 2012 Pearson Education, Inc.

    2.13Scope

  • 8/12/2019 C++ Chapter 02 Diploma

    59/79

    Copyright 2012 Pearson Education, Inc.

    Scope

    The scope of a variable: the part of theprogram in which the variable can beaccessed

    A variable cannot be used before it isdefined

    Variable Out of Scope in Program

  • 8/12/2019 C++ Chapter 02 Diploma

    60/79

    Copyright 2012 Pearson Education, Inc.

    Variable Out of Scope in Program2-20

  • 8/12/2019 C++ Chapter 02 Diploma

    61/79

    Copyright 2012 Pearson Education, Inc.

    2.14Arithmetic Operators

  • 8/12/2019 C++ Chapter 02 Diploma

    62/79

    Copyright 2012 Pearson Education, Inc.

    Arithmetic Operators

    Used for performing numeric calculations

    C++ has unary, binary, and ternaryoperators:

    unary (1 operand) -5

    binary (2 operands) 13 - 7

    ternary (3 operands) exp1 ? exp2 : exp3

  • 8/12/2019 C++ Chapter 02 Diploma

    63/79

    Arithmetic Operators in Program 2

  • 8/12/2019 C++ Chapter 02 Diploma

    64/79

    Copyright 2012 Pearson Education, Inc.

    Arithmetic Operators in Program 2-21

  • 8/12/2019 C++ Chapter 02 Diploma

    65/79

    Copyright 2012 Pearson Education, Inc.

    A Closer Look at the /Operator

    /(division) operator performs integerdivision if both operands are integerscout

  • 8/12/2019 C++ Chapter 02 Diploma

    66/79

    Copyright 2012 Pearson Education, Inc.

    A Closer Look at the %Operator

    %(modulus) operator computes theremainder resulting from integer division

    cout

  • 8/12/2019 C++ Chapter 02 Diploma

    67/79

    Copyright 2012 Pearson Education, Inc.

    2.15Comments

  • 8/12/2019 C++ Chapter 02 Diploma

    68/79

  • 8/12/2019 C++ Chapter 02 Diploma

    69/79

    Copyright 2012 Pearson Education, Inc.

    Single-Line Comments

    Begin with //through to the end of line:int length = 12; // length in

    inches

    int width = 15; // width in inchesint area; // calculated area

    // calculate rectangle areaarea = length * width;

  • 8/12/2019 C++ Chapter 02 Diploma

    70/79

    Copyright 2012 Pearson Education, Inc.

    Multi-Line Comments

    Begin with /*, end with */

    Can span multiple lines:/* this is a multi-line

    comment

    */

    Can begin and end on the same line:int area; /* calculated area */

  • 8/12/2019 C++ Chapter 02 Diploma

    71/79

  • 8/12/2019 C++ Chapter 02 Diploma

    72/79

    Copyright 2012 Pearson Education, Inc.

    Named Constants

    Named constant (constant variable):variable whose content cannot bechanged during program execution

    Used for representing constant values withdescriptive names:const double TAX_RATE = 0.0675;

    const int NUM_STATES = 50;

    Often named in uppercase letters

  • 8/12/2019 C++ Chapter 02 Diploma

    73/79

    Copyright 2012 Pearson Education, Inc.

    Named Constants in Program 2-28

  • 8/12/2019 C++ Chapter 02 Diploma

    74/79

    Copyright 2012 Pearson Education, Inc.

    2.17Programming Style

  • 8/12/2019 C++ Chapter 02 Diploma

    75/79

    Copyright 2012 Pearson Education, Inc.

    Programming Style

    The visual organization of the source code

    Includes the use of spaces, tabs, andblank lines

    Does not affect the syntax of the program

    Affects the readability of the source code

  • 8/12/2019 C++ Chapter 02 Diploma

    76/79

    Copyright 2012 Pearson Education, Inc.

    Programming Style

    Common elements to improve readability: Braces { }aligned vertically

    Indentation of statements within a set of

    braces Blank lines between declaration and other

    statements

    Long statements wrapped over multiplelines with aligned operators

  • 8/12/2019 C++ Chapter 02 Diploma

    77/79

    Copyright 2012 Pearson Education, Inc.

    2.18Standard and Prestandard C++

    S C

  • 8/12/2019 C++ Chapter 02 Diploma

    78/79

    Copyright 2012 Pearson Education, Inc.

    Standard and Prestandard C++

    Older-style C++ programs: Use .hat end of header files:

    #include

    Use #definepreprocessor directive insteadof const definitions

    Do not use using namespaceconvention

    May not compile with a standard C++compiler

  • 8/12/2019 C++ Chapter 02 Diploma

    79/79