Evaluate the addition, subtraction, multiplication, and division of any two numbers using the overloaded Operator

Source: Internet
Author: User

# Include <iostream. h>
Class complex // define a complex class
{
PRIVATE:
Double real; // real
Double imag; // virtual part
Public:
Complex () {}// define the constructor
Complex (Double R, double I) // overload Constructor
{
Real = R;
Imag = I;
}
Void display ()
{
Cout <"(" <real <"," <imag <")" <Endl;
}
Complex operator + (complex I2) // overload addition Operators
{
Complex;
A. Real = This-> real + i2.real;
A. imag = This-> imag + i2.imag;
Return;
}
Complex operator-(complex I2) // overload subtraction Operator
{
Complex B;
B. Real = This-> real-i2.real;
B. imag = This-> imag-i2.imag;
Return B;
}
Complex operator * (complex I2) // overload multiplication Operator
{
Complex C;
C. Real = This-> real * i2.real;
C. imag = This-> imag * i2.imag;
Return C;
}
Complex operator/(complex I2) // overload division Operator
{
Complex D;
D. Real = This-> real/i2.real;
D. imag = This-> imag/i2.imag;
Return D;
}

};
Void main ()
{

Int X1, Y1, X2, Y2;

Cin> x1> Y1> X2> Y2;
Complex A, B, C, D, I1 (x1, Y1), I2 (X2, Y2 );
A = I1 + I2; // calculate the sum of C1, C2 and
B = i1-i2; // evaluate the difference between C1 and C2 using the operator after overloading

C = I1 * I2; // calculate the product of C1 and C2 using the overloaded Operator

D = I1/I2; // calculate division of C1 and C2 using the overloaded Operator

Cout <"I1 ="; i1.display ();
Cout <"I2 ="; i2.display ();
Cout <"I1 + I2 ="; A. Display ();
Cout <"i1-i2 ="; B. Display ();
Cout <"I1 * I2 ="; C. Display ();
Cout <"I1/I2 ="; D. Display ();
}

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.