operator overloading

i trying to full fill the code to run according to main function ..
but how can i use operator overloading function according to main fuction ?


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
  #include<iostream>
using namespace std;
class point
{
    int a;
    int b;
    public:
        point(int x,int y)
        {
            a=x;
            b=y;
        }
        int getx()
        {
            return a;

        }
        int gety()
        {
            return b;
        }
        void show()
        {
            cout<<"a=:"<<a<<endl;
            cout<<"b=:"<<b<<endl;

        }
       
};







int main()
{
	point p1(10,20);
	point p2(20,30);
	point p3;
	p3 = -p1;
	p3.show();
	p3 = p1 + p2;
	p3.show();
	p3 = p1 + 5;
	p3.show();
	p3 = p2;
	p3.show();
}
Line 41: You have no default constructor.

but how can i use operator overloading function according to main fuction ?

You have to write the operator overloads.
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    //  Add two points
    point & operator + (const point & p)
    {   a += p.a;
        b += p.b;
        return *this;
    }  
    //  Offset a point by an int
    point & operator + (int off)
    {   a += off;
        b += off;
        return *this;
    }  
    //  Unary minus
    point & operator - ()
    {   a = -a;
        b = -b;
        return *this;
    }

Last edited on
Topic archived. No new replies allowed.