Description
int home has i1 and I2 brothers, small hand a pull i1+i2, add up, double home has D1 and D2 sisters, small hand also pull, d1+d2, also added up. C + + village came the plural (Complex) family, there are two brothers C1 and C2, want to come up with a cumulative, stupid, C1.add (C2). C1 and C2 sad very much, also imagine other children like, small hand a pull, c1+c2, also can add up. This task is given to the magician who is looking at the problem, to help them a busy, so that the plural can also be added with the + number. (You can copy the code of the hint section to start your programming)
Input
Four numbers representing the real and imaginary parts of two imaginary C1 and C2 respectively. If input 2.5 3.1-4.5 0.3, represents two imaginary numbers: C1 (2.5+3.1i), C2 ( -4.5+0.3i)
Output
Result of two imaginary numbers added, format (a,bi) Form
Sample Input
2.5 3.1-4.5 0.3
Sample Output
( -2,3.4i)
/* All rights reserved. * File name: Test.cpp * Chen Dani * Completion Date: July 1, 2015 * Version number: v1.0 */ #include <iostream>using namespace St D;class complex{public: Complex () {}//declares required member or friend functions, including constructors, functions for overloading operators, and functions for displaying results Complex (double r,double i): Real (R), Imag (i) {} Complex operator + (Complex &c2); void display ();p rivate: double real; Double imag;}; Complex Complex::operator + (Complex &c2) { Complex C; C.real=real+c2.real; C.imag=imag+c2.imag; return c;} void Complex::d isplay () { cout<< "(" <<real<< "," <<imag<< "i)" <<ENDL;} The following defines the member function//test with the main () function below, complete the input output int main () { double CR1, Ci1, Cr2, Ci2; cin>>cr1>>ci1>>cr2>>ci2; Complex C1 (CR1, CI1), C2 (Cr2, Ci2), C3; C3=C1+C2; C3.display (); return 0;}
Experience: This problem is very typical and very simple, there is a question to emphasize, the default constructor is generally written or relatively good, write member functions, Ken can define an object that does not require parameters, then you need to use the default constructor. So in order to avoid mistakes, or write insurance. Keep working on it!!!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
17th Week OJ Brush problem--problem A: Implementing the Add operator overload in a complex class "C + + operator overloading"