8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
1/25
Object-Oriented Programming
(OOP)Lecture No. 18
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
2/25
ssignment operatorModifying:
class String{
public:
void operator =(const String &);
};
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
3/25
ssignment operatorvoid 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;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
4/25
ssignment operatorint main(){
String str1(ABC");
String str2(DE), str3(FG);
str1 = str2; // Valid
str1 = str2 = str3; // Errorreturn 0;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
5/25
ssignment operatorstr1=str2=str3is resolved as:
str1.operator=(str2.operator=
(str3))
Return type isvoid. Parameter
cant be void
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
6/25
ssignment operatorSolution: modify the operator=function as follows:
class String{
public:
String & operator = (constString &);
};
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
7/25
ssignment operatorString & 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;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
8/25
ssignment operatorvoid main(){
String str1(AB");
String str2(CD), str3(EF);
str1 = str2;
str1 = str2 = str3; // Now valid
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
9/25
ssignment operator
Return type isString .
str1=str2=str3is resolved as:
str1.operator=(str2.operator=
(str3))
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
10/25
ssignment operator
int main(){String str1("Fakhir");
// Self Assignment problem
str1 = str1;
return 0;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
11/25
// size = rhs.size;
// delete [] bufferPtr;
???
ssignment operatorResult of str1 = str1
str1
Fakhir
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
12/25
ssignment operatorString & 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; }
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
13/25
ssignment operatorNow self-assignment is properly handled:
int main(){
String str1("Fakhir");
str1 = str1;return 0;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
14/25
ssignment operatorSolution: modify the operator=function as follows:
class String{
public:
constString & operator=
(const String &);
};
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
15/25
ssignment operatorint main(){
String s1(ABC),
s2(DEF),
s3(GHI);
// Error
(s1 = s2) = s3;return 0;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
16/25
ssignment operatorBut we can do the following withprimitive types:
int main(){
int a, b, c;
(a = b) = c;
return 0;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
17/25
Other Binary operatorsOverloading +=operator:
class Complex{
double real, img;
public:
Complex & operator+=(const Complex &rhs);
Complex & operator+=(count double &
rhs);
...
};
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
18/25
Other Binary operatorsComplex & Complex::operator +=
(const Complex & rhs){
real = real + rhs.real;
img = img + rhs.img;
return * this;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
19/25
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
20/25
Other Binary operatorsint main(){
Complex c1, c2, c3;
c1 += c2;
c3 += 0.087;
return 0;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
21/25
Operator overloadingFriend functions minimizeencapsulation
This can result in:Data vulnerability
Programming bugs
Tough debugging
Hence, use of friend functions mustbe limited
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
22/25
Operator overloadingThe + operator can be defined as anon-member, non-friend function:
Complex operator + (const Complex &
a, const Complex & b){
Complex t = a;
return t += b
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
23/25
Operator overloadingComplex operator + (const double &
a, const Complex & b){Complex t = b;
return t += a;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
24/25
Operator overloadingComplex operator + (const Complex &
a, const double & b){Complex t = a;
return t += b;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 18
25/25
Other Binary operatorsThe operators
-=, /=, *=, |=, %=, &=, ^=,
=, !=
can be overloaded in a very
similar fashion