C + + operator overloading is non-member function

Source: Internet
Author: User

1#include <iostream>2 using namespacestd;3 classcomplex{4  Public:5Complex (DoubleR=0.0,DoubleI=0.0): Real (R), Imag (i) {}6Friend Complexoperator+ (ConstComplex &c1,ConstComplex &C2);7Friend Complexoperator- (ConstCOMPLEX&AMP;C1,ConstComplex &C2);8Friend Ostream &operator<< (Ostream & out,ConstComplex &c);9 Private:Ten     DoubleReal,imag; One }; AComplexoperator+ (ConstComplex &c1,ConstComplex &C2) - { -     returnComplex (c1.real+c2.real,c1.imag+c2.imag); the } -Complexoperator- (ConstComplex &c1,ConstComplex &C2) - { -     returnComplex (c1.real-c2.real,c1.imag-c2.imag); + } -Ostream &operator<< (Ostream & out,ConstComplex &c) + { A      out<<"("<<c.real<<","<<c.imag<<")"; at     return  out; - } - intMain () - { -Complex C1 (5,4), C2 (2,Ten), C3; -cout<<"c1="<<c1<<Endl; incout<<"c2="<<c2<<Endl; -C3=c1-C2; tocout<<"c3=c1-c2="<<c3<<Endl; +c3=c1+C2; -cout<<"c3=c1+c2="<<c3<<Endl; the     return  0; *}
View Code

C + + operator overloading is non-member function

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.