fork download
  1. // If you are not sure what some lines of code do, try looking back at
  2. // previous example programs, notes, or ask a question.
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. // Example class implementing several overloaded operators
  9. class Vector2 {
  10. public:
  11. // Default constructor
  12. Vector2();
  13. // Parametrized constructor
  14. Vector2(int _x, int _y);
  15. // Copy constructor
  16. Vector2(const Vector2& src);
  17.  
  18. // Destructor
  19. ~Vector2();
  20.  
  21. // Assignment operator
  22. Vector2& operator=(const Vector2& src);
  23.  
  24. // Example math operators
  25. Vector2 operator+(const Vector2& src); // Note that this is not returned by reference,
  26. // as we are returning a new object
  27. Vector2& operator+=(const Vector2& src); // Note that this is returned by reference,
  28. // as we are returning an object that already exists
  29.  
  30. // Example comparison operators
  31. bool operator==(const Vector2& comp);
  32. bool operator<(const Vector2& comp);
  33.  
  34. // Input/output operators
  35. friend ostream& operator<<(ostream& out, const Vector2& src);
  36. friend istream& operator>>(istream& in, Vector2& src); // Note that src is not constant,
  37. // as its values will be changed
  38.  
  39. private:
  40. // Data members
  41. int x, y;
  42. };
  43.  
  44. Vector2::Vector2() {
  45. x = 0;
  46. y = 0;
  47. }
  48.  
  49. Vector2::Vector2(int _x, int _y) {
  50. x = _x;
  51. y = _y;
  52. }
  53.  
  54. Vector2::Vector2(const Vector2& src) {
  55. // Don't have to do any checking here, as we know we are creating a new object
  56. x = src.x;
  57. y = src.y;
  58. }
  59.  
  60. Vector2::~Vector2() {
  61.  
  62. }
  63.  
  64. Vector2& Vector2::operator=(const Vector2& src) {
  65. // Check that you're not assigning the object to itself
  66. if(this != &src) {
  67. x = src.x;
  68. y = src.y;
  69. }
  70. // Return for chaining
  71. return *this;
  72. }
  73.  
  74. Vector2 Vector2::operator+(const Vector2& src) {
  75. // Use the parametrized constructor to return a new Vector2
  76. // with the added data
  77. return Vector2(x + src.x, y + src.y);
  78. }
  79.  
  80. Vector2& Vector2::operator+=(const Vector2& src) {
  81. // Here you want to modify the calling object
  82. x += src.x;
  83. y += src.y;
  84. return *this;
  85. }
  86.  
  87. bool Vector2::operator==(const Vector2& comp) {
  88. // Compare data members
  89. return x == comp.x && y == comp.y;
  90. }
  91.  
  92. bool Vector2::operator<(const Vector2& comp) {
  93. // Compare data members
  94. return x < comp.x && y < comp.y;
  95. }
  96.  
  97. ostream& operator<<(ostream& out, const Vector2& src) {
  98. // Output values. Note that there is no calling object
  99. out << "x: " << src.x << " y: " << src.y;
  100. // Return for chaining
  101. return out;
  102. }
  103.  
  104. istream& operator>>(istream& in, Vector2& src) {
  105. // Input values
  106. in >> src.x >> src.y;
  107. // Return for chaining
  108. return in;
  109. }
  110.  
  111. int main() {
  112. Vector2 v1;
  113. Vector2 v2(1,5);
  114. Vector2 v3(v2);
  115.  
  116. cout << "v1: " << v1 << endl
  117. << "v2: " << v2 << endl
  118. << "v3: " << v3 << endl << endl;
  119.  
  120. Vector2 v4 = v2 + v3;
  121. Vector2 v5;
  122. v5 += v4;
  123.  
  124. bool test1 = v4 == v5;
  125. bool test2 = v1 < v4;
  126.  
  127. cout << "v4: " << v4 << endl
  128. << "v5: " << v5 << endl
  129. << "test1: " << test1 << endl
  130. << "test2: " << test2 << endl << endl;
  131.  
  132. // THIS WILL CALL THE COPY CONSTRUCTOR
  133. Vector2 v6 = v3;
  134. // THIS WILL CALL THE ASSIGNMENT OPERATOR
  135. Vector2 v7;
  136. v7 = v3;
  137.  
  138. cout << "v6: " << v6 << endl
  139. << "v7: " << v7 << endl << endl;
  140.  
  141. system("pause");
  142.  
  143. return 0;
  144. }
  145.  
Success #stdin #stdout #stderr 0s 3456KB
stdin
Standard input is empty
stdout
v1: x: 0 y: 0
v2: x: 1 y: 5
v3: x: 1 y: 5

v4: x: 2 y: 10
v5: x: 2 y: 10
test1: 1
test2: 1

v6: x: 1 y: 5
v7: x: 1 y: 5

stderr
sh: 1: pause: not found