• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Operator overloading (oop)lecture2
 

Operator overloading (oop)lecture2

on

  • 339 views

 

Statistics

Views

Total Views
339
Views on SlideShare
339
Embed Views
0

Actions

Likes
0
Downloads
8
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Operator overloading (oop)lecture2 Operator overloading (oop)lecture2 Presentation Transcript

    • Object-Oriented ProgrammingObject-Oriented Programming (OOP)(OOP) Lecture No. 17Lecture No. 17
    • Binary operatorsBinary operators ►Overloading + operator:Overloading + operator: class Complex{class Complex{ private:private: double real, img;double real, img; public:public: …… Complex operator +(constComplex operator +(const Complex & rhs);Complex & rhs); };};
    • Binary operatorsBinary operators Complex Complex::operator +(Complex Complex::operator +( const Complex & rhs){const Complex & rhs){ Complex t;Complex t; t.real = real + rhs.real;t.real = real + rhs.real; t.img = img + rhs.img;t.img = img + rhs.img; return t;return t; }}
    • 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 isThe above statement is automatically converted by theautomatically converted by the compiler into appropriate functioncompiler into appropriate function calls:calls: ((c1.operator +(c2)c1.operator +(c2))).operator +.operator + (c3);(c3);
    • Binary operatorsBinary operators ►The binary operator is always calledThe binary operator is always called with reference to the left handwith reference to the left hand argumentargument ►Example:Example: InIn c1+c2c1+c2,, c1.operator+(c2)c1.operator+(c2) InIn c2+c1c2+c1,, c2.operator+(c1)c2.operator+(c1)
    • Binary operatorsBinary operators ►The above examples don’tThe above examples don’t handle the following situation:handle the following situation: Complex c1;Complex c1; c1 + 2.325c1 + 2.325 ►To do this, we have to modifyTo do this, we have to modify thethe ComplexComplex classclass
    • Binary operatorsBinary operators ►Modifying the complex class:Modifying the complex class: class Complex{class Complex{ ...... Complex operator+(constComplex operator+(const Complex & rhs);Complex & rhs); Complex operator+(constComplex operator+(const double& rhs);double& rhs); };};
    • Binary operatorsBinary operators Complex operator + (const double&Complex operator + (const double& rhs){rhs){ Complex t;Complex t; t.real = real + rhs;t.real = real + rhs; t.img = img;t.img = img; return t;return t; }}
    • 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;
    • 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 to 450.120450.120 ►No predefined overloaded + operator isNo predefined overloaded + operator is there that takesthere that takes ComplexComplex as an argumentas an argument
    • Binary operatorsBinary operators ►Now if we write the following twoNow if we write the following two functions to the class, we can add afunctions to the class, we can add a ComplexComplex to ato a realreal or vice versa:or vice versa: Class Complex{Class Complex{ …… friend Complex operator + (constfriend Complex operator + (const Complex & lhs, const double &Complex & lhs, const double & rhsrhs);); friend Complex operator + (constfriend Complex operator + (const double &double & lhslhs, const Complex & rhs);, const Complex & rhs); }}
    • Binary operatorsBinary operators Complex operator +(const Complex &Complex operator +(const Complex & lhs, const double& rhs){lhs, const double& rhs){ Complex t;Complex t; t.real = lhs.real + rhs;t.real = lhs.real + rhs; t.img = lhs.img;t.img = lhs.img; return t;return t; }}
    • Binary operatorsBinary operators Complex operator + (const double &Complex operator + (const double & lhs, const Complex & rhs){lhs, const Complex & rhs){ Complex t;Complex t; t.real = lhs + rhs.real;t.real = lhs + rhs.real; t.img = rhs.img;t.img = rhs.img; return t;return t; }}
    • Binary operatorsBinary operators Class Complex{Class Complex{ …… Complex operator + (constComplex operator + (const Complex &);Complex &); friend Complex operator + (constfriend Complex operator + (const Complex &, const double &);Complex &, const double &); friend Complex operator + (constfriend Complex operator + (const double &, const Complex &);double &, const Complex &); };};
    • Binary operatorsBinary operators ►Other binary operators are overloadedOther binary operators are overloaded very similar to the + operator asvery similar to the + operator as demonstrated in the above examplesdemonstrated in the 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);
    • AssignmentAssignment operatoroperator ►Consider a string class:Consider a string class: class String{class String{ int size;int size; char * bufferPtr;char * bufferPtr; public:public: String();String(); String(char *);String(char *); String(const String &);String(const String &); …… };};
    • AssignmentAssignment operatoroperator String::String(char * ptr){String::String(char * ptr){ if(ptr != NULL){if(ptr != NULL){ size = strlen(ptr);size = strlen(ptr); bufferPtr = new char[size+1];bufferPtr = new char[size+1]; strcpy(bufferPtr, ptr);strcpy(bufferPtr, ptr); }} else{else{ bufferPtr = NULL; size = 0; }bufferPtr = NULL; size = 0; } }}
    • AssignmentAssignment operatoroperator String::String(const String & rhs){String::String(const String & rhs){ size = rhs.size;size = rhs.size; if(rhs.size != 0){if(rhs.size != 0){ bufferPtr = new char[size+1];bufferPtr = new char[size+1]; strcpy(bufferPtr, ptr);strcpy(bufferPtr, ptr); }} elseelse bufferPtr = NULL;bufferPtr = NULL; }}
    • AssignmentAssignment operatoroperator int main(){int main(){ String str1(“Hello");String str1(“Hello"); String str2(“World”);String str2(“World”); str1 = str2;str1 = str2; return 0;return 0; }} Member wise copy assignment
    • AssignmentAssignment operatoroperator ►Result ofResult of str1 = str2str1 = str2 (memory leak)(memory leak) str1 Hello str2 World
    • AssignmentAssignment operatoroperator ►Modifying:Modifying: class String{class String{ …… public:public: …… void operator =(const String &);void operator =(const String &); };};
    • AssignmentAssignment operatoroperator void String::operator = (const String & rhs){void String::operator = (const String & rhs){ size = rhs.size;size = rhs.size; if(rhs.size != 0){if(rhs.size != 0){ delete [] bufferPtr;delete [] bufferPtr; bufferPtr = new char[rhs.size+1];bufferPtr = new char[rhs.size+1]; strcpy(bufferPtr,rhs.bufferPtr);strcpy(bufferPtr,rhs.bufferPtr); }} elseelse bufferPtr = NULL;bufferPtr = NULL; }}
    • AssignmentAssignment operatoroperator int main(){int main(){ String str1(“ABC");String str1(“ABC"); String str2(“DE”), str3(“FG”);String str2(“DE”), str3(“FG”); str1 = str2;str1 = str2; // Valid…// Valid… str1 = str2 = str3;str1 = str2 = str3; // Error…// Error… return 0;return 0; }}
    • AssignmentAssignment operatoroperator ►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. Parameter can’t be void