[Pin to the top] 8-1 -- computing task 3 between the double data and the plural data that are reloaded by the plural Operator

Source: Internet
Author: User
/** [Project 1] implements Operator Overloading in the plural class * Program Copyright and version description Section * copyright (c) 2012, Yantai University Computer college student * All rightsreserved. * file name: object. CPP * siege cainiao: egg * completion date: October 13, April 18, 2013 * version: V1.0 * input Description: Two plural real and virtual parts * Problem description: solution 3: Based on solution 2, extends the functions of the plus (+), minus (-), minus (*), and minus (/) operators to perform operations on double-type data. Set Complex C; double D; C? D and D? The result of C is the result (where? Is + ). In addition, define the objective operator-and-C is equivalent to 0-c. * Program output: Result of the operation using the operator */# include <iostream> Using STD: cout; Using STD: Endl; Class Complex {public: complex () {real = 0; imag = 0;} complex (Double R, double I) {real = r; imag = I;} friend complex operator + (double & D, complex & C2); friend complex operator-(double & D, complex & C2); friend complex operator * (double & D, complex & C2 ); friend complex operator/(double & D, complex & C2); friend complex operator + (complex & C, double & D); friend complex operator-(complex & C, double & D); friend complex operator * (complex & C, double & D); friend complex operator/(complex & C, double & D ); friend complex operator-(complex & C); void display (); Private: Double real; double imag ;}; // The member function complex operator + (double & D, complex & C) {return complex (D + C. real, C. IMAG);} complex operator-(double & D, complex & C) {return complex (d-c.real, C. IMAG);} complex operator * (double & D, complex & C) {return complex (D * C. real, D * C. IMAG);} complex operator/(double & D, complex & C) {return complex (D * C. real/(c. real * C. real + C. imag * C. IMAG),-D * C. imag/(c. real * C. real + C. imag * C. IMAG);} complex operator-(complex & C) {return complex (0-c.real, 0-c.imag);} complex operator + (complex & C, double & D) {return complex (C. real + D, C. IMAG);} complex operator-(complex & C, double & D) {return complex (C. real-D, C. IMAG);} complex operator * (complex & C, double & D) {return complex (C. real * D, C. imag * D);} complex operator/(complex & C, double & D) {return complex (C. real/D, C. imag/d);} void complex: Display () {cout <"(" <real <"," <imag <"I) "<Endl ;}// the test function int main () {double D = 3.14; Complex C (5,-10), C3; cout <"d =" <D <Endl; cout <"c ="; C. display (); C3 = d + C; cout <"d + c ="; c3.display (); C3 = D-C; cout <"D-c = "; c3.display (); C3 = D * C; cout <"D * c ="; c3.display (); C3 = D/C; cout <"d/C = "; c3.display (); C3 = C + D; cout <"C + D ="; c3.display (); C3 = c-d; cout <"c-d = "; c3.display (); C3 = C * D; cout <"C * D ="; c3.display (); C3 = C/d; cout <"C/D = "; c3.display (); C3 =-C; cout <"-c ="; c3.display (); Return 0 ;}

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.