Description
Write a program that handles the addition of a complex number to a double, and the result is stored in a double variable d1, outputting the value of D1, and then outputting the value in plural form. Define the complex (plural) class that contains the overloaded type conversion operator in the member function:
Operator double () {return real;}
Input
A plural with a double number
Output
The value of D1 and the plural form of this value
Sample Input
3 42.5
Sample Output
D1=5.50c2= (5.50, 0.00)
/* All rights reserved. * File name: Test.cpp * Chen Dani * Completion date: June 24, 2015 * Version number: v1.0 */ #include <iostream> #include <ioma Nip>using namespace Std;class complex{public: Complex (); Complex (double R); Complex (double r,double i); operator double (); void display ();p rivate: double real; Double imag;}; Complex::complex () {}complex::complex (double R) { real=r;} Complex::complex (double r,double i) { real=r; Imag=i;} Complex::operator double () { return real;} void Complex::d isplay () { cout<< "(" <<real<< "," <<imag<< ")";} int main () { cout<<setiosflags (ios::fixed); Cout<<setprecision (2); Double Real,imag; cin>>real>>imag; Complex C1 (REAL,IMAG); Double D1; cin>>d1; D1=D1+C1; cout<< "d1=" <<d1<<endl; Complex C2=complex (d1); cout<< "c2="; C2.display (); return 0;}
Learning experience: This problem is still relatively good to write, but is the output of the format to pay attention to, the format is a big problem, continue to work, and recently are their own in the settlement of the problem, so very good, continue to work!!!
15th Week OJ Brush problem--problem n:c++ exercises plural class--overloaded operator