C + + complex class pair division operator/Overload

Source: Internet
Author: User

C8-1 Complex Subtraction (100.0/100.0 points) Title Description

Ask for the subtraction of two complex numbers.

Enter a description

First row two double type number, representing the real part of the first complex

Second row two double type number, representing the real part of the second complex


Output description

The output computes the subtraction of two complex numbers in turn, one row of results

The output complex number first outputs the real part, the space, then the imaginary 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 namespacestd;7 8 classcomplex{9  Public:TenComplex (DoubleR =0.0,Doublei =0.0): Real (R), Imag (i) {}; OneComplexoperator+ (ConstComplex &c2)Const; AComplexoperator- (ConstComplex &c2)Const; -  -     /*implement the following three functions*/ theComplexoperator* (ConstComplex &c2)Const; -Complexoperator/ (ConstComplex &c2)Const; -Friend Ostream &operator<< (Ostream & out,ConstComplex &c); -  + Private: -     DoubleReal; +     Doubleimag; A }; at  -Complex Complex::operator+ (ConstComplex &c2)Const { -     returnComplex (real + c2.real, Imag +c2.imag); - } -  -Complex Complex::operator- (ConstComplex &c2)Const { in     returnComplex (Real-c2.real, Imag-c2.imag); - } to  +Complex Complex::operator* (ConstComplex &c2)Const - { the     returnComplex (Real*c2.real-imag*c2.imag, Real*c2.imag + imag*c2.real); * } $ Panax NotoginsengComplex Complex::operator/ (ConstComplex &c2)Const - { the     if(C2.imag = =0) +         returnComplex (Real/c2.real, Imag/c2.real); A     Else the         return(* This) *complex (C2.real,-c2.imag)/Complex (C2.real*c2.real + C2.imag*c2.imag,0); + } -  $Ostream &operator<< (Ostream & out,ConstComplex &c) $ { -      out<< C.real <<" "<< C.imag <<Endl; -     return  out; the } - Wuyi intMain () { the     DoubleReal, imag; -CIN >> Real >>imag; Wu Complex C1 (real, imag); -CIN >> Real >>imag; About Complex C2 (real, imag); $cout << C1 +C2; -cout << C1-C2; -cout << C1 *C2; -cout << C1/C2; A}

Is the overload of the C + + operator.

There are two places to be aware of:

1, in the overload of <<, note to return out, so that you can achieve the cascade output of << (multiple parallel);

2, the overload of the/, pay attention to return (*this) *complex (c2.real,-c2.imag)/Complex (c2.real*c2.real + c2.imag*c2.imag, 0); This sentence will continue to call this overloaded function itself! It itself is the overload of the/, and you use it here again/, so it will be handed down! Therefore must add return Complex (Real/c2.real, imag/c2.real); Allow recursion to be attributed to trivial situations (in fact only one layer is recursive).




C + + complex class pair division operator/Overload

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.