Overloading
C++ Operators Available for Overloading
+-*/%^&|~=
<>+=-=*=/=%=^=&=|=
<<>>>>=<<===!=<=>=&&||
++---<*,->[]()newdelete!
Operator +
  1. #ifndef V_H
  2. #define V_H
  3. #include <iostream>
  4. #include <string>
  5.  
  6. class V
  7. {
  8. private:
  9. int size;
  10. int *array;
  11. public:
  12. //constructor
  13. V(int s):size(s)
  14. {
  15. std::cout<<"Constructor ..."<<std::endl;
  16. array = new int[size];
  17. for(int i = 0; i < size; i++)
  18. array[i] = 10*i;
  19. std::cout<<"----End Constructor ..."<<std::endl;
  20. }
  21.  
  22. //copy constructor
  23. V(const V &right):size(right.size), array(size?new int [size]:nullptr)
  24. {
  25. std::cout<<"Copy Constructor ..."<<std::endl;
  26. std::copy(right.array, right.array+size, array);
  27. std::cout<<"----End Copy Constructor ..."<<std::endl;
  28. }
  29.  
  30. //move constructor
  31. V(V && right):size(0), array(nullptr)
  32. {
  33. std::cout<<"Move Constructor ..."<<std::endl;
  34. size = right.size;
  35. array = right.array;
  36. right.size = 0;
  37. right.array = nullptr;
  38. std::cout<<"----End Move Constructor ..."<<std::endl;
  39. }
  40.  
  41. int *getAddress() const {return array;}
  42. int getSize() const {return size;}
  43.  
  44. void display() const
  45. {
  46. for(int i = 0; i < size; i++)
  47. std::cout<<array[i]<<" ";
  48. std::cout<<std::endl;
  49. }
  50.  
  51. std::string toString() const
  52. {
  53. std::string str = "V: ";
  54. for(int i = 0; i < size; i++)
  55. str += std::to_string(array[i])+" ";
  56. return str;
  57. }
  58.  
  59. V & time(int n)
  60. {
  61. for(int i = 0; i < size; i++)
  62. array[i] *= n;
  63. return *this;
  64. }
  65.  
  66. //copy assignment
  67. const V& operator=(const V &right)
  68. {
  69. std::cout<<"Copy Assignment ..."<<size<<std::endl;
  70. if(this != &right)
  71. {
  72. V temp(right);
  73. //std::swap(size, temp.size);
  74. //std::swap(array, temp.array);
  75. std::swap(*this, temp);
  76. }
  77. std::cout<<"----End Assignment ..."<<std::endl;
  78.  
  79. return *this;
  80. }
  81.  
  82. //move assignment
  83. V & operator=(V&& right)
  84. {
  85. std::cout<<"Move Assignment ..."<<std::endl;
  86. if(this != &right)
  87. {
  88. delete [] array;
  89. array = 0;
  90. size = 0;
  91.  
  92. size = right.size;
  93. array = right.array;
  94.  
  95. right.size = 0;
  96. right.array = nullptr;
  97. }
  98. std::cout<<"----End Move Assignment ..."<<std::endl;
  99.  
  100. return *this;
  101. }
  102.  
  103. //Overloading
  104. V operator+(const V &right)
  105. {
  106. std::cout<<"Operator + ..."<<std::endl;
  107. V temp(size+right.size);
  108. std::copy(array, array+size, temp.array);
  109. std::copy(right.array, right.array+right.size, temp.array+size);
  110. std::cout<<"End Operator + ..."<<std::endl;
  111.  
  112. return temp;
  113. }
  114.  
  115. ~V()
  116. {
  117. std::cout<<"Destructor ..."<<size<<std::endl;
  118. delete array;
  119. array = 0;
  120. size = 0;
  121. }
  122. };
  123. #endif
  1. #include <iostream>
  2. #include "V.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. V v(3);
  7. V v2(4);
  8.  
  9. V v3 = v + v2;
  10.  
  11. v.display();
  12. v2.display();
  13. v3.display();
  14.  
  15. return 0;
  16. }
Prefix ++
  1. //Prefix ++ Operator
  2. V operator++()
  3. {
  4. std::cout<<"Prefix ++ ..."<<std::endl;
  5. for(int i = 0; i < size; i++)
  6. array[i] += 1;
  7. std::cout<<"End Prefix ++ ..."<<std::endl;
  8. return *this;
  9. }
Postfix ++
  1. //Postfix ++ Operator
  2. V operator++(int)
  3. {
  4. std::cout<<"Postfix ++ ..."<<std::endl;
  5. V temp(*this);
  6. for(int i = 0; i < size; i++)
  7. array[i] += 1;
  8. std::cout<<"End Postfix ++ ..."<<std::endl;
  9. return temp;
  10. }
  1. #include <iostream>
  2. #include "V.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. V v(10);
  7.  
  8. V v2 = ++v;
  9.  
  10. std::cout<<v.toString()<<std::endl;
  11. std::cout<<v2.toString()<<std::endl;
  12.  
  13. V v3 = v++;
  14. std::cout<<v.toString()<<std::endl;
  15. std::cout<<v3.toString()<<std::endl;
  16.  
  17. return 0;
  18. }
Operator <
  1. bool operator<(const V &right)
  2. {
  3. if(size < right.size)
  4. return true;
  5. else
  6. return false;
  7. }
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include "V.h"
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. std::vector<V> container;
  9. V v(10), v2(2), v3(4), v4(20);
  10. container.push_back(v);
  11. container.push_back(v2);
  12. container.push_back(v3);
  13. container.push_back(v4);
  14.  
  15. std::sort(container.begin(), container.begin()+4);
  16.  
  17. for(int i = 0; i < container.size(); i++)
  18. std::cout<<container[i].toString()<<std::endl;
  19.  
  20. return 0;
  21. }
Operator <<
  1. #ifndef V_H
  2. #define V_H
  3. #include <iostream>
  4. #include <string>
  5.  
  6. class V
  7. {
  8. private:
  9. int size;
  10. int *array;
  11. public:
  12. //constructor
  13. V(int s):size(s)
  14. {
  15. std::cout<<"Constructor ..."<<std::endl;
  16. array = new int[size];
  17. for(int i = 0; i < size; i++)
  18. array[i] = 10*i;
  19. std::cout<<"----End Constructor ..."<<std::endl;
  20. }
  21.  
  22. //copy constructor
  23. V(const V &right):size(right.size), array(size?new int [size]:nullptr)
  24. {
  25. std::cout<<"Copy Constructor ..."<<std::endl;
  26. std::copy(right.array, right.array+size, array);
  27. std::cout<<"----End Copy Constructor ..."<<std::endl;
  28. }
  29.  
  30. //move constructor
  31. V(V && right):size(0), array(nullptr)
  32. {
  33. std::cout<<"Move Constructor ..."<<std::endl;
  34. size = right.size;
  35. array = right.array;
  36. right.size = 0;
  37. right.array = nullptr;
  38. std::cout<<"----End Move Constructor ..."<<std::endl;
  39. }
  40.  
  41. int *getAddress() const {return array;}
  42. int getSize() const {return size;}
  43.  
  44. void display() const
  45. {
  46. for(int i = 0; i < size; i++)
  47. std::cout<<array[i]<<" ";
  48. std::cout<<std::endl;
  49. }
  50.  
  51. std::string toString() const
  52. {
  53. std::string str = "V: ";
  54. for(int i = 0; i < size; i++)
  55. str += std::to_string(array[i])+" ";
  56. return str;
  57. }
  58.  
  59. V & time(int n)
  60. {
  61. for(int i = 0; i < size; i++)
  62. array[i] *= n;
  63. return *this;
  64. }
  65.  
  66. //copy assignment
  67. const V& operator=(const V &right)
  68. {
  69. std::cout<<"Copy Assignment ..."<<size<<std::endl;
  70. if(this != &right)
  71. {
  72. V temp(right);
  73. //std::swap(size, temp.size);
  74. //std::swap(array, temp.array);
  75. std::swap(*this, temp);
  76. }
  77. std::cout<<"----End Assignment ..."<<std::endl;
  78.  
  79. return *this;
  80. }
  81.  
  82. //move assignment
  83. V & operator=(V&& right)
  84. {
  85. std::cout<<"Move Assignment ..."<<std::endl;
  86. if(this != &right)
  87. {
  88. delete [] array;
  89. array = 0;
  90. size = 0;
  91.  
  92. size = right.size;
  93. array = right.array;
  94.  
  95. right.size = 0;
  96. right.array = nullptr;
  97. }
  98. std::cout<<"----End Move Assignment ..."<<std::endl;
  99.  
  100. return *this;
  101. }
  102.  
  103. //Overloading
  104. //Operator <<
  105. friend std::ostream &operator<<(std::ostream &strm, const V &right);
  106. //Operator >>
  107. friend std::istream &operator>>(std::istream &strm, V &right);
  108.  
  109. ~V()
  110. {
  111. std::cout<<"Destructor ..."<<size<<std::endl;
  112. delete array;
  113. array = 0;
  114. size = 0;
  115. }
  116. };
  117. #endif
  1. #ifndef UTIL_H
  2. #define UTIL_H
  3. #include <iostream>
  4.  
  5. class V;
  6.  
  7. std::ostream &operator<<(std::ostream &strm, const V &right)
  8. {
  9. strm<<"V: ";
  10. for(int i = 0; i < right.size; i++)
  11. strm<<right.array[i]<<" ";
  12. return strm;
  13. }
  14.  
  15. std::istream &operator>>(std::istream &strm, V &right)
  16. {
  17. delete [] right.array;
  18. right.array = nullptr;
  19. right.size = 0;
  20.  
  21. std::cout<<"Enter the size: "<<std::endl;
  22. strm>>right.size;
  23. right.array = new int[right.size];
  24. for(int i = 0; i < right.size; i++)
  25. right.array[i] = i*10;
  26.  
  27. return strm;
  28. }
  29. #endif
  1. #include <iostream>
  2. #include "V.h"
  3. #include "util.h"
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. V v(10);
  8.  
  9. std::cout<<v<<std::endl;
  10.  
  11. V v2(0);
  12. std::cin>>v2;
  13. std::cout<<v2<<std::endl;
  14.  
  15.  
  16. return 0;
  17. }
Operator []
  1. int &operator[](const int &index)
  2. {
  3. if(index < 0 || index >= size)
  4. {
  5. std::cerr<<"Out of range ..."<<std::endl;
  6. std::exit(1);
  7. }
  8. return array[index];
  9. }
  1. #include <iostream>
  2. #include "V.h"
  3. #include "util.h"
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. V v(10);
  8.  
  9. int a = v[2];
  10. std::cout<<a<<std::endl;
  11.  
  12. v[2] = 100;
  13. std::cout<<v<<std::endl;
  14.  
  15.  
  16. return 0;
  17. }
Object Conversion
  1. //Convert object to integer
  2. operator int()
  3. {
  4. return size;
  5. }
  1. #include <iostream>
  2. #include "V.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. V v(10);
  7.  
  8. int size = v;
  9.  
  10. std::cout<<"Size: "<<size<<std::endl;
  11.  
  12. return 0;
  13. }