自考新教材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;
}

上一篇:实验一


下一篇:实验一:类与对象