lect01 c++

Upload: abagalawis

Post on 08-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Lect01 C++

    1/55

    C++ Review

    Lecture 1

  • 8/6/2019 Lect01 C++

    2/55

    2

    Outline

    C++ basic features Programming paradigm and statement syntax

    Class definitions Data members, methods, constructor, destructor

    Pointers, arrays, and strings Parameter passing in functions

    Unlike the text book, in this course, we willnot use Templates

    Friend

    Operator overloading

    I/O streams An example on file copy

    Makefile on unix

  • 8/6/2019 Lect01 C++

    3/55

    3

    Functions & Memory

    Every function needs a place tostore its local variables.

    Collectively, this storage is called

    the stack

    This storage (memory aka RAM),

    is a series of storage spaces andtheir numerical addresses

    Instead of using raw addresses,

    we use variables to attach a

    name to an address

    All of the data/variables for aparticular function call are located

    in a stack frame

    Memory

    location

    void aFunc(int x, int y)

    {double d1, d2;

    int i;}

    x

    y

    d2

    d1

    i

  • 8/6/2019 Lect01 C++

    4/55

  • 8/6/2019 Lect01 C++

    5/55

  • 8/6/2019 Lect01 C++

    6/55

    6

    Modular Concept - Problems

    Advantages

    Disadvantages

    Decoupled Data and Operations The resulting module structure is oriented on the

    operations rather than the actual data

    The defined operations specify the data to be

    used. E.g.: in previous example,

  • 8/6/2019 Lect01 C++

    7/55

    7

    Object-Oriented Concept (C++)

    Objects of the program interact by sending messages to each

    other

  • 8/6/2019 Lect01 C++

    8/55

    8

    Basic C++

    Inherit allC syntax Primitive data types

    Supported data types: int, long, short, float,double, char, bool, and enum

    The size of data types is platform-dependent

    Basic expression syntax Defining the usual arithmetic and logical operations such

    as +, -, /, %, *, &&, !, and ||

    Defining bit-wise operations, such as &, |, and ~

    Basic statement syntax If-else, for, while, and do-while

  • 8/6/2019 Lect01 C++

    9/55

    9

    Basic C++ (cont)

    Add a new comment mark // For 1 line comment

    /* */ for a group ofline comment

    New data type Reference data type &. Much likes pointer

    int ix; /* ix is "real" variable */

    int & rx = ix; /* rx is "alias" for ix */

    ix = 1; /* also rx == 1 */

    rx = 2; /* also ix == 2 */

    const support for constant declaration, justlikes C

    Use this when

  • 8/6/2019 Lect01 C++

    10/55

  • 8/6/2019 Lect01 C++

    11/55

    11

    Two labels:publicandprivate Determine visibility of class members

    A member that ispublicmay be accessed by any method in

    any class

    A member that isprivate may only be accessed by methods

    in its class Information hiding

    Data members are declaredprivate, thus restricting access

    to internal details of the class

    Methods intended for general use are madepublic

    Information Hiding in C++

  • 8/6/2019 Lect01 C++

    12/55

    12

    A constructoris a special method that describes how aninstance of the class (called object) is constructed

    Whenever an instance of the class is created, itsconstructor is called.

    C++ provides a default constructorfor each class, whichis a constructor with no parameters. But, one can definemultiple constructors for the same class, and may evenredefine the default constructor

    How to distinguish them?

    E.g. How to create IntCell with storedValue=10?

    storedValue=0?

    Hint: C++ has an operator new

    Constructors

  • 8/6/2019 Lect01 C++

    13/55

  • 8/6/2019 Lect01 C++

    14/55

    14

    Pointers

    Apointeris a variable which

    contains addresses of other

    variables

    Accessing the data at the

    contained address is calleddereferencing a pointer or

    following a pointer

    n(4096)

    y

    (4100)

    x

    (4104)

    4096

    7pointer

  • 8/6/2019 Lect01 C++

    15/55

  • 8/6/2019 Lect01 C++

    16/55

    16

    Interface and Implementation

    In C++ it is more common to separate the classinterface from its implementation.

    The interface lists the class and its members (data

    and functions).

    The implementation provides implementations ofthe functions.

  • 8/6/2019 Lect01 C++

    17/55

    17

    class IntCell

    {

    public:

    explicit IntCell( intinitialValue = 0 );

    int read( ) const;

    void write( int x );

    private:

    int storedValue;

    }

    IntCell::IntCell( int initialValue )

    : storedValue ( initialValue ){ }

    int IntCell::read( ) const

    { return storedValue; }

    void IntCell::write( )

    { storedValue = x; }

    The interface is typically placed in a file that ends with .h. Themember functions are defined as:

    ReturnType FunctionName(parameterList);The implementation file typically ends with .cpp, .cc, or.C. The

    member functions are defined as follows:

    ReturnType ClassName::FunctionName(parameterList)

    { }

    IntCell.h IntCell.cpp

    Scopingoperator

  • 8/6/2019 Lect01 C++

    18/55

  • 8/6/2019 Lect01 C++

    19/55

  • 8/6/2019 Lect01 C++

    20/55

  • 8/6/2019 Lect01 C++

    21/55

  • 8/6/2019 Lect01 C++

    22/55

  • 8/6/2019 Lect01 C++

    23/55

    23

    int f(int x) { cout

  • 8/6/2019 Lect01 C++

    24/55

  • 8/6/2019 Lect01 C++

    25/55

  • 8/6/2019 Lect01 C++

    26/55

  • 8/6/2019 Lect01 C++

    27/55

  • 8/6/2019 Lect01 C++

    28/55

    28

    A reference variable is different from a pointer

    A pointer need NOT be initialized while defining, but a

    reference variable should always refer to some other

    object.int * p;

    int m = 10;int & j = m; //valid

    int & k; //compilation error

    Reference Variables (cont)

  • 8/6/2019 Lect01 C++

    29/55

  • 8/6/2019 Lect01 C++

    30/55

    30

    A pointercan be assigned a new value to point at adifferent object, but a reference variable always refers tothe same object.Assigning a reference variable with anew value actually changes the value of the referredobject.int * p;

    int m = 10;int & j = m; //valid

    p = &m; //p now points at m, p=address of m

    int n = 12;

    j = n; // the value of m is set to 12. But j still refers to m,not to n.

    cout

  • 8/6/2019 Lect01 C++

    31/55

  • 8/6/2019 Lect01 C++

    32/55

  • 8/6/2019 Lect01 C++

    33/55

  • 8/6/2019 Lect01 C++

    34/55

    34

    Class and Objects

    point.cc, point.cpp

    void Point::setX(const int val) {

    _x = val;

    }

    void Point::setY(const int val) {

    _y = val;

    }

  • 8/6/2019 Lect01 C++

    35/55

    35

    Main program

    main.cc, main.cppint main(int argc, char* argv[]) {

    Point apoint;

    apoint.setX(1); // Initializationapoint.setY(1);

    //

    // x is needed from here, hence, we define ithere and// initialize it to the x-coordinate of apoint

    //

    int x = apoint.getX();

    }

  • 8/6/2019 Lect01 C++

    36/55

  • 8/6/2019 Lect01 C++

    37/55

  • 8/6/2019 Lect01 C++

    38/55

    38

    Standard Input/OutputStreams

    Stream is a sequence of characters

    Working with cin and cout

    Streams convert internal representations to

    character streams >> input operator (extractor)

  • 8/6/2019 Lect01 C++

    39/55

  • 8/6/2019 Lect01 C++

    40/55

  • 8/6/2019 Lect01 C++

    41/55

  • 8/6/2019 Lect01 C++

    42/55

  • 8/6/2019 Lect01 C++

    43/55

  • 8/6/2019 Lect01 C++

    44/55

    44

    CountChars.cpp (Output)

    cout

  • 8/6/2019 Lect01 C++

    45/55

  • 8/6/2019 Lect01 C++

    46/55

    46

    Files

    #define associates the name of the streamwith the actual file name

    fail() function - returns nonzero if file fails

    to open Program CopyFile.cpp demonstrates the use

    of the otherfstream functions

    get, put, close and eof

    Copy from one file to another

  • 8/6/2019 Lect01 C++

    47/55

  • 8/6/2019 Lect01 C++

    48/55

  • 8/6/2019 Lect01 C++

    49/55

  • 8/6/2019 Lect01 C++

    50/55

  • 8/6/2019 Lect01 C++

    51/55

    51

    CopyFile.cpp (Opening OutputFile)

    outs.open(outFile);if (outs.fail()) {

    cerr

  • 8/6/2019 Lect01 C++

    52/55

    52

    CopyFile.cpp (Copy Line by Line)

    // Copy each character from inData to outData.lineCount = 0;

    do{

    if (copyLine(ins, outs) != 0)

    lineCount++;

    }while (!ins.eof());

    // Display a message on the screen.cout

  • 8/6/2019 Lect01 C++

    53/55

    53

    CopyFile.cpp (copyLine procedure)

    // Copy one line of text from one file to another

    // Pre: ins is opened for input and outs for

    // output.

    // Post: Next line of ins is written to outs.

    // The last character processed from

    // ins is ;// the last character written to outs

    // is .

    // Returns: The number of characters copied.

  • 8/6/2019 Lect01 C++

    54/55

    54

    CopyFile.cpp (Character Reading)

    int copyLine (ifstream& ins, ofstream& outs){// Local data ...

    const charNWLN = '\n';

    char nextCh;

    int charCount = 0;

    // Copy all data characters from stream ins to// stream outs.

    ins.get(nextCh);

    while ((nextCh != NWLN) && !ins.eof()){

    outs.put(nextCh);

    charCount++;

    ins.get (nextCh);} // end while

  • 8/6/2019 Lect01 C++

    55/55

    55

    CopyFile.cpp (Detection of EOF)

    // If last character read was NWLN write it

    // to outs.

    if (!ins.eof())

    {

    outs.put(NWLN);

    charCount++;}

    return charCount;

    } // end copyLine