operator overloading

what is operator overloading?please give some examples.
closed account (28poGNh0)
Let's say that you have a remote controle ,You're enjoying changing chanels,sadenly I tok It ,then I overload it ,You know what I can do with it ?
Nothing but making people fly !
well let get back to reallity

say that you have a simple class like this one:

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# include <iostream>
using namespace std;

class cVector
{
    private:
        int x,y;
    public :
        cVector(int a,int b);
        cVector(){;}

        int getX(void);
        int getY(void);
};

cVector::cVector(int a,int b)
{
    x = a;
    y = b;
}

int cVector::getX(void)
{
    return x;
}

int cVector::getY(void)
{
    return y;
}

int main()
{
    cVector cv(2,3),cv2(5,6),cvSum(0,0);

    cout << "V1(" << cv.getX() << " , " << cv.getY() << ")" << endl;
    cout << "V2(" << cv2.getX() << " , " << cv2.getY() << ")" << endl;
    cout << "VSum(" << cvSum.getX() << " , " << cvSum.getY() << ")" << endl;

    /*cvSum = cv + cv2;
    cout << endl << "After addition " << endl;
    cout << "VSum(" << cvSum.getX() << " , " << cvSum.getY() << ")" << endl;*/
    return 0;
}


You compile it everything is alright .Now try to uncomment the comment above
You get an error "no match for operator+" well you should overload the operator +

like this way

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# include <iostream>
using namespace std;

class cVector
{
    private:
        int x,y;
    public :
        cVector(int a,int b);
        cVector(){;}

        int getX(void);
        int getY(void);

        cVector operator+(cVector);
};

cVector::cVector(int a,int b)
{
    x = a;
    y = b;
}

int cVector::getX(void)
{
    return x;
}

int cVector::getY(void)
{
    return y;
}

cVector cVector::operator+(cVector cv)
{
    cVector tmpCv;

    tmpCv.x = x + cv.x;
    tmpCv.y = y + cv.y;

    return tmpCv;
}

int main()
{
    cVector cv(2,3),cv2(5,6),cvSum(0,0);

    cout << "V1(" << cv.getX() << " , " << cv.getY() << ")" << endl;
    cout << "V2(" << cv2.getX() << " , " << cv2.getY() << ")" << endl;
    cout << "VSum(" << cvSum.getX() << " , " << cvSum.getY() << ")" << endl;

    cvSum = cv + cv2;
    cout << endl << "After addition " << endl;
    cout << "VSum(" << cvSum.getX() << " , " << cvSum.getY() << ")" << endl;
    return 0;
}

Complex.h
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <ostream>
using namespace std;

#ifndef COMPLEX_H
#define	COMPLEX_H

class Complex {
public:
    Complex(double r, double i) : re_(r), im_(i) {
    }

    Complex(double r) : re_(r), im_(0) {
    }

    Complex() : re_(0), im_(0) {
    }
    
    friend Complex operator+(const Complex a1, const Complex a2);
    
    friend ostream& operator<<(const ostream& stream, const Complex a);

    double getre() const {
        return re_;
    }

    double getim() const {
        return im_;
    }

private:
    double re_;
    double im_;
};

Complex operator+(const Complex a1, const Complex a2) {
    Complex tmp;
    tmp.re_ = a1.re_ + a2.re_;
    tmp.im_ = a1.im_ + a2.im_;
    return tmp;
}

ostream& operator<<(ostream& stream, const Complex a) {
    stream << "re = " << a.getre() << endl;
    stream << "im = " << a.getim() << endl;
    return stream;
}

#endif	/* COMPLEX_H */ 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "Complex.h"
#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
    Complex a(5.5, 5.5);
    Complex b(10.5, 15.5);
    Complex c;
    
    c = a + b;
    
    cout << c << endl;
    
    return 0;
}


Output:

re = 16
im = 21
Last edited on
Topic archived. No new replies allowed.