Operator overloading (oop)lecture2

201
-1

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
201
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Operator overloading (oop)lecture2

  1. 1. Object-Oriented ProgrammingObject-Oriented Programming (OOP)(OOP) Lecture No. 17Lecture No. 17
  2. 2. 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); };};
  3. 3. 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; }}
  4. 4. 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);
  5. 5. 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)
  6. 6. 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
  7. 7. 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); };};
  8. 8. 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; }}
  9. 9. 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;
  10. 10. 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
  11. 11. 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); }}
  12. 12. 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; }}
  13. 13. 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; }}
  14. 14. 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 &); };};
  15. 15. 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);
  16. 16. 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 &); …… };};
  17. 17. 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; } }}
  18. 18. 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; }}
  19. 19. 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
  20. 20. AssignmentAssignment operatoroperator ►Result ofResult of str1 = str2str1 = str2 (memory leak)(memory leak) str1 Hello str2 World
  21. 21. AssignmentAssignment operatoroperator ►Modifying:Modifying: class String{class String{ …… public:public: …… void operator =(const String &);void operator =(const String &); };};
  22. 22. 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; }}
  23. 23. 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; }}
  24. 24. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×