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 operators (+=, -=, *=, /=, %=)

Example:6
Add two objects using the overloaded operator “+=”. with the friend function.

// to overload += operator with friend function
// add two objects
#include<iostream>
#include<conio.h>
using namespace std;
class sample
{
	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 s1,s2,s3;
	s1.get();s2.get();
	s1.disp();s2.disp();
	s3=(s1+=s2);  //s3=opertor+=(s1,s2);
	s3.disp();
	getch();
	return(0);
}

Output:

Enter the value of a 10
Enter the value of a 25
a= 10
a= 25
a= 35

Example:7
Multiply two objects using overloaded operator “*=”. with friend function.

code

Output:

Example:8
Using overloaded operator “+=” add a value to the objects data element

//  overload += operator
//  to add a value to the data element  of an object
#include<iostream>
#include<conio.h>
using namespace std;
class sample
{
	int a;
public:
	sample();
	sample(int n);
	void get();
	void disp();
	void operator+=(int n);
};
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;
}
void  sample::operator+=(int n)
{
	a+=n;
}

int main()
{
	sample s1;
	int value;
	s1.get();s1.disp();
	cout<<"Enter the value to add ";
	cin>>value;
	s1+=value; //s1.operator+=(value);
	s1.disp();
	getch();
	return(0);
}

Output:

Enter the value of a 10
a= 10
Enter the value to add 25
a= 35

Example:9
Using overloaded operator
“+=” : add a value to the objects data element
“*=”: calculate the product of two objects
“-=” : calculate the difference between two objects

// to overload += operator
//  to add a value to an object value
// subtract a value
// multiple by a value
#include<stdio.h>
#include<iostream>
#include<conio.h>
using namespace std;
class sample
{
	private:
		int a;
	public:
		sample();
		sample(int n);
		~sample();
		void get();
		void disp();
		void operator+=(int n);
		void operator-=(int n);
		void operator*=(int n);
};
sample::sample()
{
	a=0;
}
sample::sample(int n)
{
	a=n;
}
sample::~sample()
{
}
void sample::get()
{
	cout<<"Enter the value of a ";
	cin>>a;
}
void sample::disp()
{
	cout<<"a= "<<a<<endl;
}
void  sample::operator+=(int n)
{
	a+=n;
}
void sample::operator-=(int n)
{
	a-=n;
}
void sample::operator*=(int n)
{
	a*=n;
}
int main()
{
	sample s1;
	int value;
	// to add
	s1.get();
	cout<<"s1 is "<<endl;
	s1.disp();
	cout<<"Enter the value to add ";
	cin>>value;
	s1+=value;
	cout<<"s1 is "<<endl;
	s1.disp();
	// to sub
	s1.get();
	cout<<"s1 is "<<endl;
	s1.disp();
	cout<<"Enter the value to sub ";
	cin>>value;
	s1-=value;
	cout<<"s1 is "<<endl;
	s1.disp();
	// to mul
	s1.get();
	cout<<"s1 is "<<endl;
	s1.disp();
	cout<<"Enter the value to mul with  ";
	cin>>value;
	s1*=value;
	cout<<"s1 is "<<endl;
	s1.disp();
	getch();
	return(0);
}

Output:

Enter the value of a 10
s1 is
a= 10
Enter the value to add 500
s1 is
a= 510
Enter the value of a 10
s1 is
a= 10
Enter the value to sub 5
s1 is
a= 5
Enter the value of a 50
s1 is
a= 50
Enter the value to mul with 5
s1 is
a= 250