Operator overloading (oop)lecture2
-
Upload
university-of-central-punjab -
Category
Education
-
view
154 -
download
0
Transcript of Operator overloading (oop)lecture2
Object-Oriented Object-Oriented Programming (OOP)Programming (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 +Complex operator +(const (const Complex & Complex & rhs);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 The return type is Complex so as to facilitate complex as to facilitate complex statements like:statements like:
Complex t = c1 + c2 + c3;Complex t = c1 + c2 + c3;
►The above statement is The above statement is automatically converted by the automatically converted by the compiler into appropriate compiler into appropriate function calls:function calls:
((c1.operator +(c2)c1.operator +(c2))).operator +.operator +(c3);(c3);
Binary operatorsBinary operators
►The binary operator is always The binary operator is always called with reference to the left called with reference to the left hand argumenthand argument
►Example:Example:
In In c1+c2c1+c2, , c1.operator+(c2)c1.operator+(c2)
In In c2+c1c2+c1, , c2.operator+(c1)c2.operator+(c1)
Binary operatorsBinary operators
►The above examples don’t The above examples don’t handle the following handle the following situation:situation:
Complex c1;Complex c1;
c1 + 2.325c1 + 2.325
►To do this, we have to To do this, we have to modify themodify the Complex Complex class class
Binary operatorsBinary operators►Modifying the complex Modifying the complex class:class:
class Complex{class Complex{
......
Complex operator+(const Complex operator+(const Complex & rhs);Complex & rhs);
Complex operator+(const Complex 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 But problem arises if we do the following:following:
Complex c5 = Complex c5 = 450.120450.120 + c1; + c1;
►The + operator is called with The + operator is called with reference to reference to 450.120450.120
►No predefined overloaded + No predefined overloaded + operator is there that takes operator is there that takes ComplexComplex as an argumentas an argument
Binary operatorsBinary operators►Now if we write the following two Now if we write the following two functions to the class, we can add a functions to the class, we can add a ComplexComplex to a to a realreal or vice versa: or vice versa:
Class Complex{Class Complex{
……
friend Complex operator + (const friend Complex operator + (const Complex & lhs, const double & Complex & lhs, const double & rhsrhs););
friend Complex operator + (const friend 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 operatorsClass Complex{Class Complex{
……
Complex operator + (const Complex operator + (const Complex Complex
&);&);
friend Complex operator + (const friend Complex operator + (const Complex &, const double &);Complex &, const double &);
friend Complex operator + (const friend Complex operator + (const double &, const Complex &);double &, const Complex &);
};};
Binary operatorsBinary operators►Other binary operators are Other binary operators are overloaded very similar to the + overloaded very similar to the + operator as demonstrated in the operator as demonstrated in the above examplesabove examples
►Example:Example:Complex operator Complex operator ** (const Complex & (const Complex &
c1, const Complex & c2);c1, const Complex & c2);
Complex operator Complex operator // (const Complex & (const Complex & c1, const Complex & c2);c1, const Complex & c2);
Complex operator Complex operator -- (const Complex & (const Complex & c1, const Complex & c2);c1, const Complex & c2);
Assignment operatorAssignment operator►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 &);
……
};};
Assignment operatorAssignment operator
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; }
}}
Assignment operatorAssignment operator
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;
}}
Assignment operatorAssignment operator
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
Assignment operatorAssignment operator
►Result of Result of str1 = str2str1 = str2 (memory (memory leak)leak)
str1
Hello
str2
World
Assignment operatorAssignment operator
►Modifying:Modifying:
class String{class String{
……
public:public:
……
void operator =(const String void operator =(const String &);&);
};};
Assignment operatorAssignment operator
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;
}}
Assignment operatorAssignment operator
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;
}}
Assignment operatorAssignment operator
►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