OPERATORS

Hello !
Please,U would do me enormous favour if someone would explain me the word OPERATOR in this example!
Many thanks!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// overloading operators example
#include <iostream>
using namespace std;

class CVector {
  public:
    int x,y;
    CVector () {};
    CVector (int a,int b) : x(a), y(b) {}
    CVector operator + (const CVector&);
};

CVector CVector::operator+ (const CVector& param) {
  CVector temp;
  temp.x = x + param.x;
  temp.y = y + param.y;
  return temp;
}

int main () {
  CVector foo (3,1);
  CVector bar (1,2);
  CVector result;
  result = foo + bar;
  cout << result.x << ',' << result.y << '\n';
  return 0;
}
Last edited on
The code above uses operator overloading. A function overloads an operator if the name of the function is operator followed by the operator being overloaded. It is just a normal function with a special name.
http://www.cplusplus.com/doc/tutorial/templates/
Topic archived. No new replies allowed.