码迷,mamicode.com
首页 > 其他好文 > 详细

自考新教材-p156

时间:2020-02-04 14:11:12      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:源程序   临时对象   技术   mamicode   info   inf   hang   mic   turn   

源程序:

#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("\t\t\tc1");
c2.outCom("\t\t\tc2");
res = c1 + c2;
res.outCom("执行res=c1+c2->\tres");
res = c1.addCom(c2);
res.outCom("执行res=c1.addCom(c2)->\tres");
res = c1 + 5;
res.outCom("执行res=c1+5->\tres");
res = 5 + c1;
res.outCom("执行res=c1+c2->\tres");
res = c1; //调用成员函数必须通过类对象
c1.outCom("\t\t\tc1");
res.outCom("执行res=c1->\tres");
c1.changReal(-3);
res.outCom("执行res=c1.addCom(-3)->\tc1");
c1.outCom("\t\t\tres");
res = c1;
res.outCom("执行res=c1->\tres");
res = 7;
res.outCom("执行res=7->\tres");
res = 7 + 8;
res.outCom("执行res=(7+8)->\tres");
res = c1 = c2;
c1.outCom("\t\t\tc1");
c2.outCom("\t\t\tc2");
res.outCom("执行res=c1=c2->\tres");
system("pause");
return 0;
}

运行结果:

技术图片

 

自考新教材-p156

标签:源程序   临时对象   技术   mamicode   info   inf   hang   mic   turn   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!