15th Week OJ Brush problem--problem n:c++ exercises plural class--overloaded operator

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.