Operator overloading (oop) lecture18

271 views
211 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
271
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Operator overloading (oop) lecture18

  1. 1. Object-Oriented ProgrammingObject-Oriented Programming (OOP)(OOP) Lecture No. 18Lecture No. 18
  2. 2. AssignmentAssignment operatoroperator ►Modifying:Modifying: class String{class String{ …… public:public: …… void operator =(const String &);void operator =(const String &); };};
  3. 3. AssignmentAssignment operatoroperator void String::operator = (const String & rhs){void String::operator = (const String & rhs){ size = rhs.size;size = rhs.size; delete [] bufferPtr;delete [] bufferPtr; if(rhs.size != 0){if(rhs.size != 0){ 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; }}
  4. 4. 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; }}
  5. 5. 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
  6. 6. AssignmentAssignment operatoroperator►Solution: modify theSolution: modify the operatoroperator == functionfunction as follows:as follows: class String{class String{ …… public:public: …… String & operator = (constString & operator = (const String &);String &); };};
  7. 7. AssignmentAssignment operatoroperator String & String :: operator = (const String &String & String :: operator = (const String & rhs){rhs){ size = rhs.size;size = rhs.size; delete [] bufferPtr;delete [] bufferPtr; if(rhs.size != 0){if(rhs.size != 0){ bufferPtr = new char[rhs.size+1];bufferPtr = new char[rhs.size+1]; strcpy(bufferPtr,rhs.bufferPtr);strcpy(bufferPtr,rhs.bufferPtr); }} else bufferPtr = NULL;else bufferPtr = NULL; return *this;return *this; }}
  8. 8. AssignmentAssignment operatoroperator void main(){void main(){ String str1(“AB");String str1(“AB"); String str2(“CD”), str3(“EF”);String str2(“CD”), str3(“EF”); str1 = str2;str1 = str2; str1 = str2 = str3;str1 = str2 = str3; // Now valid…// Now valid… }}
  9. 9. AssignmentAssignment operatoroperator Return type is String . ►str1=str2=str3str1=str2=str3 is resolved as:is resolved as: str1.operator=(str1.operator=(str2.operator=str2.operator= (str3)(str3)))
  10. 10. AssignmentAssignment operatoroperator int main(){int main(){ String str1("Fakhir");String str1("Fakhir"); // Self Assignment problem…// Self Assignment problem… str1 = str1;str1 = str1; return 0;return 0; }}
  11. 11. … // size = rhs.size; // delete [] bufferPtr; … ??? AssignmentAssignment operatoroperator ►Result ofResult of str1 = str1str1 = str1 str1 Fakhir
  12. 12. AssignmentAssignment operatoroperator String & String :: operator = (constString & String :: operator = (const String & rhs){String & rhs){ if(this != &rhs){if(this != &rhs){ size = rhs.size;size = rhs.size; delete [] bufferPtr;delete [] bufferPtr; if(rhs.bufferPtr != NULL){if(rhs.bufferPtr != NULL){ bufferPtr = new char[rhs.size+1];bufferPtr = new char[rhs.size+1]; strcpy(bufferPtr,rhs.bufferPtr);strcpy(bufferPtr,rhs.bufferPtr); }} else bufferPtr = NULL;else bufferPtr = NULL; }} return *this; }return *this; }
  13. 13. AssignmentAssignment operatoroperator ►Now self-assignment is properly handled:Now self-assignment is properly handled: int main(){int main(){ String str1("Fakhir");String str1("Fakhir"); str1 = str1;str1 = str1; return 0;return 0; }}
  14. 14. AssignmentAssignment operatoroperator►Solution: modify theSolution: modify the operator=operator= functionfunction as follows:as follows: class String{class String{ …… public:public: …… constconst String & operator=String & operator= (const String &);(const String &); };};
  15. 15. AssignmentAssignment operatoroperator int main(){int main(){ String s1(“ABC”),String s1(“ABC”), s2(“DEF”),s2(“DEF”), s3(“GHI”);s3(“GHI”); // Error…// Error… (s1 = s2) = s3;(s1 = s2) = s3; return 0;return 0; }}
  16. 16. AssignmentAssignment operatoroperator But we can do the following withBut we can do the following with primitive types:primitive types: int main(){int main(){ int a, b, c;int a, b, c; (a = b) = c;(a = b) = c; return 0;return 0; }}
  17. 17. Other BinaryOther Binary operatorsoperators►OverloadingOverloading +=+= operator:operator: class Complex{class Complex{ double real, img;double real, img; public:public: Complex & operator+=(const Complex &Complex & operator+=(const Complex & rhs);rhs); Complex & operator+=(count double &Complex & operator+=(count double & rhs);rhs); ...... };};
  18. 18. Other BinaryOther Binary operatorsoperators Complex & Complex::operator +=Complex & Complex::operator += (const Complex & rhs){(const Complex & rhs){ real = real + rhs.real;real = real + rhs.real; img = img + rhs.img;img = img + rhs.img; return * this;return * this; }}
  19. 19. Other BinaryOther Binary operatorsoperators Complex & Complex::operator +=Complex & Complex::operator += (const double & rhs){(const double & rhs){ real = real + rhs;real = real + rhs; return * this;return * this; }}
  20. 20. Other BinaryOther Binary operatorsoperators int main(){int main(){ Complex c1, c2, c3;Complex c1, c2, c3; c1 += c2;c1 += c2; c3 += 0.087;c3 += 0.087; return 0;return 0; }}
  21. 21. OperatorOperator overloadingoverloading ►Friend functions minimizeFriend functions minimize encapsulationencapsulation ►This can result in:This can result in: Data vulnerabilityData vulnerability Programming bugsProgramming bugs Tough debuggingTough debugging ►Hence, use of friend functions mustHence, use of friend functions must be limitedbe limited
  22. 22. OperatorOperator overloadingoverloading ►The + operator can be defined as aThe + operator can be defined as a non-member, non-friend function:non-member, non-friend function: Complex operator + (const Complex &Complex operator + (const Complex & a, const Complex & b){a, const Complex & b){ Complex t = a;Complex t = a; return t += breturn t += b }}
  23. 23. OperatorOperator overloadingoverloading Complex operator + (const double &Complex operator + (const double & a, const Complex & b){a, const Complex & b){ Complex t = b;Complex t = b; return t += a;return t += a; }}
  24. 24. OperatorOperator overloadingoverloading Complex operator + (const Complex &Complex operator + (const Complex & a, const double & b){a, const double & b){ Complex t = a;Complex t = a; return t += b;return t += b; }}
  25. 25. Other BinaryOther Binary operatorsoperators The operatorsThe operators -=, /=, *=, |=, %=, &=, ^=,-=, /=, *=, |=, %=, &=, ^=, <<=, >>=, !=<<=, >>=, != can be overloaded in a verycan be overloaded in a very similar fashionsimilar fashion

×