"="作为运算符重载,必须是成员函数

源程序:

#include <iostream>
using namespace std;

class myComplex
{
private:
double real,imag;
public:
myComplex();
myComplex(double r,double i);
myComplex &operator=(const myComplex &c);
void outCom();
};

myComplex::myComplex()
{
real=0;
imag=0;
}

myComplex::myComplex(double r,double i)
{
real=r;
imag=i;
}

void myComplex::outCom()
{
cout<<"("<<real<<","<<imag<<")"<<endl;
}

myComplex &myComplex::operator=(const myComplex &c1)
{
this->real=c1.real;
this->imag=c1.imag;
return *this;
}

int main()
{
myComplex c1(1,2),c2(3,4),res;
c1.outCom();
c2.outCom();
res=c1=c2;
res.outCom();
return 1;
}

posted @ 2021-11-29 19:19  bobo哥  阅读(169)  评论(0编辑  收藏  举报