computer notes - binary operators

Upload: ecomputernotes

Post on 06-Apr-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Computer Notes - Binary Operators

    1/24

    Binary operatorsBinary operators

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    2/24

    Binary operatorsBinary operators

    Overloading + operator:Overloading + operator:

    class Complex{class Complex{

    private:private:

    double real,double real, imgimg;;public:public:

    Complex operator +(constComplex operator +(constComplex &Complex & rhsrhs););

    };};

  • 8/3/2019 Computer Notes - Binary Operators

    3/24

    Binary operatorsBinary operators

    ComplexComplex Complex::operatorComplex::operator +(+(const Complex &const Complex & rhsrhs){){

    Complex t;Complex t;

    t.realt.real = real += real + rhs.realrhs.real;;

    t.imgt.img == imgimg ++ rhs.imgrhs.img;;

    return t;return t;

    }}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    4/24

    Binary operatorsBinary operators

    The return type is Complex so as toThe return type is Complex so as to

    facilitate complex statements like:facilitate complex statements like:Complex t = c1 + c2 + c3;Complex t = c1 + c2 + c3;

    The above statement is automaticallyThe above statement is automaticallyconverted by the compiler intoconverted by the compiler intoappropriate function calls:appropriate function calls:

    ((c1.operator +(c2)c1.operator +(c2))).operator.operator+(c3);+(c3);

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    5/24

    Binary operatorsBinary operators

    The binary operator is always calledThe binary operator is always calledwith reference to the left hand argumentwith reference to the left hand argument

    Example:Example:InIn c1+c2c1+c2,, c1.operator+(c2)c1.operator+(c2)

    InIn c2+c1c2+c1,, c2.operator+(c1)c2.operator+(c1)

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    6/24

    Binary operatorsBinary operators

    The above examples donThe above examples dont handlet handle

    the following situation:the following situation:

    Complex c1;Complex c1;

    c1 + 2.325c1 + 2.325

    To do this, we have to modify theTo do this, we have to modify theComplexComplex classclass

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    7/24

    Binary operatorsBinary operatorsModifying the complex class:Modifying the complex class:

    class Complex{class Complex{

    ......

    ComplexComplex operator+(constoperator+(const

    Complex &Complex & rhsrhs););

    ComplexComplex operator+(constoperator+(constdouble&double& rhsrhs););

    };}; http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    8/24

    Binary operatorsBinary operators

    Complex operator + (const double&Complex operator + (const double&

    rhsrhs){){

    Complex t;Complex t;

    t.realt.real = real += real + rhsrhs;;

    t.imgt.img == imgimg;;

    return t;return t;

    }}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    9/24

    Binary operatorsBinary operators

    Now suppose:Now suppose:

    Complex c2, c3;Complex c2, c3;

    We can do the following:We can do the following:

    Complex c1 = c2 + c3;Complex c1 = c2 + c3;

    andand

    Complex c4 = c2 + 235.01;Complex c4 = c2 + 235.01;

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    10/24

    Binary operatorsBinary operators

    But problem arises if we do the following:But problem arises if we do the following:

    Complex c5 =Complex c5 = 450.120450.120 + c1;+ c1;

    The + operator is called with reference toThe + operator is called with reference to450.120450.120

    No predefined overloaded + operator is thereNo predefined overloaded + operator is there

    that takesthat takes ComplexComplex as an argumentas an argument

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    11/24

    Binary operatorsBinary operators

    Now if we write the following two functionsNow if we write the following two functions

    to the class, we can add ato the class, we can add a ComplexComplex to ato a realrealor vice versa:or vice versa:

    Class Complex{Class Complex{

    friend Complex operator + (constfriend Complex operator + (constComplex & lhs, const double &Complex & lhs, const double & rhsrhs););

    friend Complex operator + (constfriend Complex operator + (constdouble &double & lhslhs, const Complex &, const Complex & rhsrhs););

    }}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    12/24

    Binary operatorsBinary operators

    Complex operator +(const Complex &Complex operator +(const Complex &

    lhs, const double&lhs, const double& rhsrhs){){

    Complex t;Complex t;

    t.realt.real == lhs.reallhs.real ++ rhsrhs;;

    t.imgt.img == lhs.imglhs.img;;

    return t;return t;}}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    13/24

    Binary operatorsBinary operators

    Complex operator + (const double &Complex operator + (const double &

    lhs, const Complex &lhs, const Complex & rhsrhs){){

    Complex t;Complex t;

    t.realt.real = lhs += lhs + rhs.realrhs.real;;

    t.imgt.img == rhs.imgrhs.img;;

    return t;return t;}}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    14/24

    Binary operatorsBinary operators

    Class Complex{Class Complex{

    Complex operator + (constComplex operator + (constComplex &);Complex &);

    friend Complex operator + (constfriend Complex operator + (constComplex &, const double &);Complex &, const double &);

    friend Complex operator + (constfriend Complex operator + (const

    double &, const Complex &);double &, const Complex &);};};

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    15/24

    Binary operatorsBinary operators

    Other binary operators are overloaded veryOther binary operators are overloaded very

    similar to the + operator as demonstrated insimilar to the + operator as demonstrated inthe above examplesthe above examples

    Example:Example:

    Complex operatorComplex operator ** (const Complex &(const Complex &c1, const Complex & c2);c1, const Complex & c2);

    Complex operatorComplex operator // (const Complex &(const Complex &c1, const Complex & c2);c1, const Complex & c2);

    Complex operatorComplex operator -- (const Complex &(const Complex &c1, const Complex & c2);c1, const Complex & c2);

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    16/24

    Assignment operatorAssignment operator

    Consider a string class:Consider a string class:class String{class String{

    intint size;size;

    char *char *bufferPtrbufferPtr;;

    public:public:

    String();String();

    String(charString(char *);*);

    String(constString(const String &);String &);

    };};

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    17/24

    Assignment operatorAssignment operator

    String::String(charString::String(char **ptrptr){){

    if(ptrif(ptr != NULL){!= NULL){

    size =size = strlen(ptrstrlen(ptr););

    bufferPtrbufferPtr = new char[size+1];= new char[size+1];strcpy(bufferPtrstrcpy(bufferPtr,,ptrptr););

    }}

    else{else{

    bufferPtrbufferPtr = NULL; size = 0; }= NULL; size = 0; }

    }}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    18/24

    Assignment operatorAssignment operator

    String::String(constString::String(const String &String & rhsrhs){){

    size =size = rhs.sizerhs.size;;

    if(rhs.sizeif(rhs.size != 0){!= 0){

    bufferPtrbufferPtr = new char[size+1];= new char[size+1];strcpy(bufferPtrstrcpy(bufferPtr,,ptrptr););

    }}

    elseelse

    bufferPtrbufferPtr = NULL;= NULL;

    }}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    19/24

    Assignment operatorAssignment operator

    intintmain(){main(){

    String str1(String str1(Hello");Hello");

    String str2(String str2(WorldWorld););

    str1 = str2;str1 = str2;

    return 0;return 0;}}

    Member wise copy

    assignment

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    20/24

    Assignment operatorAssignment operator

    Result ofResult ofstr1 = str2str1 = str2 (memory leak)(memory leak)

    str1

    Hello

    str2

    World

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    21/24

    Assignment operatorAssignment operator

    Modifying:Modifying:

    class String{class String{

    public:public:

    void operator =(const String &);void operator =(const String &);

    };};

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Binary Operators

    22/24

    Assignment operatorAssignment operator

    voidvoidString::operatorString::operator = (const String &= (const String & rhsrhs){){

    size =size = rhs.sizerhs.size;;

    if(rhs.sizeif(rhs.size != 0){!= 0){

    delete []delete []bufferPtrbufferPtr;;bufferPtrbufferPtr = new char[rhs.size+1];= new char[rhs.size+1];

    strcpy(bufferPtr,rhs.bufferPtrstrcpy(bufferPtr,rhs.bufferPtr););

    }}

    elseelse

    bufferPtrbufferPtr = NULL;= NULL;

    }}

  • 8/3/2019 Computer Notes - Binary Operators

    23/24

    Assignment operatorAssignment operator

    intintmain(){main(){

    String str1(String str1(ABC");ABC");

    String str2(String str2(DEDE), str3(), str3(FGFG););

    str1 = str2;str1 = str2; // Valid// Valid

    str1 = str2 = str3;str1 = str2 = str3; // Error// Error

    return 0;return 0;

    }}

  • 8/3/2019 Computer Notes - Binary Operators

    24/24

    Assignment operatorAssignment operator

    str1=str2=str3str1=str2=str3 is resolved as:is resolved as:str1.operator=(str1.operator=(str2.operator=str2.operator=

    (str3)(str3)))

    Return type is

    void. Parametercant be void

    http://ecomputernotes.com