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:7
C++ program to add two objects using overload binary operator “+” with friend functions.

// to overload + operator
// add two objects  with friend functions
#include<iostream>
#include<conio.h>
using namespace std;
class sample
{
private:
	int a;
public:
	sample();
	sample(int n);
	void get();
	void disp();
friend sample operator+(sample s1,sample s2);
};
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 operator+(sample s1,sample s2)
{
  sample t;
  t.a=s1.a+s2.a;
  return(t);
}
int main()
{
  sample e1,e2,e3;
  e1.get();e2.get();
  e1.disp();e2.disp();
  e3=e1+e2;//e3=operator+(e1,e2);
  e3.disp();
  getch();
  return(0);
}

Output:

Enter the value of a 10
Enter the value of a 20
a= 10
a= 20
a= 30

Example:8
Declare a class named “distance” with attributes as feet and inches. Take input for two objects calculate and print their sum using overloaded binary operator “+”.

//distance1 : feet inches  (without friend function)
// add two distance1s using "+" overloaded operator
#include<iostream>
#include<conio.h>
using namespace std;
class distance1
{
private:
  int feet,inches;
public:
  distance1();//defa
  distance1(int f,int i); //para
  void read();
  void show();
  distance1 operator+(distance1 d);
};
distance1::distance1()
{
   feet=0;
   inches=0;
}
distance1::distance1(int f,int i)
{
   feet=f;
   inches=i;
}
void distance1::read()
{
   cout<<"Enter feet and inches ";
   cin>>feet>>inches;
}
void distance1::show()
{
   cout<<"feet "<<feet<<" inches "<<inches<<endl;
}
distance1 distance1::operator+(distance1 d)
{
  distance1 t;
  t.feet=feet+d.feet;
  t.inches=inches+d.inches;
  if(t.inches>=12)
  {
     t.feet=t.feet+t.inches/12;//t.feet+=t.inches/12;
     t.inches=t.inches%12;
  }
  return(t);
}
int main()
{
  distance1 d1,d2,d3;
  d1.read();d2.read();
  d1.show();d2.show();
  d3=d1+d2;//d3=d1.operator+(d2);
  d3.show();
  getch();
  return(0);
}

Output:

Enter feet and inches 5 10
Enter feet and inches 6 11
feet 5 inches 10
feet 6 inches 11
feet 12 inches 9

Example:9
Declare a class named “distance” with attributes as feet and inches. Take input for two objects calculate and print their sum using overloaded binary operator “+” with friend function.

//distance1 : feet inches
//using friend function

#include<iostream>
#include<conio.h>
using namespace std;
class distance1
{
private:
  int feet,inches;
public:
  distance1();
  distance1(int f,int i);
  void read();
  void show();
  friend distance1 operator+(distance1 d1,distance1 d2);
};
distance1::distance1()
{
  feet=0;
  inches=0;
}
distance1::distance1(int f,int i)
{
	feet=f;
	inches=i;
}
void distance1::read()
{
	cout<<"Enter feet and inches ";
	cin>>feet>>inches;
}
void distance1::show()
{
	cout<<feet<<" "<<inches<<endl;
}
distance1 operator+(distance1 d1,distance1 d2)
{
  distance1 t;
  t.feet=d1.feet+d2.feet;
  t.inches=d1.inches+d2.inches;
  if(t.inches>=12)
  {
     t.feet=t.feet+t.inches/12;//t.feet+=t.inches/12;
     t.inches=t.inches%12;
  }
  return(t);
}
int main()
{
  distance1 d1,d2,d3;
  d1.read();d2.read();d1.show();d2.show();
  d3=d1+d2;//d3=operator+(d1,d2);
  d3.show();
  getch();
  return(0);
}

Output:

Enter feet and inches 5 20
Enter feet and inches 6 30
5 20
6 30
15 2