请编写程序,处理一个复数与一个double数相加的运算,结果存放在一个double型的变量d1中,输出d1的值,再以复数形式输出此值。定义Complex(复数)类,在成员函数中包含重载类型转换运算符:
operator double(){return real;}
- #include<iostream>
- using namespace std;
- class Complex
- {
- public:
- Complex(){real=0;imag=0;}
- Complex(double r,double i){real=r;imag=i;}
- operator double(){return real;}
- Complex(double r){real=r;imag=0;}
- void display();
- double real;
- double imag;
- };
- void Complex::display()
- {
- cout<<"( "<<real<<" , "<<imag<<" )";
- }
- double operator +(Complex &c1,double a)
- {
- double b;
- b=c1.real+a;
- return b;
- }
- int main()
- {
- Complex c1(3,2),c2;
- double a=2.5,b;
- b=c1+a;
- cout<<b<<endl;
- c2=b;
- c2.display();
- }