+ operator overloading

#include<iostream>
using namespace std;
class complex
{
    int real,img;
public:
    complex()
    {
        real=0;
        img=0;
    }
    complex(int r,int i)
    {
        real=r;
        img=i;
    }
    void show()
    {
        cout<<real<<"+i"<<img<<endl;
    }
    complex operator+(complex x);
};
complex complex:: operator+(complex x)
{
    complex temp;
    temp.real=real+x.real;
    temp.img=img+x.img;
    return temp;
}

main()
{
    complex c1(10,13),c2(34,56),c3;
    c1.show();
    c2.show();
    c3=c1+c2;
    c3.show();
}

1 comment: