Object Oriented Programming: Lecture 8: Operator Overloading
Object Oriented Programming: Lecture 8: Operator Overloading
void Display()
{
cout << "numerator: " << num<<endl;
cout << "denominator: " << den<<endl;
}
void Display()
{
cout << "numerator: " << num<<endl;
cout << "denominator: " << den<<endl;
}
int main()
{
Ratio r1(1,1),r2(5,6),r3(2,2);
r1 + r2;
r2.Display();
return 0;
}
return 0;
}
Object oriented programming in C++ by Robert Lafore 7
Non-void return type and effect of
pass by reference
int main()
{
Ratio r1(1,1),r2(5,6),r3(2,2);
return 0;
}
Object oriented programming in C++ by Robert Lafore 8
Multiple calls in a single line
Ratio operator +(Ratio r2)
{
int main()
{
Ratio r1(1,1),r2(5,6),r3(2,2);
Ratio r4 = r1+r2+r3;
r4.Display();
return 0;
}
int main()
{
Ratio r1(1,1),r2(5,6),r3(2,2);
Ratio r4 = r1+r2-r3;
r4.Display();
return 0;
}
r1 = r2;
return 0;
}
return 0;
}
Object oriented programming in C++ by Robert Lafore 12
Assignment operator = (with return type) Pass/return by reference
int main()
{
Ratio r1(1,1),r2(5,6),r3(2,2);
cout << "Values of objects before call" <<
endl;
cout << "Object 1" << endl; Ratio& operator =(Ratio &r)
r1.Display(); {
cout << "Object 2" << endl;
num = r.num;
r2.Display();
cout << "Object 3" << endl; den = r.den;
r3.Display(); return *this;
}
r1 = r2 = r3;
return 0;
}
Object oriented programming in C++ by Robert Lafore 13
Arithmetic Assignment Operators dist1 += dist2; //dist1 = dist1 + dist2
int main()
{
void operator +=(Ratio r) Ratio r1(1,1),r2(5,6),r3(2,2);
{ cout << "Values of objects before call" << endl;
num += r.num; cout << "Object 1" << endl;
den += r.den; r1.Display();
cout << "Object 2" << endl;
}
r2.Display();
cout << "Object 3" << endl;
r3.Display();
r1 += r2;
return 0;
}
Object oriented programming in C++ by Robert Lafore 14
int main()
{
Comparison Operators Ratio r1(1,1),r2(5,6),r3(2,2);
cout << "Values of objects before call" <<
endl;
cout << "Object 1" << endl;
bool operator >(Ratio r) r1.Display();
cout << "Object 2" << endl;
{ r2.Display();
if (num > r.num) cout << "Object 3" << endl;
return true; r3.Display();
else
if (r1 > r2)
return false; cout << "R1 is greater" << endl;
} else
cout << "R2 is greater" << endl;
return 0;
}
Object oriented programming in C++ by Robert Lafore 15
Overloading Subscript or array index operator []
• Overloading of [] may be useful when we want to check for index out of bound
class Array
Array(int* p = NULL, int s = 0) int main()
{
{ {
private:
size = s; int a[] = { 1, 2, 4, 5 };
int* ptr;
ptr = NULL; Array arr1(a, 4);
int size;
if (s != 0) arr1[2] = 6;
public:
{ arr1.print();
int& operator[](int index)
ptr = new int[s]; arr1[8] = 6;
{
for (int i = 0; i < s; i++) return 0;
if (index >= size)
ptr[i] = p[i]; }
{
}
cout << "Array index out of bound,
}
exiting";
void print() const
exit(0);
{
}
for (int i = 0; i < size; i++)
return ptr[index];
cout << ptr[i] << " ";
}
cout << endl;
}
};
Object oriented programming in C++ by Robert Lafore 16
Following is the list of operators which can be overloaded
19
()
• The function call operator () can be overloaded for objects of class type.
• When you overload ( ), you are not creating a new way to call a function.
• Rather, you are creating an operator function that can be passed an arbitrary
number of parameters.