Week 8 project 1 (3)

Source: Internet
Author: User

(3) define a fully defined class (it can be released as an independent product and become a "Basic Project" among many projects "). On the basis of (2), this type of class extends the functions of the +,-, *, And/operators so that it can perform operations with double data. If Complex C; double D; C + D and D + C are set, the result is "the plural number that treats D as the real part as D is added together with C". Other-, *, And/operators are similar.

/** Copyright (c) 2014, School of Computer and control engineering, Yantai University * All Rights Reserved. * dood luck * file name: D. CPP * Author: Zhang wanghua * Completion Date: July 15, April 29, 2015 * version number: V1.0 **/# 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 + (const complex & C1, const complex & C2); friend complex operator-(const complex & C1, const complex & C2); friend complex operator * (const complex & C1, const complex & C2 ); friend complex operator/(const complex & C1, const complex & C2); friend complex operator + (const double & B, const complex & C2 ); friend complex operator-(const double & B, const complex & C2); friend complex operator * (const double & B, const complex & C2 ); friend complex operator/(const double & B, const complex & C2); friend complex operator + (const complex & C1, const double & B ); friend complex operator-(const complex & C1, const double & B); friend complex operator * (const complex & C1, const double & B ); friend complex operator/(const complex & C1, const double & B); void display (); Private: Double real; double imag ;}; // The following defines the member function complex operator + (const double & B, const complex & C2) {Complex C (B, 0); Return C + C2 ;} complex operator-(const double & B, const complex & C2) {Complex C (B, 0); Return c-c2;} complex operator * (const double & B, const complex & C2) {Complex C (B, 0); Return C * C2;} complex operator/(const double & B, const complex & C2) {Complex C (B, 0); Return C/C2;} complex operator + (const complex & C1, const double & B) {Complex C (B, 0); Return C1 + C ;} complex operator-(const complex & C1, const double & B) {Complex C (B, 0); Return c1-c;} complex operator * (const complex & C1, const double & B) {Complex C (B, 0); Return C1 * C;} complex operator/(const complex & C1, const double & B) {Complex C (B, 0); Return C1/C;} void complex: Display () {cout <"(" <real <"," <imag <") "<Endl;} complex operator + (const complex & C1, const complex & C2) {complex A;. real = c1.real + c2.real;. imag = c1.imag + c2.imag; return a;} complex operator-(const complex & C1, const complex & C2) {complex A;. real = c1.real-c2.real;. imag = c1.imag-c2.imag; return a;} complex operator * (const complex & C1, const complex & C2) {complex A;. real = c1.real * c2.real + c1.imag * c2.imag;. imag = c1.imag + c2.real + c1.real * c2.imag; return a;} complex operator/(const complex & C1, const complex & C2) {complex A; Double K; k = 1/(c2.real * c2.real + c2.imag * c2.imag);. real = (c1.real * c2.real + c1.imag * c2.imag) * k;. imag = (c1.imag + c2.real-c1.real * c2.imag) * k; return a;} // The main () function int main () {complex C1 (3, 4) used for testing is defined below ), c2 (5,-10), C3; double D = 11; cout <"C1 ="; c1.display (); cout <"C2 ="; c2.display (); cout <"d =" <D <Endl; cout <"the calculation result of the overload operator is as follows:" <Endl; C3 = C1 + C2; cout <"C1 + C2 ="; c3.display (); cout <"C1 + D ="; (C1 + D ). display (); cout <"d + C1 ="; (D + C1 ). display (); C3 = c1-c2; cout <"c1-c2 ="; c3.display (); cout <"c1-d ="; (c1-d ). display (); cout <"d-c1 ="; (d-c1 ). display (); C3 = C1 * C2; cout <"C1 * C2 ="; c3.display (); cout <"C1 * D ="; (C1 * D ). display (); cout <"D * C1 ="; (D * C1 ). display (); C3 = C1/C2; cout <"C1/C2 ="; c3.display (); cout <"C1/D ="; (C1/d ). display (); cout <"d/C1 ="; (D/C1 ). display (); Return 0 ;}
Running result:

Learning Experience:

I have been trying to find a simple method to implement heavy load. If I cannot find it, I should use this method first. I will know it later.


Week 8 project 1 (3)

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.