請編寫程序,處理一個複數與一個double數相加的運算,結果存放在一個double型的變量d1中,輸出d1的值,再以複數形式輸出此值。定義Complex(複數)類,在成員函數中包含重載類型轉換運算符:ios
operator double(){return real;}ide
- #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();
- }