#include <iostream>
// using namespace std;
using std::cout;
using std::endl;
using std::cin;
using std::istream;
using std::ostream;
class Complex {
public:
Complex(); // default constructor
Complex( double ); // constructor
Complex( double, double ); // constructor
Complex& operator=( const Complex& ); // overload the assignment operator
Complex& operator+=( const Complex& ); // overload operator +=
Complex& operator-=( const Complex& ); // overload operator -=
Complex& operator*=( const Complex& ); // overload operator *=
Complex& operator/=( const Complex& ); // overload operator /=
Complex& operator++(); // overload the prefix self-addition
Complex& operator--(); // overload the prefix self-subtration
Complex operator++( int ); // overload the suffix self-addition
Complex operator--( int ); // overload the suffix self-subtration
void print() const; // the function to output the complex
friend Complex operator+( const Complex&, const Complex& ); // overload the addition operator
friend Complex operator-( const Complex&, const Complex& ); // overload the subtration operator
friend Complex operator*( const Complex&, const Complex& ); // overload the multiplication operator
friend Complex operator/( const Complex&, const Complex& ); // overload the division operator
friend bool operator==( const Complex&, const Complex& ); // overload the equivalent operator
friend bool operator!=( const Complex&, const Complex& ); // overload the unequal operator
friend ostream& operator<<( ostream&, const Complex& );
friend istream& operator>>( istream&, Complex& );
private:
double real; // the real part of the complex
double imag; // the imaginary part of the complex
};
/**
* The default constructor to create a complex without parameter.
* Both the real part and imaginary part are made zero.
*
*/
Complex::Complex() {
real = 0;
imag = 0;
}
/**
* The constructor to create a complex with one paramater to set the real part,
* the imaginary part is made zero.
*/
Complex::Complex( double r ) {
real = r;
imag = 0;
}
/**
* The constructor to create a complex with two paramaters.
*/
Complex::Complex( double r, double i ) {
real = r;
imag = i;
}
/**
* The program is to overload the assignment operator with a paramater.
*
* @param t the complex to assign.
* @return the self object
*/
Complex& Complex::operator=( const Complex& t ) {
real = t.real; // assign the real part of the paramater to the object
imag = t.imag; // assign the imaginary part of the paramater to the object
return *this;
}
/**
* This program is to overload the operator +=,with a complex paramater.
*
* @param t the complex to be added to the objext
* @return self object
*/
Complex& Complex::operator+=( const Complex& t ) {
real = real + t.real; // add the real part of the paramater to the real part of the object
imag = imag + t.imag; // add the imaginary part of the paramater to the imaginary part of the object
return *this;
}
/**
* This program is to overload the operator -=,with a complex paramater.
*
* @param t the complex to be subtracted to the objext
* @return self object
*/
Complex& Complex::operator-=( const Complex& t ) {
real = real - t.real; // subtract the real part
imag = imag - t.imag; // subtract the imaginary part
return *this;
}
/**
* This program is to overload the operator *=,with a complex paramater.
*
* @param t the complex to be multiplied to the objext
* @return self object
*/
Complex& Complex::operator*=( const Complex& t ) {
double temp1; // the first intermediate variable
double temp2; // the second intermediate variable
temp1 = real;
temp2 = imag;
// calculate the result and set it to the object
real = temp1 * t.real - temp2 * t.imag;
imag = temp1 * t.imag + temp2 * t.real;
return *this;
}
/**
* This program is to overload the operator /=,with a complex paramater.
*
* @param t the complex to be divided to the objext
* @return self object
*/
Complex& Complex::operator/=( const Complex& t ) {
double temp1; // the first intermediate variable
double temp2; // the second intermediate variable
temp1 = real;
temp2 = imag;
// calculate the result and set it to the object
real = ( temp1 * t.real + temp2 * t.imag ) / ( t.real * t.real + t.imag * t.imag );
imag = ( temp2 * t.real - temp1 * t.imag ) / ( t.real * t.real + t.imag * t.imag );
return *this;
}
/**
* This program is to overload the operator prefix ++,without paramater.
*
* @return self object
*/
Complex& Complex::operator++() {
real++; // the real part add one
imag++; // the imaginary part add one
return *this;
}
/**
* This program is to overload the operator prefix --,without paramater.
*
* @return self object
*/
Complex& Complex::operator--() {
real--; // the real part minus one
imag--; // the imaginary part minus one
return *this;
}
/**
* This program is to overload the operator suffix ++,without paramater.
*
* @return the complex before add
*/
Complex Complex::operator++( int ) {
Complex temp( real, imag ); // set the real part and imaginary part to another complex object
real++; // the real part add one
imag++; // the imaginary add one
return temp;
}
/**
* This program is to overload the operator suffix --,without paramater.
*
* @return the object before minus
*/
Complex Complex::operator--( int ) {
Complex temp( real, imag ); // set the real part and imaginary part to another complex object
real--; // the real part minus one
imag--; // the imaginary part minus one
return temp;
}
/**
* This program is to overload the operator +,with tow complex paramaters.
*
* @param t the first addend
* @param u the second addend
* @return a complex object
*/
Complex operator+( const Complex& t, const Complex& u ) {
return Complex( t.real + u.real, t.imag + u.imag );
}
/**
* This program is to overload the operator -,with two complex paramaters.
*
* @param t the first subtrahend
* @param u the second subtrahend
* @return a complex object
*/
Complex operator-( const Complex& t, const Complex& u ) {
return Complex( t.real - u.real, t.imag - u.imag );
}
/**
* This program is to overload the operator *,with two complex paramaters.
*
* @param t the first multiplier
* @param u the second multiplier
* @return a complex object
*/
Complex operator*( const Complex& t, const Complex& u ) {
return Complex( t.real * u.real - t.imag * u.imag, t.real * u.imag + t.imag * u.real );
}
/**
* This program is to overload the operator /,with two complex paramaters.
*
* @param t the dividend
* @param u the divisor
* @return a complex object
*/
Complex operator/( const Complex& t, const Complex& u ) {
return Complex( ( t.real * u.real + t.imag * u.imag ) / ( u.real * u.real + u.imag * u.imag ), ( t.imag * u.real - t.real * u.imag ) / ( u.real * u.real + u.imag * u.imag ) );
}
/**
* This program is to overload the operator ==,with two complex paramaters.
* If both the real part and imaginary part are equal,return true;otherwise,
* return false.
*
* @param t the first complex to be compared
* @param u the second complex to be compared
* @return true or false
*/
bool operator==( const Complex& t, const Complex& u ) {
if ( t.real == u.real && t.imag == u.imag ) { // both the real part and imaginary part are equal
return true;
} // end if
else { // not both the real part and imaginary part are equal
return false;
} // end else
}
/**
* This program is to overload the operator !=,with two complex paramaters.
* If not both the real part and imaginary part are equal,return true;otherwise,
* return false.
*
* @param t the first complex to be compared
* @param u the second complex to be compared
* @return true or false
*/
bool operator!=( const Complex& t, const Complex& u ) {
return !( t == u );
}
ostream& operator<<( ostream& os, const Complex& c) {
os << c.real << "+" << c.imag << "i";
return os;
}
istream& operator>>( istream& is, Complex& c) {
is >> c.real >> c.imag;
return is;
}
/**
* This program is to output complex object in a format.
*/
void Complex::print() const {
cout << "(" <<real << "," << imag << ")" << endl;
}
int main()
{
Complex x( 1, 1 );
Complex x1( 1, 1 );
Complex y( 2, 2 );
Complex z( 4, 4 );
// invoke operator==
if( x == x1 ) {
cout << "The same!\n";
}
// invoke operator!=
if ( x != y ) {
cout << "Not same!\n";
}
// invoke operator=
cout << "\nBefore assign,x1=";
x1.print();
x1 = y;
cout << "After assign,x1=";
x1.print();
// invoke operator+=
cout << "\nBefore the operator+=\n";
cout << "x=";
x.print();
cout << "y=";
y.print();
x += y;
cout << "After x+=y\n";
cout << "x=";
x.print();
// invoke operator-=
cout << "\nBefore the operator-=\nx=";
x.print();
cout << "y=";
y.print();
x -= y;
cout << "After x-=y\nx=";
x.print();
// invoke operator*=
cout << "\nBefore the operator*=\nx=";
x.print();
cout << "y=";
y.print();
x *= y;
cout << "After x*=y\nx=";
x.print();
// invoke operator/=
cout << "\nBefore the operator/=\nx=";
x.print();
cout << "y=";
y.print();
x /= y;
cout << "After x/=y\nx=";
x.print();
// invoke operator+
cout << "\nInvoke the operator+\nx=";
x.print();
cout << "y=";
y.print();
z = x + y;
cout << "z=x+y=";
z.print();
// invoke operator-
cout << "\nInvoke the operator-\nx=";
x.print();
cout << "y=";
y.print();
z = x - y;
cout << "z=x-y=";
z.print();
// invoke operator*
cout << "\nInvoke the operator*\nx=";
x.print();
cout << "y=";
y.print();
z = x * y;
cout << "z=x*y=";
z.print();
// invoke operator/
cout << "\nInvoke the operator/\nx=";
x.print();
cout << "y=";
y.print();
z = x / y;
cout << "z=x/y=";
z.print();
// invoke operator prefix ++
cout << "\nInvoke the operator prefix++\nx=";
x.print();
z = ++x;
cout << "After z=++x,\nz=";
z.print();
cout << "x=";
x.print();
// invoke operator prefix --
cout << "\nInvoke the operator prefix--\nx=";
x.print();
z = --x;
cout << "After z=--x,\nz=";
z.print();
cout << "x=";
x.print();
// invoke operator suffix ++
cout << "\nInvoke the operator suffix++\nx=";
x.print();
z = x++;
cout << "After z=x++,\nz=";
z.print();
cout << "x=";
x.print();
// invoke operator suffix --
cout << "\nInvoke the operator suffix--\nx=";
x.print();
z = x--;
cout << "After z=x--,\nz=";
z.print();
cout << "x=";
x.print();
cin >> x;
cout << x << endl;
return 0;
}
C++运算符重载(以复数为例)
最新推荐文章于 2022-02-24 14:41:58 发布