Computer Notes - Assignment operator

0

No comments posted yet

Comments

Slide 1

Assignment operator http://ecomputernotes.com

Slide 2

Assignment operator Modifying: class String{ … public: … void operator =(const String &); }; http://ecomputernotes.com

Slide 3

Assignment operator void String::operator = (const String & rhs){ size = rhs.size; delete [] bufferPtr; if(rhs.size != 0){ bufferPtr = new char[rhs.size+1]; strcpy(bufferPtr,rhs.bufferPtr); } else bufferPtr = NULL; } http://ecomputernotes.com

Slide 4

Assignment operator int main(){ String str1(“ABC"); String str2(“DE”), str3(“FG”); str1 = str2; // Valid… str1 = str2 = str3; // Error… return 0; } http://ecomputernotes.com

Slide 5

Assignment operator str1=str2=str3 is resolved as: str1.operator=(str2.operator= (str3)) Return type is void. Parameter can’t be void http://ecomputernotes.com

Slide 6

Assignment operator Solution: modify the operator = function as follows: class String{ … public: … String & operator = (const String &); }; http://ecomputernotes.com

Slide 7

Assignment operator String & String :: operator = (const String & rhs){ size = rhs.size; delete [] bufferPtr; if(rhs.size != 0){ bufferPtr = new char[rhs.size+1]; strcpy(bufferPtr,rhs.bufferPtr); } else bufferPtr = NULL; return *this; } http://ecomputernotes.com

Slide 8

Assignment operator void main(){ String str1(“AB"); String str2(“CD”), str3(“EF”); str1 = str2; str1 = str2 = str3; // Now valid… } http://ecomputernotes.com

Slide 9

Assignment operator Return type is String . str1=str2=str3 is resolved as: str1.operator=(str2.operator= (str3)) http://ecomputernotes.com

Slide 10

Assignment operator int main(){ String str1("Fakhir"); // Self Assignment problem… str1 = str1; return 0; } http://ecomputernotes.com

Slide 11

… // size = rhs.size; // delete [] bufferPtr; … ??? Assignment operator Result of str1 = str1 str1 Fakhir http://ecomputernotes.com

Slide 12

Assignment operator String & String :: operator = (const String & rhs){ if(this != &rhs){ size = rhs.size; delete [] bufferPtr; if(rhs.bufferPtr != NULL){ bufferPtr = new char[rhs.size+1]; strcpy(bufferPtr,rhs.bufferPtr); } else bufferPtr = NULL; } return *this; }

Slide 13

Assignment operator Now self-assignment is properly handled: int main(){ String str1("Fakhir"); str1 = str1; return 0; } http://ecomputernotes.com

Slide 14

Assignment operator Solution: modify the operator= function as follows: class String{ … public: … const String & operator= (const String &); }; http://ecomputernotes.com

Slide 15

Assignment operator int main(){ String s1(“ABC”), s2(“DEF”), s3(“GHI”); // Error… (s1 = s2) = s3; return 0; } http://ecomputernotes.com

Slide 16

Assignment operator But we can do the following with primitive types: int main(){ int a, b, c; (a = b) = c; return 0; } http://ecomputernotes.com

Slide 17

Other Binary operators Overloading += operator: class Complex{ double real, img; public: Complex & operator+=(const Complex & rhs); Complex & operator+=(count double & rhs); ... }; http://ecomputernotes.com

Slide 18

Other Binary operators Complex & Complex::operator += (const Complex & rhs){ real = real + rhs.real; img = img + rhs.img; return * this; } http://ecomputernotes.com

Slide 19

Other Binary operators Complex & Complex::operator += (const double & rhs){ real = real + rhs; return * this; } http://ecomputernotes.com

Slide 20

Other Binary operators int main(){ Complex c1, c2, c3; c1 += c2; c3 += 0.087; return 0; } http://ecomputernotes.com

Slide 21

Operator overloading Friend functions minimize encapsulation This can result in: Data vulnerability Programming bugs Tough debugging Hence, use of friend functions must be limited

Slide 22

Operator overloading The + operator can be defined as a non-member, non-friend function: Complex operator + (const Complex & a, const Complex & b){ Complex t = a; return t += b } http://ecomputernotes.com

Slide 23

Operator overloading Complex operator + (const double & a, const Complex & b){ Complex t = b; return t += a; } http://ecomputernotes.com

Slide 24

Operator overloading Complex operator + (const Complex & a, const double & b){ Complex t = a; return t += b; } http://ecomputernotes.com

Slide 25

Other Binary operators The operators -=, /=, *=, |=, %=, &=, ^=, <<=, >>=, != can be overloaded in a very similar fashion http://ecomputernotes.com

Summary: http://ecomputernotes.com - Computer Notes - Assignment operator in Object oriented Programming what is Assignment operator OOPs Explain about it in detail .explain it with example

Tags: computer notes assignment operator

URL:
More by this User
Most Viewed