其他分享
首页 > 其他分享> > 自考新教材p132

自考新教材p132

作者:互联网

源程序:

#include <iostream>
using namespace std;

class myComplex
{
private:
double real, imag;
public:
myComplex();
myComplex(double r, double i);
myComplex addCom(myComplex c);
void outCom();
};

myComplex::myComplex()
{
real = 0;
imag = 0;
}
myComplex::myComplex(double r, double i)
{
real = r;
imag = i;
}
myComplex myComplex::addCom(myComplex c)
{
return myComplex(this->real + c.real, this->imag + c.imag);
}

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

int main()
{
myComplex c1(1, 2), c2(3, 4), res;
res = c1.addCom(c2); //c1+c2
c1.outCom();
cout << "+";
c2.outCom();
cout << "=";
res.outCom();
cout << endl;
return 1;
}

标签:real,cout,p132,新教材,double,imag,outCom,自考,myComplex
来源: https://www.cnblogs.com/duanqibo/p/15614327.html