Introduction
Operator overloading is one of the best features of C++. By overloading operators, we can give additional meaning to operators like +,-,*,<=,>=, etc. which by default are supposed to work only on standard data types like int, float, etc.
The operators that cannot be overloaded are ., ::, ? and : .
Here, we created a simple class, whose name is xy example for operator overloading. We created two int variables, x,y, to test all operations of operator overloading features in C++.
- #include<iostream.h>
- #include<conio.h>
- class xy
- {
- private:
- int x,y;
- public:
- // ======constructors=========
- xy()
- { x=y=0; }
- xy(int i,int j)
- {x=i;y=j;}
- xy(xy &z)
- {x=z.x;y=z.y;}
- // ====== unary operators ========
- xy operator-();
- xy operator++();
- xy operator--();
- xy operator++(int);
- xy operator--(int);
- //======= binary operators ========
- xy operator+(xy &);
- xy operator-(xy &);
- xy operator*(xy &);
- xy operator/(xy &);
- xy operator%(xy &);
- xy operator=(xy &);
- xy operator+=(xy &);
- xy operator-=(xy &);
- xy operator*=(xy &);
- xy operator/=(xy &);
- xy operator%=(xy &);
- // =======comparison or logical operators============
- int operator<(xy &);
- int operator>(xy &);
- int operator==(xy &);
- int operator!=(xy &);
- int operator<=(xy &);
- int operator>=(xy &);
- //========= inseration and extraction operators ==========
- friend ostream&operator<<(ostream&, xy&);
- friend istream&operator>>(istream&, xy&);
- };
Next, we created a body of all functions and called a function in the functions of a C++ program.
- //Function operator-() for return absolute value.
- xy xy::operator-()
- {
- if(x<0){x*=-1;}
- if(y<0){y*=-1;}
- return *this;
- }
- //Function of unary operators.
- xy xy::operator++()
- {
- ++x;++y;
- return *this;
- }
- xy xy::operator--()
- {
- --x;--y;
- return *this;
- }
- xy xy::operator++(int)
- {
- x++;y++;
- return *this;
- }
- xy xy::operator--(int)
- {
- x--;y--;
- return *this;
- }
- //Function of binary operators.
- xy xy::operator+(xy &z)
- {
- xy t;
- t.x=x+z.x;
- t.y=y+z.y;
- return t;
- }
- xy xy::operator-(xy &z)
- {
- xy t;
- t.x=x-z.x;
- t.y=y-z.y;
- return t;
- }
- xy xy::operator*(xy &z)
- {
- xy t;
- t.x=x*z.x;
- t.y=y*z.y;
- return t;
- }
- xy xy::operator=(xy &z)
- {
- x=z.x;y=z.y;
- return *this;
- }
- xy xy::operator/(xy &z)
- {
- xy t;
- t.x=x/z.x;
- t.y=y/z.y;
- return t;
- }
- xy xy::operator%(xy &z)
- {
- xy t;
- t.x=x%z.x;
- t.y=y%z.y;
- return t;
- }
- xy xy::operator+=(xy &z)
- {
- xy t;
- t.x=x+z.x;
- t.y=y+z.y;
- return t;
- }
- xy xy::operator-=(xy &z)
- {
- xy t;
- t.x=x-z.x;
- t.y=y-z.y;
- return t;
- }
- xy xy::operator*=(xy &z)
- {
- xy t;
- t.x=x*z.x;
- t.y=y*z.y;
- return t;
- }
- xy xy::operator/=(xy &z)
- {
- xy t;
- t.x=x/z.x;
- t.y=y/z.y;
- return t;
- }
- xy xy::operator%=(xy &z)
- {
- xy t;
- t.x=x%z.x;
- t.y=y%z.y;
- return t;
- }
- //Function of insertion(>>) & extraction(<<) operators.
- ostream&operator<<(ostream &o, xy &z)
- {
- o<<endl<<"(x: "<<z.x<<",y: "<<z.y<<")";
- return o;
- }
- istream&operator>>(istream &i, xy &z)
- {
- cout<<endl<<"Enter X & Y: ";
- i>>z.x>>z.y;
- return i;
- }
- //main program to call some overloaded operators.
- void main()
- {
- clrscr();
- xy a,b(10,20),c;
- cin>>a;
- cout<<a<<b;
- cout<<endl<<"a+b:";
- c=a+b;
- cout<<c;
- cout<<endl<<"a-b:";
- c=a-b;
- cout<<c;
- cout<<endl<<"a*b:";
- c=a*b;
- cout<<c;
- cout<<endl<<"a/b:";
- c=a/b;
- cout<<c;
- cout<<endl<<"a%b:";
- c=a%b;
- cout<<c;
- cout<<endl<<"c+=b:";
- c=c+=b;
- cout<<c;
- getch();
- }

The calling function internal layout is c=a.operator+(b); but C++ provided user-friendly features operator overloading, so our calling layout is c=a+b like normal default data types operations. I hope operator overloading will be helpful for students and beginner to understand basic fundamentals of object-oriented programming.

Join the conversation! Your thoughts help the community grow.