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++:Operator Overloading 21

Example:7
Define an equal to relational operator function named operator ==() that can be used to compare two date class objects having data members as day, month and year.
Sol:

// to overload == operator
//  to compare two date classes(day,month,year) objects
#include<iostream>
#include<conio.h>
using namespace std;
class date
{
private:
	int day,month,year;
public:
	date();
	date(int d,int m,int y);
	void get();
	void disp();
	int operator==(date d);

};
date::date()
{
	day=0;month=0;year=0;
}
date::date(int d,int m,int y)
{
   day=d;month=m;year=y;
}
void date::get()
{
  cout<<"Enter date(day,month and year) ";
  cin>>day>>month>>year;
}
void date::disp()
{
  cout<<"day "<<day<<" month "<<month<<" year "<<year<<endl;
}
int date::operator==(date d)
{
  if (day==d.day && month==d.month && year==d.year)
  return(1);
  else
  return(0);
}
int main()
{
  date s1,s2;
  s1.get();s2.get();
  s1.disp();s2.disp();
  int t;
  t=s1==s2;  //t=s1.operator==(s2);
  if (t==1)
  cout<<"Both the dates are same"<<endl;
  else
  cout<<"Date are not same"<<endl;
  getch();
  return(0);
}

Output:

Enter date(day,month and year) 10 2 2021
Enter date(day,month and year) 10 2 2021
day 10 month 2 year 2021
day 10 month 2 year 2021
Both the dates are same

 

Example:8
Declare a class named time with attributes as hh,mm and ss. Take input for two objects and compare them using overloaded “==” operator.
Sol:

code

Output: