02-Overview of C++ (Part 1)

download 02-Overview of C++ (Part 1)

of 39

Transcript of 02-Overview of C++ (Part 1)

  • 7/24/2019 02-Overview of C++ (Part 1)

    1/39

    Data Structures

    Overview of C++ (part 1)

  • 7/24/2019 02-Overview of C++ (Part 1)

    2/39

    CT077-3-2-DSTR Data Structures 2

    Learning Objective

    At the end of the lecture, you should be able to know:

    the basic components of a C++ program

    how to write a simple C++ program and use

    preprocessor directives how to use input\output in a program

    the selection control structures if, if...else, and

    switch in a program the repetition (looping) control structures

    how to write and use a simple function

  • 7/24/2019 02-Overview of C++ (Part 1)

    3/39

    CT077-3-2-DSTR Data Structures 3

    Creating a C++ Program

    A C++ program has two parts:

    Preprocessor directives

    The program statements (functions, classes, and

    types definitions)

    Preprocessor directives and program statements

    constitute C++ source code (in .cpp and .h files)

    Compiler generates object code (.obj) and a process

    called Linking generates executable file (.exe on

    windows)

  • 7/24/2019 02-Overview of C++ (Part 1)

    4/39

    CT077-3-2-DSTR Data Structures 4

    Creating a C++ Program

    A simple C++ program can be a collection of

    functions, one of which is the function main

    The first line of the function main is called theheading of the function:

    void main()

    The statements enclosed between the curly

    braces ({and }) form the body of the function

    int main () {

    //mandatory by some compilers

    return 0; //or other int value

    }

  • 7/24/2019 02-Overview of C++ (Part 1)

    5/39

  • 7/24/2019 02-Overview of C++ (Part 1)

    6/39

    CT077-3-2-DSTR Data Structures 6

    Creating a C++ Program

    Syntax to include a header file:

    For example:

    #include

    Causes the preprocessor to include the

    header file iostream in the program

  • 7/24/2019 02-Overview of C++ (Part 1)

    7/39CT077-3-2-DSTR Data Structures 7

    Creating a C++ Program

    cinand coutare declared in the header file

    iostream, but within stdnamespace

    To use cinand coutin a program, use the

    following two statements:

    #include

    using namespacestd;

    Without using std namespace, you must use scope

    operator :: to write fully qualified names (std::cinand

    std::cout)

  • 7/24/2019 02-Overview of C++ (Part 1)

    8/39CT077-3-2-DSTR Data Structures 8

    Creating a C++ Program

    To use the string type, you need to access its

    definition from the header file string

    Include the following preprocessor directive:

    #include

  • 7/24/2019 02-Overview of C++ (Part 1)

    9/39CT077-3-2-DSTR Data Structures 9

    Creating a C++ Program

  • 7/24/2019 02-Overview of C++ (Part 1)

    10/39CT077-3-2-DSTR Data Structures 10

    Creating a C++ Program

    Sample Run:

    Line 9: firstNum = 18

    Line 10: Enter an integer: 15

    Line 13: secondNum = 15

    Line 15: The new value of firstNum = 60

  • 7/24/2019 02-Overview of C++ (Part 1)

    11/39CT077-3-2-DSTR Data Structures 11

    Programming Example

    Write a program that takes a given length as input

    expressed in feet and inches. Convert and output

    the length in centimeters

    Convert the length in feet and inches to all

    inches:

    Multiply the number of feet by 12

    Use the conversion formula (1 inch = 2.54

    centimeters) to find the equivalent length in

    centimeters

  • 7/24/2019 02-Overview of C++ (Part 1)

    12/39CT077-3-2-DSTR Data Structures 12

    Programming Example

  • 7/24/2019 02-Overview of C++ (Part 1)

    13/39CT077-3-2-DSTR Data Structures 13

    Programming Example

    Sample Run:

    Enter two integers, one for feet, one for inches: 15 7

    The numbers you entered are 15 for feet and 7 for inches.The total number of inches = 187

    The number of centimeters = 474.98

  • 7/24/2019 02-Overview of C++ (Part 1)

    14/39CT077-3-2-DSTR Data Structures 14

    Selection Control

    One-Way Selection

    Two-Way Selection

    Multiple Selections: Nested if

    Conditions evaluation to true and false

    Multiple Selections: switch Statement

  • 7/24/2019 02-Overview of C++ (Part 1)

    15/39CT077-3-2-DSTR Data Structures 15

    One-Way Selection

    The syntax of one-way selection is:

    The statement is executed if the value of theexpression evaluates to true

    The statement is bypassed if the value is false;

    program goes to the next statement ifis a reserved word

  • 7/24/2019 02-Overview of C++ (Part 1)

    16/39CT077-3-2-DSTR Data Structures 16

    Two-Way Selection

    Two-way selection takes the form:

    If expression is true, statement1 is executed;

    otherwise, statement2 is executed

    statement1 and statement2 are any C++statements

    elseis a reserved word

  • 7/24/2019 02-Overview of C++ (Part 1)

    17/39CT077-3-2-DSTR Data Structures 17

    Multiple Selections: Nested if

    Nesting: one control statement in another

    To test more than one condition, an ifstatement

    can be nested inside another ifstatement

  • 7/24/2019 02-Overview of C++ (Part 1)

    18/39

    CT077-3-2-DSTR Data Structures 18

    Conditions Evaluation

    Beware that in C and C++, any value evaluated

    to zerois considered to be false, and any other

    value is treated as true

    This can help write short code, but may lead tobugs which are very difficult to detect:

    char c = 'Y';

    while(c = 'Y') {

    //do something here

    cout > c;

    } //why doesn't my loop ever end?

    This is an assignment operator, not checking equality

    operator. The code compiles without a problem because

    any value is evaluated as a condition, and this

    expression evaluates to true always ('Y' is not zero) !!

  • 7/24/2019 02-Overview of C++ (Part 1)

    19/39

    CT077-3-2-DSTR Data Structures 19

    switch Structures

    switch structure: alternate to

    if-else

    Value of the expression

    determines whichcorresponding action is taken

    Expression is sometimes

    called the selector

  • 7/24/2019 02-Overview of C++ (Part 1)

    20/39

    CT077-3-2-DSTR Data Structures 20

    switch Structures

    One or more statements may

    follow a case label

    The breakstatement may or

    may not appear after eachstatement

    switch, case, break, and

    defaultare reserved words

  • 7/24/2019 02-Overview of C++ (Part 1)

    21/39

    CT077-3-2-DSTR Data Structures 21

    switch Structures

  • 7/24/2019 02-Overview of C++ (Part 1)

    22/39

    CT077-3-2-DSTR Data Structures 22

    Repetition Control

    Repetition control structure causes a statement

    or group of statements to be repeated

    Repetition allows you to efficiently use variables

    For example, to add five numbers: Declare a variable for each number, input the

    numbers and add the variables together

    Create a loop that reads a number into avariable and adds it to a variable that contains

    the sum of the numbers

  • 7/24/2019 02-Overview of C++ (Part 1)

    23/39

    CT077-3-2-DSTR Data Structures 23

    while Loop

    The general form of the whilestatement is:

    Statement can be simple or compound

    Expression acts as a decision maker and is

    usually a logical expression

    whileis a reserved word

  • 7/24/2019 02-Overview of C++ (Part 1)

    24/39

    CT077-3-2-DSTR Data Structures 24

    while Loop

  • 7/24/2019 02-Overview of C++ (Part 1)

    25/39

    CT077-3-2-DSTR Data Structures 25

    Case 1: Counter-Controlled Loops

    If you know exactly how many pieces of data

    need to be read, the whileloop becomes a

    counter-controlled loop

  • 7/24/2019 02-Overview of C++ (Part 1)

    26/39

    CT077-3-2-DSTR Data Structures 26

    Case 2: Sentinel-Controlled Loops

    Sentinel variable is tested in the condition

    and loop ends when sentinel is encountered

  • 7/24/2019 02-Overview of C++ (Part 1)

    27/39

    CT077-3-2-DSTR Data Structures 27

    Case 3: Flag-Controlled Loops

    A flag-controlled while loop uses a bool variable

    to control the loop

  • 7/24/2019 02-Overview of C++ (Part 1)

    28/39

    CT077-3-2-DSTR Data Structures 28

    for Loop

    The general form of the forstatement is:

    The initial statement, loop condition, andupdate statement are called forloop control

    statements

    foris a reserved word

  • 7/24/2019 02-Overview of C++ (Part 1)

    29/39

    CT077-3-2-DSTR Data Structures 29

    dowhile Loop

    General form of a do...while:

    The statement executes first, and then the

    expression is evaluated

    To avoid an infinite loop, body must contain astatement that makes the expression false

    Loop always iterates at least once

  • 7/24/2019 02-Overview of C++ (Part 1)

    30/39

    CT077-3-2-DSTR Data Structures 30

    dowhile Loop

  • 7/24/2019 02-Overview of C++ (Part 1)

    31/39

    CT077-3-2-DSTR Data Structures 31

    Choosing the Right Looping

    Structure

    All three loops have their place in C++

    If you know or can determine in advance the

    number of repetitions needed, the forloop is

    the best choice

    If you do not know and cannot determine in

    advance the number of repetitions needed,

    and it could be zero, use a whileloop If you do not know and cannot determine in

    advance the number of repetitions needed,

    and it is at least one, use a do...while loop

  • 7/24/2019 02-Overview of C++ (Part 1)

    32/39

    CT077-3-2-DSTR Data Structures 32

    User-Defined Functions

    A function is defined in C++ in a very similar way

    as in Java

    A function can exist outside a class definition

    General syntax:

    functionType can be void, and a function may have no

    parameter list at all

    A function prototype is useful, and may be

    necessary at some cases

  • 7/24/2019 02-Overview of C++ (Part 1)

    33/39

    CT077-3-2-DSTR Data Structures 33

    Function Prototype

    Function prototype: function heading without the

    body of the function

    Syntax:

    It is not necessary to specify the variable name

    in the parameter list The data type of each parameter must be

    specified

  • 7/24/2019 02-Overview of C++ (Part 1)

    34/39

    CT077-3-2-DSTR Data Structures 34

    Function Example

  • 7/24/2019 02-Overview of C++ (Part 1)

    35/39

    CT077-3-2-DSTR Data Structures 35

    Function Example

  • 7/24/2019 02-Overview of C++ (Part 1)

    36/39

    CT077-3-2-DSTR Data Structures 36

    User-Defined Functions

    By default, parameters are passed by value

    Values that are passed when you call a function get

    copied, and any change to the parameters inside the

    function are not carried out on original passed-in

    parameters of the function call

    We will learn how to pass parameters by pointer and

    by reference in order to change them, or avoid copying

    Write a int max (int a, int b)function and test it

    Write a void changeMe(int v)function and test it

  • 7/24/2019 02-Overview of C++ (Part 1)

    37/39

    CT077-3-2-DSTR Data Structures 37

    Further Reading

    NTUs C++ Programming Tutorial

    The Process of Writing a C++ Program

    www3.ntu.edu.sg/home/ehchua/programming/cpp/cp0_I

    ntroduction.html

    Check this article on some common mistakes in

    C\C++ programming

    8 Common Programming Mistakes:

    www.cprogramming.com/tutorial/common.html

    http://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp0_Introduction.htmlhttp://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp0_Introduction.htmlhttp://www.cprogramming.com/tutorial/common.htmlhttp://www.cprogramming.com/tutorial/common.htmlhttp://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp0_Introduction.htmlhttp://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp0_Introduction.htmlhttp://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp0_Introduction.htmlhttp://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp0_Introduction.htmlhttp://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp0_Introduction.htmlhttp://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp0_Introduction.html
  • 7/24/2019 02-Overview of C++ (Part 1)

    38/39

    CT077-3-2-DSTR Data Structures 38

  • 7/24/2019 02-Overview of C++ (Part 1)

    39/39

    What we will cover next

    Overview of C++

    Simple Data Types

    Arrays

    Passing parameters to functions

    Passing arrays to functions