Operator Overloading In C++ For Students And Beginners

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++.
  1. #include<iostream.h>  
  2. #include<conio.h>  
  3. class xy  
  4. {  
  5.  private:  
  6.   int x,y;  
  7.  public:  
  8.   // ======constructors=========  
  9.   xy()  
  10.   { x=y=0; }  
  11.   xy(int i,int j)  
  12.   {x=i;y=j;}  
  13.   xy(xy &z)  
  14.   {x=z.x;y=z.y;}  
  15.   // ====== unary operators ========  
  16.   xy operator-();  
  17.   xy operator++();  
  18.   xy operator--();  
  19.   xy operator++(int);  
  20.   xy operator--(int);  
  21.   //======= binary operators ========  
  22.   xy operator+(xy &);  
  23.   xy operator-(xy &);  
  24.   xy operator*(xy &);  
  25.   xy operator/(xy &);  
  26.   xy operator%(xy &);  
  27.   xy operator=(xy &);  
  28.   xy operator+=(xy &);  
  29.   xy operator-=(xy &);  
  30.   xy operator*=(xy &);  
  31.   xy operator/=(xy &);  
  32.   xy operator%=(xy &);  
  33.   // =======comparison or logical operators============  
  34.   int operator<(xy &);  
  35.   int operator>(xy &);  
  36.   int operator==(xy &);  
  37.   int operator!=(xy &);  
  38.   int operator<=(xy &);  
  39.   int operator>=(xy &);  
  40.   //========= inseration and extraction operators ==========  
  41.   friend ostream&operator<<(ostream&, xy&);  
  42.   friend istream&operator>>(istream&, xy&);  
  43. };  
Next, we created a body of all functions and called a function in the functions of a C++ program.
  1. //Function operator-() for return absolute value.  
  2.   
  3. xy xy::operator-()  
  4. {  
  5.  if(x<0){x*=-1;}  
  6.  if(y<0){y*=-1;}  
  7.  return *this;  
  8. }  
  9.   
  10. //Function of unary operators.  
  11.   
  12. xy xy::operator++()  
  13. {  
  14.  ++x;++y;  
  15.  return *this;  
  16. }  
  17. xy xy::operator--()  
  18. {  
  19.  --x;--y;  
  20.  return *this;  
  21. }  
  22. xy xy::operator++(int)  
  23. {  
  24.  x++;y++;  
  25.  return *this;  
  26. }  
  27. xy xy::operator--(int)  
  28. {  
  29.  x--;y--;  
  30.  return *this;  
  31. }  
  32.   
  33. //Function of binary operators.  
  34.   
  35. xy xy::operator+(xy &z)  
  36. {  
  37.   xy t;  
  38.   t.x=x+z.x;  
  39.   t.y=y+z.y;  
  40.   return t;  
  41. }  
  42. xy xy::operator-(xy &z)  
  43. {  
  44.   xy t;  
  45.   t.x=x-z.x;  
  46.   t.y=y-z.y;  
  47.   return t;  
  48. }  
  49. xy xy::operator*(xy &z)  
  50. {  
  51.   xy t;  
  52.   t.x=x*z.x;  
  53.   t.y=y*z.y;  
  54.   return t;  
  55. }  
  56. xy xy::operator=(xy &z)  
  57. {  
  58.   x=z.x;y=z.y;  
  59.   return *this;  
  60. }  
  61. xy xy::operator/(xy &z)  
  62. {  
  63.   xy t;  
  64.   t.x=x/z.x;  
  65.   t.y=y/z.y;  
  66.   return t;  
  67. }  
  68. xy xy::operator%(xy &z)  
  69. {  
  70.   xy t;  
  71.   t.x=x%z.x;  
  72.   t.y=y%z.y;  
  73.   return t;  
  74. }  
  75. xy xy::operator+=(xy &z)  
  76. {  
  77.   xy t;  
  78.   t.x=x+z.x;  
  79.   t.y=y+z.y;  
  80.   return t;  
  81. }  
  82. xy xy::operator-=(xy &z)  
  83. {  
  84.   xy t;  
  85.   t.x=x-z.x;  
  86.   t.y=y-z.y;  
  87.   return t;  
  88. }  
  89. xy xy::operator*=(xy &z)  
  90. {  
  91.   xy t;  
  92.   t.x=x*z.x;  
  93.   t.y=y*z.y;  
  94.   return t;  
  95. }  
  96. xy xy::operator/=(xy &z)  
  97. {  
  98.   xy t;  
  99.   t.x=x/z.x;  
  100.   t.y=y/z.y;  
  101.   return t;  
  102. }  
  103. xy xy::operator%=(xy &z)  
  104. {  
  105.   xy t;  
  106.   t.x=x%z.x;  
  107.   t.y=y%z.y;  
  108.   return t;  
  109. }  
  110.   
  111. //Function of insertion(>>) & extraction(<<) operators.  
  112.   
  113. ostream&operator<<(ostream &o, xy &z)  
  114. {  
  115.  o<<endl<<"(x: "<<z.x<<",y: "<<z.y<<")";  
  116.  return o;  
  117. }  
  118. istream&operator>>(istream &i, xy &z)  
  119. {  
  120.  cout<<endl<<"Enter X & Y: ";  
  121.  i>>z.x>>z.y;  
  122.  return i;  
  123. }  
  124.   
  125. //main program to call some overloaded operators.  
  126.   
  127. void main()  
  128. {  
  129.   clrscr();  
  130.   xy a,b(10,20),c;  
  131.   cin>>a;  
  132.   cout<<a<<b;  
  133.   cout<<endl<<"a+b:";  
  134.   c=a+b;  
  135.   cout<<c;  
  136.   cout<<endl<<"a-b:";  
  137.   c=a-b;  
  138.   cout<<c;  
  139.   cout<<endl<<"a*b:";  
  140.   c=a*b;  
  141.   cout<<c;  
  142.   cout<<endl<<"a/b:";  
  143.   c=a/b;  
  144.   cout<<c;  
  145.   cout<<endl<<"a%b:";  
  146.   c=a%b;  
  147.   cout<<c;  
  148.   cout<<endl<<"c+=b:";  
  149.   c=c+=b;  
  150.   cout<<c;  
  151.   getch();  
  152. }  
Output
 
 
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.


Similar Articles