0% found this document useful (0 votes)
109 views22 pages

Object Oriented Programming: Lecture 8: Operator Overloading

This document discusses operator overloading in C++. It explains how overloading operators like +, -, = allows objects to be used in arithmetic expressions and assignments in a natural way. It provides examples of overloading binary operators to add two distance objects, overloading = to assign one ratio to another, and overloading += to perform addition assignment. It also discusses returning by reference, void return types, and overloading comparison operators.

Uploaded by

khawar abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views22 pages

Object Oriented Programming: Lecture 8: Operator Overloading

This document discusses operator overloading in C++. It explains how overloading operators like +, -, = allows objects to be used in arithmetic expressions and assignments in a natural way. It provides examples of overloading binary operators to add two distance objects, overloading = to assign one ratio to another, and overloading += to perform addition assignment. It also discusses returning by reference, void return types, and overloading comparison operators.

Uploaded by

khawar abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Object oriented Programming

Lecture 8: Operator overloading

Object oriented programming in C++ by Robert Lafore 1


Recap..
• Revising Copy constructor (const and by reference arguments)
• Passing objects by value and by reference
• Operator overloading: Unary operators
• Nameless temporary objects and parametrized constructor
• Prefix and postfix operator overloading

Object oriented programming in C++ by Robert Lafore 2


Overloading Binary Operators
dist3.add_dist(dist1, dist2);
• By overloading the + operator we can reduce this dense-looking expression to
dist3 = dist1 + dist2;
OR
dist3 = dist1.operator+(dist2);

Object oriented programming in C++ by Robert Lafore 3


Without operator overloading

Ratio() { num = 1; den = 1; }


Ratio(int n,int d) { num = n; den = d; } Main():
Ratio addRatio(Ratio r1, Ratio r2)
{ Ratio r1,r2;
cout << "+ operator function called" << endl; Ratio r3 = r1.addRatio(r2);
return Ratio(num+r2.num,den+r2.den); r3.Display();
} return 0;

void Display()
{
cout << "numerator: " << num<<endl;
cout << "denominator: " << den<<endl;
}

Object oriented programming in C++ by Robert Lafore 4


With operator overloading

Ratio() { num = 1; den = 1; }


Ratio(int n,int d) { num = n; den = d; } Main():
Ratio operator +(Ratio r2)
{ Ratio r1,r2;
cout << "+ operator function called" << endl; Ratio r3 = r1+r2;
return Ratio(num+r2.num,den+r2.den); r3.Display();
} return 0;

void Display()
{
cout << "numerator: " << num<<endl;
cout << "denominator: " << den<<endl;
}

Object oriented programming in C++ by Robert Lafore 5


Void return type
void operator +(Ratio r2)
{
cout <<" Operator + called " << endl;
cout <<num << " and "<<r2.num<<" getting added
"<<endl;
//return Ratio(num+r2.num,den+r2.den);
}

int main()
{
Ratio r1(1,1),r2(5,6),r3(2,2);
r1 + r2;
r2.Display();
return 0;
}

Object oriented programming in C++ by Robert Lafore 6


Non-void return type and effect of
pass by value
int main()
{
Ratio r1(1,1),r2(5,6),r3(2,2);

cout << "Values of objects before call" << endl;


cout << "Object 1" << endl;
r1.Display();
cout << "Object 2" << endl;
Ratio operator -(Ratio r2)
r2.Display();
{
cout << "Object 3" << endl;
return Ratio(num - r2.num, den - (r2.den++));
r3.Display();
}
r3 = r1 - r2;

cout << "Values of objects before call" << endl;


cout << "Object 1" << endl;
r1.Display();
cout << "Object 2" << endl;
r2.Display();
cout << "Object 3" << endl;
r3.Display();

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);

cout << "Values of objects before call" << endl;


cout << "Object 1" << endl;
r1.Display();
cout << "Object 2" << endl;
Ratio operator -(Ratio &r2)
r2.Display();
{
cout << "Object 3" << endl;
return Ratio(num - r2.num, den - (r2.den++));
r3.Display();
}
r3 = r1 - r2;

cout << "Values of objects before call" << endl;


cout << "Object 1" << endl;
r1.Display();
cout << "Object 2" << endl;
r2.Display();
cout << "Object 3" << endl;
r3.Display();

return 0;
}
Object oriented programming in C++ by Robert Lafore 8
Multiple calls in a single line
Ratio operator +(Ratio r2)
{

cout <<num << " and "<<r2.num<<" getting added "<<endl;


return Ratio(num+r2.num,den+r2.den);
}

int main()
{
Ratio r1(1,1),r2(5,6),r3(2,2);
Ratio r4 = r1+r2+r3;
r4.Display();
return 0;
}

Object oriented programming in C++ by Robert Lafore 9


Use of Operator + and operator – in one call
Ratio operator +(Ratio r2) Ratio operator -(Ratio r2)
{ {
cout <<" Operator + called " << endl; cout << " Operator - called " << endl;
cout <<num << " and "<<r2.num<<" getting cout << num << " and " << r2.num << " getting
added "<<endl; subtracted " << endl;
return Ratio(num+r2.num,den+r2.den); return Ratio(num - r2.num, den - r2.den);
} }

int main()
{
Ratio r1(1,1),r2(5,6),r3(2,2);
Ratio r4 = r1+r2-r3;
r4.Display();
return 0;
}

Object oriented programming in C++ by Robert Lafore 10


Assignment operator = (void) void operator =(Ratio r)
{
cout << "Operator = function called" << endl;
cout << "for object with numerator: " << r.num <<
int main() endl;
{ num = r.num;
Ratio r1(1,1),r2(5,6),r3(2,2); den = r.den;
cout << "Values of objects before call" << endl; }
cout << "Object 1" << endl;
r1.Display();
cout << "Object 2" << endl;
r2.Display();
cout << "Object 3" << endl;
r3.Display();

r1 = r2;

cout << "Values of objects before call" << endl;


cout << "Object 1" << endl;
r1.Display();
cout << "Object 2" << endl;
r2.Display();
cout << "Object 3" << endl;
r3.Display();

return 0;
}

Object oriented programming in C++ by Robert Lafore 11


Assignment operator = (with return type)
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();
den = r.den;
cout << "Object 3" << endl;
r3.Display(); return *this;
}
r1 = r2 = r3;

cout << "Values of objects before call" <<


endl;
cout << "Object 1" << endl;
r1.Display();
cout << "Object 2" << endl;
r2.Display();
cout << "Object 3" << endl;
r3.Display();

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;

cout << "Values of objects before call" <<


endl;
cout << "Object 1" << endl;
r1.Display();
cout << "Object 2" << endl;
r2.Display();
cout << "Object 3" << endl;
r3.Display();

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;

cout << "Values of objects before call" << endl;


cout << "Object 1" << endl;
r1.Display();
cout << "Object 2" << endl;
r2.Display();
cout << "Object 3" << endl;
r3.Display();

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;

cout << "Values of objects before call" <<


endl;
cout << "Object 1" << endl;
r1.Display();
cout << "Object 2" << endl;
r2.Display();
cout << "Object 3" << endl;
r3.Display();

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

Object oriented programming in C++ by Robert Lafore 17


Following is the list of operators, which can not be overloaded

Object oriented programming in C++ by Robert Lafore 18


>>, <<: Overloading the Binary Stream Insertion and
Stream Extraction Operators
• Note: cout is an object of ostream class and cin is an object istream class
• We will revisit this by the next couple of lectures.

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.

Object oriented programming in C++ by Robert Lafore 20


int main()
Ratio operator()(int a, int b, int c) {
{
cout << "() operator called" << endl;
Ratio r(0,0),r2(0,0);
num = a+b+c;
r2=r(1, 2, 3);
den = a+b+c;
r.Display();
return *this;
r2.Display();
}
return 0;
}

Ratio operator =(Ratio r)


{
cout << "operator = called" << endl;
num = r.num;
den = r.den;
return *this;
}

Object oriented programming in C++ by Robert Lafore 21


That’s it

Object oriented programming in C++ by Robert Lafore 22

You might also like