自考新教材-p156

Posted duanqibo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自考新教材-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;
}

运行结果:

技术图片

 

以上是关于自考新教材-p156的主要内容,如果未能解决你的问题,请参考以下文章

自考新教材-p181

自考新教材-p161

自考新教材-p159

自考新教材-p286

自考新教材-p285

自考新教材--p179