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