object oriented programming (oop) - cs304 power point slides lecture 21

Upload: sameer-hane

Post on 04-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    1/49

    Object-Oriented Programming

    (OOP)Lecture No. 21

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    2/49

    Unary OperatorsUnary operators are usually prefix,

    except for ++and --

    ++and--both act as prefix andpostfix

    Example:

    h++;

    g-- + ++h - --i;

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    3/49

    Unary OperatorsBehavior of ++ and -- for pre-defined types:

    Post-increment ++:Post-increment operator ++increments the current value and thenreturns the previous value

    Post-decrement --:

    Works exactly like post ++

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    4/49

    Unary OperatorsExample:

    int x = 1, y = 2;

    cout

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    5/49

    Unary OperatorsExample:

    int y = 2;

    y++++; // Errory++ = x; // Error

    Post-increment ++returns by value,

    hence an error while

    assignment

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    6/49

    Unary OperatorsBehavior of ++ and -- for pre-defined types:

    Pre-increment ++:

    Pre-increment operator ++ incrementsthe current value and then returns itsreference

    Pre-decrement --:

    Works exactly like Pre-increment ++

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    7/49

    Unary OperatorsExample:

    int y = 2;

    cout

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    8/49

    Unary OperatorsExample:

    int x = 2, y = 2;

    ++++y;

    cout

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    9/49

    Unary OperatorsExample (Pre-increment):

    class Complex{

    double real, img;

    public:

    ...

    Complex & operator ++ ();

    // friend Complex & operator// ++(Complex &);

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    10/49

    Unary OperatorsMember function definition:

    Complex & Complex::operator++(){

    real = real + 1;

    return * this;}

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    11/49

    Unary OperatorsFriend function definition:

    Complex & operator ++ (Complex& h){

    h.real += 1;

    return h;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    12/49

    Unary OperatorsComplex h1, h2, h3;

    ++h1;

    Function operator++()returns areference so that the object can beused as an lvalue

    ++h1 = h2 + ++h3;

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    13/49

    Unary OperatorsHow does a compiler know

    whether it is a pre-incrementor a post-increment ?

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    14/49

    Unary OperatorsA post-fix unary operator isimplemented using:

    Member function with 1 dummy intargument

    OR

    Non-member function with twoarguments

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    15/49

    Unary OperatorsIn post increment, current value ofthe object is stored in a temporary

    variable

    Current object is incremented

    Value of the temporary variable isreturned

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    16/49

    Unary OperatorsPost-increment operator:

    class Complex{

    ...

    Complex operator ++ (int);

    // friend Complex operator

    // ++(const Complex &, int);

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    17/49

    Unary OperatorsMember function definition:

    Complex Complex::operator ++(int){

    complex t = *this;

    real += 1;

    return t;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    18/49

    Unary OperatorsFriend function definition:

    Complex operator ++ (const

    Complex & h, int){

    complex t = h;

    h.real += 1;return t;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    19/49

    Unary OperatorsThe dummy parameter in theoperator function tells compiler that it

    is post-incrementExample:

    Complex h1, h2, h3;

    h1++;

    h3++ = h2 + h3++; // Error

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    20/49

    Unary OperatorsThe preand postdecrementoperator --is implemented inexactly the same way

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    21/49

    Type onversionThe compiler automatically performsa type coercion of compatible types

    e.g:

    int f = 0.021;

    double g = 34;

    // type floatis automatically converted// into int. Compiler only issues a

    // warning

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    22/49

    Type onversionThe user can also explicitly convertbetween types:

    int g = (int)0.0210;

    double h = double(35);// type floatis explicitly converted// (casted) into int. Not even a warning

    // is issued now

    C style typecasting

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    23/49

    Type onversionFor user defined classes, there are

    two types of conversionsFrom any other type to current type

    From current type to any other type

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    24/49

    Type onversionConversion from any other type to

    current type:Requires a constructor with a singleparameter

    Conversion from current type to anyother type:

    Requires an overloaded operator

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    25/49

    Type onversionConversion from other type tocurrent type (intto String):

    class String{

    ...

    public:String(int a);

    char * GetStringPtr()const;

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    26/49

    Type onversionString::String(int a){

    cout

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    27/49

    Type onversionint main(){

    String s = 345;

    cout

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    28/49

    Type onversionOutput:String(int) called

    345

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    29/49

    Type onversionAutomatic conversion hasdrawbacks

    Conversion takes placetransparently even if the user didntwanted the conversion

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    30/49

    Type onversionUser can write the following code to initializethe string with a single character:

    int main(){

    String s = A;

    cout

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    31/49

    Type onversionOutput:String(int) called

    65

    2

    ASCII code

    for A !!!

    String size

    is also 2

    instead of 1

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    32/49

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    33/49

    Type onversionKeyword explicit only workswith constructors

    Example:class String{

    public:

    explicit String(int);

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    34/49

    Type onversionint main(){

    String s;

    // Error

    s = A;

    return 0;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    35/49

    Type onversionint main(){

    String s1, s2;

    // valid, explicit casting

    s1 = String(101);

    // OR

    s2 = (String)204;

    return 0;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    36/49

    Type onversionThere is another method for type

    conversion:Operator overloading

    (Converting from current type toany other type)

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    37/49

    Type onversionGeneral Syntax:

    TYPE1::Operator TYPE2();

    Must be a member function

    NO return type and arguments arespecified

    Return type is implicitly taken to be

    TYPE2by compiler

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    38/49

    Type onversionOverloading pre-defined types:

    class String{

    public:

    operator int();operator char *();

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    39/49

    Type onversionString::operator int(){

    if(size > 0)

    return atoi(bufferPtr);

    else

    return -1;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    40/49

    Type onversionString::operator char *(){

    return bufferPtr;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    41/49

    Type onversionint main(){

    String s("2324");cout

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    42/49

    Type onversionOutput:

    2324

    2324

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    43/49

    Type onversionUser-defined types can be overloadedin exactly the same way

    Only prototype is shown below:class String{

    operator Complex();

    operator HugeInt();

    operator IntVector();

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    44/49

    Type onversionModifying String class:

    class String{

    public:

    String(char *);operator int();

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    45/49

    Type onversionint main(){

    String s(Fakhir");//

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    46/49

    Type onversionOutput:

    Junk Returned

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    47/49

    Type onversionModifying String class:

    class String{

    public:

    String(char *);int AsInt();

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    48/49

    Type onversionint String::AsInt(){

    if(size > 0)

    return atoi(bufferPtr);

    else

    return -1;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

    49/49

    Type onversionint main(){

    String s(434");

    //