双目运算符重载及友元函数重载

来源:互联网 发布:北京交通大学知行校训 编辑:程序博客网 时间:2024/04/26 11:19
#include <iostream>using namespace std;class CComplex{private:double real,virt;public:CComplex(double r=0,double i=0){real=r;virt=i;}CComplex operator+(CComplex num);CComplex operator-(CComplex num);CComplex operator*(CComplex num);void operator()();};CComplex CComplex::operator+(CComplex num){CComplex temp;temp.real=real+num.real;temp.virt=virt+num.virt;return temp;}CComplex CComplex::operator-(CComplex num){CComplex temp;temp.real=real-num.real;temp.virt=virt-num.virt;return temp;}CComplex CComplex::operator*(CComplex num){CComplex temp;temp.real=real*num.real-virt*num.virt;temp.virt=real*num.virt+virt*num.real;return temp;}void CComplex::operator()(){cout << real;if(virt>0) cout << "+" << virt << "i" << endl;if(virt<0) cout << virt << "i" << endl;}int main(){CComplex com1(1,2),com2(3,4),totall1,totall2,totall3;totall1=com1+com2;// totall1=com1.operator+(com2);totall2=com1-com2;// totall2=com1.operator-(com2);totall3=com1*com2;// totall3=com1.operator*(com2);cout << "输出复数com1和com2" << endl;cout << "com1=" ;com1();cout << "com2=";com2();cout << endl << "---复数运算---" << endl;cout << "com1+com2=";totall1();cout << endl;cout << "com1-com2=";totall2();cout << endl;cout << "com1*com2=";totall3();cout << endl;}


一定不要忘记 using namespace std;

要是忘了,就会出现“cout”: 未声明的标识符。、

现在还不明白的问题: 现在的+  -  * ()都已经重载了。当运算com1+com2时候,把+看成一个函数,后面是com2,所以把com2(3,4)传递到类num。

num.real=3, num.virt=4.为什么real=1.virt=2?


友元单目运算:

#include<iostream>using namespace std;class CTri{int a,b;public:CTri(int x=0,int y=0){a=x;b=y;}friend CTri operator -(CTri obj);void show();};CTri operator - (CTri obj){obj.a=-obj.a;obj.b=-obj.b;return obj;}void CTri::show(){cout << "a=" << a << endl;cout << "b=" << b << endl;} void main(){CTri t1(2,15),t2;cout << "输出对象t1的数据成员:" << endl;t1.show();cout << "输出对象t2的数据成员:" << endl;t2.show();t2=-t1;cout << "输出对象t1取反后的数据成员:" << endl;t2.show();}


#include<iostream>using namespace std;class CComplex{private:double real,virt;public:CComplex(double r=0,double i=0 ){real=r;virt=i;}friend CComplex operator +(CComplex num1, CComplex num2);friend CComplex operator -(CComplex num1, CComplex num2);friend CComplex operator *(CComplex num1, CComplex num2);void operator()();};CComplex operator +(CComplex num1,CComplex num2){CComplex temp;temp.real=num1.real+num2.real;temp.virt=num1.virt+num2.virt;return temp;}CComplex operator-(CComplex num1,CComplex num2){CComplex temp;temp.real=num1.real-num2.real;temp.virt=num1.virt-num2.virt;return temp;}CComplex operator*(CComplex num1,CComplex num2){CComplex temp;temp.real=num1.real*num2.real-num1.virt*num2.virt;temp.virt=num1.real*num2.virt+num1.virt*num2.real;return temp;}void CComplex::operator()(){cout << real;if(virt>0) cout << "+" << virt << "i" << endl;if(virt<0) cout << virt << "i" << endl;}void main(){CComplex com1(1.5,2.4),com2(3.1,4.8),totall1,totall2,totall3;totall1=com1+com2;// totall1=operator+(com1,com2);totall2=com1-com2;// totall2=operator-(com1,com2);totall3=com1*com2;// totall2=operator*(com1,com2);cout << "输出复数 com1和com2:" << endl;cout << "com1=" ;com1();cout << "com2=";com2();cout << "---------附属运算---------" << endl;cout << "com1+com2=";totall1();cout << endl;cout << "com1-com2=";totall2();cout << endl;cout << "com1*com2=";totall3();cout << endl;}


原创粉丝点击