C++ Tutorial Home Page

C++ Operator Overloading

What is operator overloading
Rules for overloading Operators
Operator Overloading restrictions
List of operators that cannot be overloaded.
Overloading Unary operator
Overloading increment operator(++)
Overloading decrement operator(–)
Overloading unary operator(-)
Overloading increment operator(++) postfix
Overloading decrement operator(–) postfix
Overloading increment operator (++) with friend function
Overloading increment operator (–) with friend function
Increment the time using ++
Decrement the time using ++
Overloading Binary operator(+,-,*,/,%)
Add two object using “+” operator with friend function
Overloading “-” operator
Overloading “*” operator
Overloading “/” operator
Overloading “%” operator
overloading all (+,-,*,/,%)
Overloading “+” operator with friend function
class distance (feet,inches) with “+” operator
class distance (feet,inches) with “+” operator with friend function
class distance (km,m) with “+” operator
class distance (km,m) with “+” operator with friend function
class distance (m,cm) with “+” operator
class distance (m,cm) with “+” operator with friend function
class distance (km,m,cm) with “+” operator
class distance (km,m,cm) with “+” operator with friend function
class distance (km,m,cm,mm) with “+” operator
class distance (km,m,cm,mm) with “+” operator with friend function
class time(hh,mm,ss) with “+” operator
class time(hh,mm,ss) with “+” operator with friend function
class complex(real,ing) with “+” operator
class complex(real,ing) with “+” operator with friend function
class string(str) with “+” operator
class string(str) with “+” operator with friend function
overloading “+=” operator

C++:Overloading Binary Operators 

Example:4
C++ program to overload binary operator “/”.

// to overload / operator
// with two objects
#include<iostream>
#include<conio.h>
using namespace std;
class sample
{
private:
	int a;
public:
	sample();
	sample(int n);
	void get();
	void disp();
	sample operator/(sample s);
};
sample::sample()
{
	a=0;
}
sample::sample(int n)
{
	a=n;
}
void sample::get()
{
	cout<<"Enter the value of a ";
	cin>>a;
}
void sample::disp()
{
	cout<<"a= "<<a<<endl;
}
sample sample::operator/(sample s)
{
	sample t;
	t.a=a/s.a;
	return(t);
}

int main()
{
  sample s1,s2,s3;
  s1.get();s2.get();
  s1.disp();s2.disp();
  s3=s1/s2;//s3=s1.operator/(S2);
  s3.disp();
  getch();
  return(0);
}

Output:

Enter the value of a 50
Enter the value of a 5
a= 50
a= 5
a= 10

Example:5
C++ program to overload binary operator “%”.

// to overload % operator
// with two objects
#include<iostream>
#include<conio.h>
using namespace std;
class sample
{
private:
	int a;
public:
	sample();
	sample(int n);
	void get();
	void disp();
	sample operator%(sample s);
};
sample::sample()
{
	a=0;
}
sample::sample(int n)
{
	a=n;
}
void sample::get()
{
	cout<<"Enter the value of a ";
	cin>>a;
}
void sample::disp()
{
	cout<<"a= "<<a<<endl;
}
sample sample::operator%(sample s)
{
	sample t;
	t.a=a%s.a;
	return(t);
}

int main()
{
  sample s1,s2,s3;
  s1.get();s2.get();
  s1.disp();s2.disp();
  s3=s1%s2;//s3=s1.operator%(S2);
  s3.disp();
  getch();
  return(0);
}

Output:

Enter the value of a 10
Enter the value of a 7
a= 10
a= 7
a= 3

Example:6
C++ program to overload all binary operator “+”, “-‘” ,”*” ,”/” and “%”.

// to overload + ,- ,* , / , % operator
// with two objects

#include<iostream>
#include<conio.h>
using namespace std;
class sample
{
private:
 int a;
public:
 sample();
 sample(int n);
 void get();
 void disp();
 sample operator+(sample s);
 sample operator-(sample s);
 sample operator*(sample s);
 sample operator/(sample s);
 sample operator%(sample s);
};
sample::sample()
{
 a=0;
}
sample::sample(int n)
{
 a=n;
}
void sample::get()
{
 cout<<"Enter the value of a ";
 cin>>a;
}
void sample::disp()
{
 cout<<"a= "<<a<<endl;
}
// +
sample sample::operator+(sample s)
{
 sample t;
 t.a=a+s.a;
 return(t);
}
// -
sample sample::operator-(sample s)
{
 sample t;
 t.a=a-s.a;
 return(t);
}
// *
sample sample::operator*(sample s)
{
 sample t;
 t.a=a*s.a;
 return(t);
}
// /
sample sample::operator/(sample s)
{
 sample t;
 t.a=a/s.a;
 return(t);
}
// %
sample sample::operator%(sample s)
{
 sample t;
 t.a=a%s.a;
 return(t);
}
int main()
{
 sample s1,s2,s3;
 s1.get();s2.get();
 s1.disp();s2.disp();
 //+
 s3=s1+s2;//s3=s1.operator+(S2);
 s3.disp();
 //-
 s3=s1-s2;//s3=s1.operator-(S2);
 s3.disp();
 //*
 s3=s1*s2;//s3=s1.operator*(S2);
 s3.disp();
 // /
 s3=s1/s2;//s3=s1.operator/(S2);
 s3.disp();
 // %
 s3=s1%s2;//s3=s1.operator%(S2);
 s3.disp();
 getch();
 return(0);
}

Output:

Enter the value of a 10
Enter the value of a 5
a= 10
a= 5
a= 15
a= 5
a= 50
a= 2
a= 0