正在加载图片...
例5.2运算符函数 operator+()将两个 complex类对象相加程序 include <iostream. h> class complext public double real double imag Void main( [complex com1(1.1, 2.2), com2(3.3, 4.4), totall, total 2 tota1 operator+(com1,com2);∥调用运算符函数 operator+() 的第一种方式 cout<< reall= <<totall. rea<< <s imag 1="<<totall imag<<endl total2=com1+com2;调用运算符函数 operator+()的第二种方 cout<< real2=<<total2, reak<< < image="<<total2 imag<<endl; 程序运行结果为: real1=4.4 imagi=6.6 real2=4.4 image=6.69 例 5.2 运算符函数operator+()将两个complex类对象相加程序 #include <iostream.h> class complex{ public: double real; double imag; complex(double r=0,double i=0) {real=r;imag=i;} }; complex operator+(complex co1,complex co2) { complex temp; temp.real=co1.real+co2.real; temp.imag=co1.imag+co2.imag; return temp; } Void main() {complex com1(1.1,2.2),com2(3.3,4.4), total1, total2; total1=operator+(com1,com2); //调用运算符函数operator+() 的第一种方式 cout<<"real1="<<total1.real<<” "<<"imag1="<<total1.imag<<endl; total2=com1+com2;//调用运算符函数operator+()的第二种方 cout<<"real2="<<total2.real<<” "<<"imag2="<<total2.imag<<endl; } 程序运行结果为: real1=4.4 imag1=6.6 real2=4.4 imag2=6.6
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有