03-Overview of C++ (Part 2)

download 03-Overview of C++ (Part 2)

of 29

Transcript of 03-Overview of C++ (Part 2)

  • 7/24/2019 03-Overview of C++ (Part 2)

    1/29

    Data Structure Using C++

    Overview of C++ (part 2)

  • 7/24/2019 03-Overview of C++ (Part 2)

    2/29

    CT077-3-2-DSTR Data Structures 2

    Learning Objective

    At the end of the lecture, you would know:

    the simple data types in C++

    how to declare and manipulate data into arrays

    the difference between passing parameters to

    functions by value or by reference

    how to pass arrays to functions

  • 7/24/2019 03-Overview of C++ (Part 2)

    3/29

    CT077-3-2-DSTR Data Structures 3

    C++ Data Type

    A data type is called simple if variables of that type can

    store only one value at a time

    Simple types are also called: Primitive or Built-in Types

    Data Type

    Simple

    Integral

    char shortint long

    bool

    Floating

    float doublelong double

    Structured

    Array

    StructUnion

    Class

  • 7/24/2019 03-Overview of C++ (Part 2)

    4/29

    CT077-3-2-DSTR Data Structures 4

    C++ Data Type

    bool type is not supported by all C++ compilers

    Can add unsigned modifier before char, short, int, and

    long, so that variables of that type are positive always

    (maximum storable number gets doubled)

    Simple Data Types

    Integral

    unsigned char unsigned short

    unsigned int unsigned long

    bool

    Floating

    float double

    longdouble

  • 7/24/2019 03-Overview of C++ (Part 2)

    5/29

    CT077-3-2-DSTR Data Structures 5

    C++ Data Type

    A structured data type is one in which each data item is acollection of other data items

    Very similar to classes, but should be thought of as not

    having methods (just data grouping)

    Data Type

    Simple

    Integral

    char shortint long

    bool

    Floating

    float doublelong double

    Structured

    Array

    StructUnion

    Class

  • 7/24/2019 03-Overview of C++ (Part 2)

    6/29

    CT077-3-2-DSTR Data Structures 6

    Arrays

    Array: a collection of a fixed number of

    components wherein all of the components have

    the same data type

    In a one-dimensional array, the components arearranged in a list form

    Syntax for declaring a one-dimensional array:

  • 7/24/2019 03-Overview of C++ (Part 2)

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

    Arrays

    Example:

    intnum[5];

  • 7/24/2019 03-Overview of C++ (Part 2)

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

    Accessing Array Components

    General syntax:

    where indexExp, called an index, is anyexpression whose value is a nonnegative integer

    Index value specifies the position of the

    component in the array (called random access) The array index always starts at 0

  • 7/24/2019 03-Overview of C++ (Part 2)

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

    Accessing Array Components

    Example:

    list [3] = 10;

    list [6] = 35;

    list [5] = list [3] + list [6];

    In memory, array elements

    are reserved adjacent to

    each other in order toguarantee fast random

    access to any element

  • 7/24/2019 03-Overview of C++ (Part 2)

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

    Array Initialization

    Arrays can be initialized during declaration

    In this case, it is not necessary to specify the

    size of the array

    Size determined by the number of initialvalues in the braces

    Example:

    doublesales[] = {12.25, 32.50, 16.90, 23, 45.68};

  • 7/24/2019 03-Overview of C++ (Part 2)

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

    Array Initialization

    The statement:

    intlist[10] = {0};

    declares list to be an array of 10 components

    and initializes all of them to zero The statement:

    int list[10] = {8, 5, 12};

    declares list to be an array of 10 components,initializes list[0] to 8, list[1] to 5, list[2] to 12 and

    all other components are initialized to 0

  • 7/24/2019 03-Overview of C++ (Part 2)

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

    Some Restrictions on Array

    Processing

    Consider the following statements:

    C++ does not allow aggregate operations on anarray:

    Solution:

  • 7/24/2019 03-Overview of C++ (Part 2)

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

    Some Restrictions on Array

    Processing

    The following is illegal too:

    Solution:

    The following statements are legal, but do not

    give the desired results This doesntprint all elements of the array.It prints the memory address of the firstelement (also called base address)

    This doesnt compare all elements of

    both arrays, but compares the memory

    base addresses of the two arrays

  • 7/24/2019 03-Overview of C++ (Part 2)

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

    Two-Dimensional Arrays

    Two-dimensional array: collection of a fixed

    number of components (of the same type)

    arranged in two dimensions

    Sometimes called matrices or tables Declaration syntax:

    where intexp1 and intexp2 specify the number of

    rows and the number of columns, respectively in

    the array

  • 7/24/2019 03-Overview of C++ (Part 2)

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

    Two-Dimensional Arrays

    Example:

    doublesales [10][5];

  • 7/24/2019 03-Overview of C++ (Part 2)

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

    Accessing Array Components

    Syntax:

    where indexexp1 and indexexp2 are expressionsyielding nonnegative integer values, and specify

    the row and column position

    2D arrays are also specially arranged in memory

    as adjacent rows, so that fast random access to

    any element (any row, any column) is guaranteed

  • 7/24/2019 03-Overview of C++ (Part 2)

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

    Accessing Array Components

    Example:

    sales [5][3] = 25.75;

  • 7/24/2019 03-Overview of C++ (Part 2)

    18/29

    CT077-3-2-DSTR Data Structures 18

    Two-Dimensional Array

    Initialization

    Two-dimensional arrays can be initialized when

    they are declared:

    Elements of each row are enclosed within

    braces and separated by commasAll rows are enclosed within braces

    For number arrays, if all components of a row

    arent specified, unspecified ones are set to 0

  • 7/24/2019 03-Overview of C++ (Part 2)

    19/29

    CT077-3-2-DSTR Data Structures 19

    Passing Parameters to Functions

    Value parameter: the formal parameter receives

    a copy of the content of corresponding actual

    parameter

    Reference parameter: the formal parameter

    receives the location (memory address) of the

    corresponding actual parameter

    Syntax: put & mark before parameter name

  • 7/24/2019 03-Overview of C++ (Part 2)

    20/29

    CT077-3-2-DSTR Data Structures 20

    Example Program

    void grow(int&age) {

    age = age + 1;

    cout

  • 7/24/2019 03-Overview of C++ (Part 2)

    21/29

    CT077-3-2-DSTR Data Structures 21

    Example Program - 2

    void main() {

    int value= 20;

    inc (value);

    cout

  • 7/24/2019 03-Overview of C++ (Part 2)

    22/29

    CT077-3-2-DSTR Data Structures 22

    Example Program - 3

  • 7/24/2019 03-Overview of C++ (Part 2)

    23/29

    CT077-3-2-DSTR Data Structures 23

    Example Program - 3

  • 7/24/2019 03-Overview of C++ (Part 2)

    24/29

    CT077-3-2-DSTR Data Structures 25

    Summary - Passing Parameters

    Value parameter: During program execution, manipulating value parameter

    changes its data copy (stored in its own memory space)

    The actual parameter stays intact

    Reference parameter:

    Received address leads to memory space of the

    corresponding actual parameter Changing reference parameter affects the actual

    parameter

  • 7/24/2019 03-Overview of C++ (Part 2)

    25/29

    CT077-3-2-DSTR Data Structures 26

    Reference Parameters

    Reference parameters are useful in three

    situations:

    You want to change the actual parameter

    No easy way to return more than one valuefrom a function, so you pass more reference

    parameters

    When passing the address would save

    memory space and time (no copying)

  • 7/24/2019 03-Overview of C++ (Part 2)

    26/29

    CT077-3-2-DSTR Data Structures 27

    Arrays as Parameters to Functions

    Arrays are passed by reference, always

    The symbol & is not used when declaring an

    array as a formal parameter

    The size of the array is usually omitted If provided, it is ignored by the compiler

    Unlike Java, array size must be passed-in to the

    function by some means (parameter, etc.)

  • 7/24/2019 03-Overview of C++ (Part 2)

    27/29

    CT077-3-2-DSTR Data Structures 28

    Implement examples given in the lecture

    Write a: voidswap (int , int) function which

    swaps the values of its two given parameters

    Homework

  • 7/24/2019 03-Overview of C++ (Part 2)

    28/29

    CT077-3-2-DSTR Data Structures 29

  • 7/24/2019 03-Overview of C++ (Part 2)

    29/29

    What we will cover next

    Overview of C++

    Pointers

    Passing parameters to functions using pointers

    Dynamic Memory Allocation

    Very Important Topics

    - not to be missed out -