The C ++ complex class is overloaded with Division operators and operators.

Source: Internet
Author: User

The C ++ complex class is overloaded with Division operators and operators.
C8-1 plural addition, subtraction, multiplication, division (100.0/100.0 points)Description

Calculate the addition, subtraction, multiplication, and division of two plural numbers.

 

Input description

The number of two double types in the first row, indicating the real part of the first complex.

The number of two double types in the second row, indicating the real part of the second plural.


Output description

The output calculates the addition, subtraction, multiplication, division, and result of one row.

The output complex number first outputs the real part, space, and then virtual part,


Sample Input

1 13 -1


Sample output

4 0-2 24 20.2 0.4




1 # include <cstdio> 2 # include <cstring> 3 # include <iostream> 4 # include <algorithm> 5 6 using namespace std; 7 8 class Complex {9 public: 10 Complex (double r = 0.0, double I = 0.0): real (r), imag (I) {}; 11 Complex operator + (const Complex & c2) const; 12 Complex operator-(const Complex & c2) const; 13 14/* implement the following three functions */15 Complex operator * (const Complex & c2) const; 16 Complex operator/(const Complex & c2) const; 17 friend ostream & operator <(ostream & out, const Complex & c); 18 19 private: 20 double real; 21 double imag; 22}; 23 24 Complex: operator + (const Complex & c2) const {25 return Complex (real + c2.real, imag + c2.imag ); 26} 27 28 Complex: operator-(const Complex & c2) const {29 return Complex (real-c2.real, imag-c2.imag); 30} 31 32 Complex :: operator * (const Complex & c2) const33 {34 return Complex (real * c2.real-imag * c2.imag, real * c2.imag + imag * c2.real); 35} 36 37 Complex :: operator/(const Complex & c2) const38 {39 if (c2.imag = 0) 40 return Complex (real/c2.real, imag/c2.real); 41 else42 return (* this) * Complex (c2.real,-c2.imag)/Complex (c2.real * c2.real + c2.imag * c2.imag, 0); 43} 44 45 ostream & operator <(ostream & out, const Complex & c) 46 {47 out <c. real <"" <c. imag <endl; 48 return out; 49} 50 51 int main () {52 double real, imag; 53 cin> real> imag; 54 Complex c1 (real, imag); 55 cin> real> imag; 56 Complex c2 (real, imag); 57 cout <c1 + c2; 58 cout <c1-c2; 59 cout <c1 * c2; 60 cout <c1/c2; 61}

It is the overload of operators in C ++.

Note the following two points:

1. In the <overload, pay attention to the returned out, so that the cascade output of <can be achieved (multiple parallel operations );

2. During/overloading, pay attention to return (* this) * Complex (c2.real,-c2.imag)/Complex (c2.real * c2.real + c2.imag * c2.imag, 0 ); this statement will continue to call the overload function itself! It is an overload of/, and you have used/here, so it will recursion! Therefore, return Complex (real/c2.real, imag/c2.real) must be added to ensure that recursion is attributed to the ordinary situation (actually only one layer is recursive ).




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.