#include <iostream>
using namespace std;
class myComplex //复数类
{
private:
double real,imag;
public:
myComplex();
myComplex(double r,double i);
friend class oper;
};
myComplex::myComplex()
{
real=0;
imag=0;
}
myComplex::myComplex(double r,double i)
{
real=r;
imag=i;
}
class oper //操作类
{
public:
myComplex addCom(myComplex c1,myComplex c2); //c1,c2都是对象,即复数
void outCom(myComplex c);
};
myComplex oper::addCom(myComplex c1,myComplex c2)
{
return myComplex(c1.real+c2.real,c1.imag+c2.imag);
}
void oper::outCom(myComplex c)
{
cout<<"("<<c.real<<","<<c.imag<<")";
}
int main()
{
myComplex c1(1,2),c2(3,-4),result;
oper o;
result=o.addCom(c1,c2); //友元类的体现
o.outCom(c1);
cout<<"+";
o.outCom(c2);
cout<<"=";
o.outCom(result);
cout<<endl;
return 1;
}
结果:
标签:oper,real,imag,c++,c2,c1,友元类,myComplex From: https://www.cnblogs.com/duanqibo/p/16906757.html