C + + declares and implements a plural class

Source: Internet
Author: User

<pre name= "code" class= "CPP" >/**************************************************************************** Complex.hpp:Copyright (c) Bit Software, Inc., All rights reserved. Purpose: Declaring and implementing a complex number class difficulty: **author:xxxcreated time:2015-4-26******************************************************** /#ifndef complex_hpp_included#define Complex_hpp_included#include <iostream >using namespace Std;class complex{public://with default values for constructors complex (double real = 0, double image = 0): _real (Real), _image (IM Age) {cout<< "complex (double real = 0, double image = 0)" &LT;&LT;ENDL; destructor ~complex () {cout<< "~complex ()" &LT;&LT;ENDL;} Copy constructor complex (const complex& D): _image (D._image), _real (d._real) {cout<< "complex (const complex& D ) "&LT;&LT;ENDL;} Assignment operator overloading//Thinking why does the operator= assignment function require a date& return value?? complex& operator= (const complex& D) {cout<< "operator= (const complex& D)" <<endl;if (This! = & Amp;d) {This->_reAl = D._real;this->_image = D._image;} return *this;} Fetch address operator overload complex* operator& () {cout<< "operator& ()" <<endl;return this; The const-Modified fetch address operator Overloads Const complex* operator& () const{cout<< "operator& () const" <<endl;return this;} void display () {cout<< "real:" <<_real<< "--image:" &LT;&LT;_IMAGE&LT;&LT;ENDL&LT;&LT;ENDL;}        Complex operator+ (const complex& c) {cout<< "operator+ (const complex& c)" <<endl; Return complex (_real+c._real,_image+c._image);}        Complex operator-(const complex& c) {cout<< "operator-(const complex& c)" <<endl; Return complex (_real-c._real,_image-c._image);}        Complex operator* (const complex& c) {cout<< "operator* (const complex& c)" <<endl; Return complex (_real*c._real-_image*c._image,c._image*_real+_image*c._real);}        Complex operator/(const complex& c) {cout<< "operator/(const complex& c)" <<endl; RetUrn Complex<span style= "font-family:arial, Helvetica, Sans-serif;" > (_real/c._real+_image*c._image)/(C,_real*c._real+c._image*c._image), (_image*c._real-_real*c._image)/(c,_ real*c._real+c._image*c._image)) </span><span style= "font-family:arial, Helvetica, Sans-serif;" >; </span>}private:double _real;double _image;}; #endif//complex_hpp_included

Main function:

#include "complex.hpp" int main () {    //test_complex1 ();    cout << "**********************" << Endl;    Complex A (1.1,2.2);    Complex B (3.3,4.4);    Complex C;    c = a + B;    C.display ();    c = A-B;    C.display ();    c = A * b;    C.display ();    c = A/b;    C.display ();    return 0;}



C + + declares and implements a plural class

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.