自考新教材-p156

源程序:

#include <iostream>
#include<string>
using namespace std;

class myComplex
{
private:
double real, imag;
public:
myComplex();
myComplex(double r, double i);
~myComplex() {}
myComplex addCom(myComplex c1); //成员函数,调用对象与参数对象c1相加
void outCom(); //成员函数
void outCom(string s); //成员函数
void changReal(double r); //成员函数

friend myComplex operator+(const myComplex &c1, const myComplex &c2);//c1+c2
friend myComplex operator+(const myComplex &c1, double r); //c1+r
friend myComplex operator+(double r, const myComplex &c1); //r+c1

friend myComplex operator-(const myComplex &c1, const myComplex &c2);//c1-c2
friend myComplex operator-(const myComplex &c1, double r); //c1-r
friend myComplex operator-(double r, const myComplex &c1); //r-c1
myComplex &operator=(const myComplex &c); //成员函数
myComplex &operator=(double); //成员函数
};

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

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

myComplex myComplex::addCom(myComplex c1)
{
return myComplex(this->real + c1.real + c1.real, this->imag + c1.imag);
}

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

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

void myComplex::changReal(double r)
{
this->real = r;
}

myComplex operator+(const myComplex &c1, const myComplex &c2) //c1+c2
{
return myComplex(c1.real + c2.real, c1.imag + c2.imag); //返回一个临时对象
}
myComplex operator+(const myComplex &c1, double r) //c1+r
{
return myComplex(c1.real, c1.imag); //返回一个临时对象
}
myComplex operator+(double r, const myComplex &c1) //r+c1
{
return myComplex(r + c1.real, c1.imag); //返回一个临时对象
}

myComplex operator-(const myComplex &c1, const myComplex &c2) //c1-c2
{
return myComplex(c1.real - c2.real, c1.imag - c2.imag); //返回一个临时对象
}
myComplex operator-(const myComplex &c1, double r) //c1-r
{
return myComplex(c1.real - r, c1.imag); //返回一个临时对象
}
myComplex operator-(double r, const myComplex &c1) //r-c1
{
return myComplex(r - c1.real, -c1.imag); //返回一个临时对象
}

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

int main()
{
myComplex c1(1, 2), c2(3, 4), res;
c1.outCom(" c1");
c2.outCom(" c2");
res = c1 + c2;
res.outCom("执行res=c1+c2-> res");
res = c1.addCom(c2);
res.outCom("执行res=c1.addCom(c2)-> res");
res = c1 + 5;
res.outCom("执行res=c1+5-> res");
res = 5 + c1;
res.outCom("执行res=c1+c2-> res");
res = c1; //调用成员函数必须通过类对象
c1.outCom(" c1");
res.outCom("执行res=c1-> res");
c1.changReal(-3);
res.outCom("执行res=c1.addCom(-3)-> c1");
c1.outCom(" res");
res = c1;
res.outCom("执行res=c1-> res");
res = 7;
res.outCom("执行res=7-> res");
res = 7 + 8;
res.outCom("执行res=(7+8)-> res");
res = c1 = c2;
c1.outCom(" c1");
c2.outCom(" c2");
res.outCom("执行res=c1=c2-> res");
system("pause");
return 0;
}

运行结果:

原文地址:https://www.cnblogs.com/duanqibo/p/12259028.html