Extended operator functions of week 8 computer task project 1-3

Source: Internet
Author: User
01./* 02 .* Program Copyright and version description section 03. * copyright (c) 2013, Yantai University Computer college student 04. * All rightsreserved. 5. * file name: complex. CPP 06. * Author: Zhao guanzhe 07. * Completion Date: April 8, April 19, 2013. * version: V1.0 09. * input Description: 10. * Problem description: 11. */# include <iostream> using namespace STD; Class Complex {public: complex () {real = 0; imag = 0;} complex (Double R, double I) {real = r; imag = I;} friend complex operator + (complex & C1, complex & C2); friend complex operator-(complex & C1, complex & C2 ); friend complex operator * (complex & C1, complex & C2); friend complex operator/(complex & C1, complex & C2); friend complex operator + (const double &, complex & C2); friend complex operator + (complex & C1, const double & A); friend complex operator-(complex & C1); void display (); Private: Double real; double imag;}; complex operator + (complex & C1, complex & C2) {Complex C; C. real = c1.real + c2.real; C. imag = c1.imag + c2.imag; return C;} complex operator-(complex & C1, complex & C2) {Complex C; C. real = c1.real-c2.real; C. imag = c1.imag-c2.imag; return C;} complex operator * (complex & C1, complex & C2) {Complex C; C. real = c1.real * c2.real-c1.imag * c2.imag; C. imag = c1.imag * c2.real + c1.real * c2.imag; return C;} complex operator/(complex & C1, complex & C2) {Complex C; C. real = (c1.real * c2.real + c1.imag * c2.imag)/(c2.real * c2.real + c2.imag * c2.imag); C. imag = (c1.imag * c2.real-c1.real * c2.imag)/(c2.real * c2.real + c2.imag * c2.imag); Return C;} complex operator + (const double & A, complex & C2) {return complex (a + c2.real, c2.imag);} complex operator + (complex & C1, const double & A) {return complex (c1.real + A, c1.imag );} complex operator-(complex & C1) {return complex (-c1.real,-c1.imag);} void complex: Display () {cout <"(" <real <"," <imag <"I)" <Endl;} int main () {complex C1 (3, 4 ), c2 (5,-10), C3; cout <"C1 ="; c1.display (); cout <"C2 ="; c2.display (); C3 = C1 + C2; cout <"C1 + C2 ="; c3.display (); C3 = c1-c2; cout <"c1-c2 ="; c3.display (); C3 = C1 * C2; cout <"C1 * C2 ="; c3.display (); C3 = C1/C2; cout <"C1/C2 ="; c3.display (); C3 = 1.66 + C2; cout <"1.66 + C2 ="; c3.display (); C3 = C1 + 1.57; cout <"C1 + 1.66 ="; c3.display (); cout <"C3 =-C1 ="; C3 =-C1; c3.display (); Return 0 ;}

Running result;

Related Article

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.