PPT8 - Operator Overleading
PPT8 - Operator Overleading
Poly-morphism
(Operator Overloading)
1
Introduction
• Operator overloading
Enabling C++’s operators to work with class objects
Using traditional operators with user-defined objects
Examples of already overloaded operators
• Operator << is both the stream-insertion operator and the
bitwise left-shift operator
• Operator >> is both the stream-extraction operator and the
bitwise right-shift operator
Introduction (Cont…)
• Overloading an operator
– Write function definition as normal
– Functionname is keywordoperator followed bythe symbol for the
operator being overloaded
– For e.g., operator+ used to overload the addition operator (+)
Note: If left operand is of a different type than class, operator function must be a non-member function
Creating a Member Operator Function
• Given an object called obj, the expression obj[3] translates into this
call to the operator[ ]( ) function: obj.operator[](3)
Example 1
#include <iostream> int operator[](int i)
using namespace std; { return a[i];
class atype { }
int a[3]; };
public: int main()
atype(int i, int j, int k) { {
a[0] = i; atype ob(1, 2, 3);
a[1] = j; cout << ob[1]; // displays 2
a[2] = k; return 0;
} } Output:
2
Example 2
#include <iostream> int main()
using namespace std; {
class atype { atype ob(1, 2, 3);
int a[3]; cout << ob[1]; // displays 2
public: cout << " ";
atype(int i, int j, int k) { ob[1] = 25; // [] on left of =
a[0] = i; cout << ob[1]; // now displays 25
a[1] = j; return 0;
a[2] = k; }
}
Output:
int &operator[](int i) {
return a[i]; 2 25
}
};
Overloading ()
• When we overload the ( ) function call operator, we are
not creating a new way to call a function.
Example:
double operator()(int a, float f, char *s);
and an object O of its class, then the statement
O(10, 23.34, "hi");
translates into this call to the operator( ) function.
O.operator()(10, 23.34, "hi");
Example
#include <iostream> // Overload + for loc
using namespace std; loc loc::operator+(loc op2)
class loc { {
int longitude, latitude; loc temp;
public: temp.longitude = op2.longitude + longitude;
loc() {} temp.latitude = op2.latitude + latitude;
loc(int lg, int lt) return temp;
{ }
longitude = lg; latitude = lt;
}
void show() { int main()
cout << longitude << " "; {
cout << latitude << "\n"; loc ob1(10, 20), ob2(1, 1);
} ob1.show();
loc operator+(loc op2); loc operator()(int i, int j); ob1(7, 8); // can be executed by itself
}; ob1.show();
// Overload ( ) for loc ob1 = ob2 + ob1(10, 10); // can be used in expressions
loc loc::operator()(int i, int j) ob1.show();
{ return 0;
longitude = i; latitude = j; }
return *this;
}
Overloading the Comma Operator
#include <iostream> cout << op2.longitude << " " << op2.latitude << "\n";
return temp;
using namespace std;
}
class loc {
// Overload + for loc
int longitude, latitude;
loc loc::operator+(loc op2)
public:
{
loc() {}
loc temp;
loc(int lg, int lt) { longitude = lg; latitude = lt;
temp.longitude = op2.longitude + longitude;
}
temp.latitude = op2.latitude + latitude; return temp;
void show() {
cout << longitude << " "; cout << latitude << "\n"; }
} int main()
loc operator+(loc op2); loc operator,(loc op2); {
loc ob1(10, 20), ob2( 5, 30), ob3(1, 1);
};
ob1.show();
// overload comma for loc
ob2.show();
loc loc::operator,(loc op2)
ob3.show(); cout << "\n";
{
loc temp; ob1 = (ob1, ob2+ob2, ob3);
temp.longitude = op2.longitude; temp.latitude = ob1.show(); // displays 1 1, the value of ob3
op2.latitude; return 0;
}
Overloading ->
• The –> pointer operator, also called the class member access operator,
is considered a unary operator when overloading.