You are on page 1of 45

Operator Overloading

1
Operator Overloading
 The process of defining additional meanings of operators is known
as operator overloading.
 It enable an operator to preform different operations depending on
the type of operands. It also enables the operators to process the
user-defined data types.
 The basic arithmetic operators such as +, -, * and / normally work
with the basic types such as int, float and long etc.
 The applications of these operators with basic types are already
defined in the language. However, an error will occur if these
operators are used with user-defined objects.

2
Overloading an Operator
 An operator can be overloaded by declaring a special member
function in the class. The member function uses the keyword
operator with the symbol of operator to be overloaded.

3
Overloading an Operator
Syntax:
The syntax of overloading an operator is as follows:
return_type operator op()
{
function body;
}

return_type: It indicates the type of value returned by the member


function.
operator: It IS the keyword that indicates that the member function
is used to overload an operator.
op: It is the symbol of operator to be overloaded.

4
Overloading an Operator
Example:
void operator ++()

function body;

5
Overloading Unary Operators
 A type of operator that works with single operand is called unary
operator.
 The unary operators are overloaded to increase their capabilities.

 The unary operator that can be overloaded in C++ are as follows:

+ - * ! ~ &
++ -- () -> new delete

6
Overloading ++ Operator
 The increment operator ++ is a unary operator. It works with
single operand. It increases the value of operand by 1. it only
works with numeric values by default.
 It can be overloaded to enable it to increase the values of data
members of an object in the same way.

7
Example Program 1
Write a program that overloads increment operator to work with user-
defined objects.

#include <iostream>
using namespace std; int main () {
Count obj;
class Count { obj.show();
private: ++obj;
int n; obj.show();
public: return 0;
Count () }
{
n=0;
}
void show ()
{
cout<<"n= "<<n<<endl;
}
void operator ++()
{ n=n+1; } };
8
9
10
How above program Works
 The above program overloads the increment operator ++ to work
with the objects of all user-defined class Count. It increases the
value of data member n by 1.
 The user can use the same format to increase the value of object
as used with the integers. The above overloading only works in
prefix notation.

11
Operator Overloading with Returned
Value
 The increment operator can be used in assignment statement to
store the incremented value in another variable.
 Suppose a and b are two integers. The following statement will
increment the value of a by 1 and then assign the new value to b:

b=++a;
 The above statement can be assigned to the operator by returning
the new value from the member function. The returned value can
be stored in another object of the same type on the left side of the
assignment operator.

12
Example Program 2
Write a program that overloads increment operator to work with the user-defined
objects. The overloaded function should return an object after incrementing the
data member.

#include <iostream> int main () {


using namespace std; Count x,y;
class Count {
x.show();
private:
int n;
y.show();
public: y= ++x;
Count () x.show();
{ n=0; } y.show();
void show () return 0;
{ cout<<"n= "<<n<<endl; } }
Count operator ++()
{ Count temp;
n=n+1;
temp.n = n;
return temp;
} };

13
14
15
How above program Works
 The above program overloads increment operator. The member
function increments the value of data member n. it stores the
incremented value in temporary object and returns the object. It
allows the user to use the increment operator in an assignment
operator.

16
Overloading Prefix/Postfix
Increment Operator
 The increment operator works in two notations i.e. prefix notation
and postfix notation.
 The operator has to be overloaded separately for both notations.

17
Example Program 3 (a)
Write a program that overloads prefix and postfix increment operator to
work with user-defined objects.
#include <iostream>
using namespace std;
class Count {
private:
int n; int main () {
public: Count x,y;
Count () x.show();
{ n=0; } ++x;
void show () x++;
{ cout<<"n= "<<n<<endl; } x.show();
void operator ++() return 0;
{ }
n=n+1;
}
void operator ++(int)
{
n=n+1; } };

18
19
20
Example Program 3 (b)
Write a program that overloads increment operator both for prefix and postfix to work with the
user-defined objects. The overloaded function should return an object after incrementing the
data member.
#include <iostream>
using namespace std;
class Count {
private:
int n; int main () {
public:
Count ()
Count x,y;
{ n=0; } x.show();
void show () y.show();
{ cout<<"n= "<<n<<endl; } y=++x;
Count operator ++()
{ Count temp;
y=x++;
n=n+1; x.show();
temp.n = n; y.show();
return temp; } return 0;
Count operator ++(int)
{ Count temp;
}
n=n+1;
temp.n = n;
return temp;} };

21
22
23
How above program Works
 The above program overloads increment operator for both prefix
and postfix notation. The keyword int in following statement
indicates that operator is overloaded for postfix:

Count operator ++(int)


 The use of int in parenthesis is not an integer parameter. It is
simply a flag to compiler that indicates that the operator is
overloaded for postfix notation.

24
Overloading Binary Operators
 A type of operator that works with two operands is called binary
operator.
 The binary operators are overloaded to increase their capabilities.

 The binary operators that can be overloaded in C++ are as


follows:

+ - * / % & | ^ << >>


== += -= /= %= &= |= ^= <<= >>=
> < <= >= && || [] ()

25
Arithmetic Operator
 The binary operators such as +,-,*,/ are binary operators. They
work with two operands.
 These operators can be overloaded to enable them to work with
user-defined object in the same way as they work with basic
types such as int, long and float etc.

26
Example Program 4
Write a program that overloads binary addition operator +.

#include <iostream> Add operator +(Add p)


using namespace std; { Add temp;
class Add {
temp.a= p.a+a;
private:
int a, b;
temp.b= p.b+b;
public: return temp;
Add () }
{ a=b=0; } };
void in()
{ cout<<"a = "; int main () {
cin>>a; Add x, y, z;
cout<<"b = "; x.in();
cin>>b; y.in();
} z=x+y;
void show()
x.show();
{ cout<<"a = "<<a<<endl;
y.show();
cout<<"b = "<<b<<endl;
} z.show();
return 0;
}
27
28
29
How above program Works
 The above program overloads the binary addition operator. When
the binary operator is used with the object, the left operand act
as calling object and the right operand acts as parameter passed
to the member function.

z = x + y;

 In the above statement, x is the calling object and y is passed as


parameter.

.
30
How above program Works
 The member function adds the values of calling object and
parameter object. It stores the result in a temporary object and
then returns the temporary object.
 The returned object is copied to the object on the right side of
assignment operator in main() function.

31
Example Program 5
Write a program that overloads arithmetic addition operator + for
concatenating two string values.
#include <iostream> int main () {
#include<string.h> String s1, s2, s3;
using namespace std; s1.in();
class String { s2.in();
private: cout<<"S1 =";
char str[50]; s1.show();
public:
cout<<"S2 =";
String ()
s2.show();
{ str[0]= '\0'; }
cout<<"S3 =";
void in()
s3.show();
{ cout<<"Enter String: ";
cout<<"Concatenating s1 and s2 in s3...
gets(str); }
"<<endl;
void show()
s3=s1+s2;
{ cout<<str<<endl; }
cout<<"S3 =";
String operator +(String s)
{ String temp; s3.show();
strcpy(temp.str, str); return 0;
strcat(temp.str, s.str); }
return temp;
} }; 32
33
34
Overloading Comparison Operator
 The comparison operators are binary operators. These operators are
frequently used to compare the values of basic data types.
 Suppose a and b are two integers and the following statement is
executed:

a==b;
 The above statement will return true if the values of a and b are
equal and false otherwise.
 The comparison operator == cannot work with user-defined
objects. But it can be overloaded in order to use it with user-defined
objects.

35
Example Program 6
Write a program that overloads the comparison operators == to work with
String class. The result of the comparison must be 1 if the two strings are of
same length and 0 otherwise.

36
#include <iostream> int main () {
#include<string.h> String s1, s2;
using namespace std; s1.in();
class String { s2.in();
private: cout<<"S1 =";
char str[50]; s1.show();
public: cout<<"S2 =";
String () s2.show();
{ str[0]= '\0'; } if(s1==s2)
void in() cout<<"Both strings are of equal
{ cout<<"Enter String: "; length.";
gets(str); else
} cout<<"Both strings are of different
void show() length.";
{ cout<<str<<endl; return 0;
} }
int operator ==(String s)
{ if(strlen(s.str)==strlen(str))
return 1;
else
return 0;
} }; 37
38
39
Overloading Arithmetic Assignment
Operators
 The arithmetic assignment operators are used to increase the
value of a variable.
 Suppose a and b are two integers and the following statement is
executed:

a+=b;
 The above statement will add the values of a and b and assign
the result to a. the arithmetic assignment operator += cannot
work with user-defined objects. But it can be overloaded in order
to use it with user-defined objects.

40
Example Program 7
Write a program that overloads arithmetic assignment operator to work with
user-defined objects.

41
#include <iostream>
#include<string.h>
using namespace std;
class Read {
private:
int days, pages;
public:
Read ()
{ days=pages=0; }
void in()
{ cout<<"How many days have you read? ";
cin>>days;
cout<<"How many pages have you read? ";
cin>>pages;

}
void show()
{ cout<<"You have read " <<pages<< " pages in "<<days<<" days."<<endl;
}
void operator +=(Read r)
{ days=days+r.days;
pages=pages+r.pages;
} };
42
int main () {
Read r1, r2;
r1.in();
r2.in();
cout<<"\n Reading number 1..."<<endl;
r1.show();
cout<<"\n Reading number 2..."<<endl;
r2.show();
cout<<"\n Adding r1 and r2 using += operator..."<<endl;
r2+=r1;
cout<<"\n10The total reading is as follows:"<<endl;
r2.show();
return 0;
}

43
44
How above program Works
 The above program overloads arithmetic assignment operator +=
to work with objects of Read class. The overloading member
function accepts an object as a parameter and adds the values of
parameter to the calling object as follows:

days=days+r.days;

pages=pages+r.pages;
 In the baove statement, days, pages are the data members of
calling object r2 and r.days and r.pages are the data members
of the parameter object.

45

You might also like