#include <iostream>
using namespace Std;
Class Complex
{
Public
Complex () {real = 0; imag = 0;}
Complex (Double R, double i) {real = r; imag = i;}
Complex operator+ (const Complex &C2);
Complex operator-(const Complex &C2);
Complex operator* (const Complex &C2);
Complex operator/(const Complex &C2);
FriendComplex operator+ (const double &c1,const Complex &C2);
Friend Complex operator-(const double &c1,const Complex &C2);
Friend Complex operator* (const double &c1,const Complex &C2);
Friend Complex operator/(const double &c1,const Complex &C2);
FriendComplex operator+ (const Complex &c1,const double &C2);
Friend Complex operator-(const Complex &c1,const double &C2);
Friend Complex operator* (const Complex &c1,const double &C2);
Friend Complex operator/(const Complex &c1,const double &C2);
void display ();
Private
Double Real;
Double imag;
};
The following defines the member functions
Complex complex::operator+ (const Complex &C2)
{
Return Complex (real + c2.real, imag + c2.imag);
}
Complex complex::operator-(const Complex &C2)
{
Return Complex (Real-c2.real, Imag-c2.imag);
}
Complex complex::operator* (const Complex &C2)
{
Return Complex (Real*c2.real-imag*c2.imag, Real*c2.imag + imag*c2.real);
}
Complex complex::operator/(const Complex &C2)//(AC+BD)/(c^2+d^2) +((bc-ad)/(c^2+d^2))i
{
Return Complex ((real*c2.real + imag*c2.imag)/(C2.real*c2.real + C2.imag*c2.imag), (IMAG*C2.REAL-REAL*C2.IMAG)/(c2.re Al*c2.real + C2.imag*c2.imag));
}
Complex operator+ (const double &C1, const Complex &C2)
{
Return Complex (c1 + c2.real, c2.imag);
}
Complex operator-(const double &C1, const Complex &C2)
{
Return Complex (C1-c2.real, C2.imag);
}
Complex operator* (const double &C1, const Complex &C2)
{
Return Complex (C1 * c2.real, C2.IMAG);
}
Complex operator/(const double &C1, const Complex &C2)
{
Return Complex (C1/c2.real, C2.imag);
}
Complex operator+ (const Complex &C1, const double &C2)
{
Return Complex (c1.real + c2, c1.imag);
}
Complex operator-(const Complex &C1, const double &C2)
{
Return Complex (C1.REAL-C2, C1.imag);
}
Complex operator* (const Complex &C1, const double &C2)
{
Return Complex (c1.real * c2, C1.imag);
}
Complex operator/(const Complex &C1, const double &C2)
{
Return Complex (C1.REAL/C2, C1.imag);
}
void Complex::d isplay ()
{
if (Imag = = 0)
cout << Real << Endl;
else if (Imag < 0)
cout << Real << "-" <<-imag << "i" << Endl;
Else
cout << Real << "+" << imag << "i" << Endl;
}
The following defines the main () function for testing
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 ();
C3 = C1 + 3;
cout << "C1 + 3 =";
C3.display ();
C3 = 5 + c2;
cout << "-5 + c2=";
C3.display ();//-----------------------+
C3 = C1-3;
cout << "c1-3=";
C3.display ();
C3 = -5-c2;
cout << " -5-c2=";
C3.display ();//-----------------------
C3 = C1 * 3;
cout << "C1 * 3 =";
C3.display ();
C3 =-5 * c2;
cout << "-5 * c2=";
C3.display ();//----------------------*
C3 = C1/3;
cout << "c1/3=";
C3.display ();
C3 = -5/c2;
cout << " -5/c2=";
C3.display ();//---------------------/
while (1) {}
return 0;
}
Eighth Week item-1.3