Complex operators reload 2-member functions

Source: Internet
Author: User

[Cpp]
/*
* Copyright and version Declaration of the program
* Copyright (c) 2012, a student from the computer College of Yantai University
* All rightsreserved.
* File name: x. cpp
* Author: Xu Benxi
* Completion date: January 1, March 24, 2013
* Version: v1.0
* Input description:
 
* Problem description:
 
* Program output:
 
*/
// My code:
# 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 );
Void display ();
Private:
Double real;
Double imag;
};
Complex operator + (Complex & c1, Complex & c2)
{
Return Complex (c1.real + c2.real, c1.imag + c2.imag );
}
Complex operator-(Complex & c1, Complex & c2)
{
Return Complex (c1.real-c2.real, c2.imag-c2.imag );
}
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.imag * 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;
}
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 ();
Int aa;
Cin> aa;
Return 0;

}

/*
* Copyright and version Declaration of the program
* Copyright (c) 2012, a student from the computer College of Yantai University
* All rightsreserved.
* File name: x. cpp
* Author: Xu Benxi
* Completion date: January 1, March 24, 2013
* Version: v1.0
* Input description:

* Problem description:

* Program output:

*/
// My code:
# 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 );
Void display ();
Private:
Double real;
Double imag;
};
Complex operator + (Complex & c1, Complex & c2)
{
Return Complex (c1.real + c2.real, c1.imag + c2.imag );
}
Complex operator-(Complex & c1, Complex & c2)
{
Return Complex (c1.real-c2.real, c2.imag-c2.imag );
}
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.imag * 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;
}
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 ();
Int aa;
Cin> aa;
Return 0;
 
}
 

 

 

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.