The C++ operator and function overloading for complex number program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: The basic complex number operation to demonstrate the C++ operator and function overloading

To show: How to implement the operator and function overloading in C++ programming

 

// C++ operator and function overloading, the classic simple complex number example

#include <iostream>

using namespace std;

 

class complexnum

{

private:

double p, q;

 

public:

// constructor forming the a + bi

// overloaded function, with two arguments

complexnum(double a, double b)

{

p = a;

q = b;

}

 

// Constructor forming the a + 0i

// overloaded function, with single argument

complexnum(double a)

{

p = a;

q = 0;

}

// returns the real part

double realpart()

{return p; }

// returns the complex part

double imaginarypart()

{ return q; }

 

// addition operation of two complex numbers

// overloaded operator +

complexnum operator+(complexnum a)

{ return complexnum(a.p + p, a.q + q); }

 

// addition a complex number and a double

// overloaded operator +

complexnum operator+(double a)

{ return complexnum(p + a, q);}

 

// subtraction operation of two complex numbers

// overloaded operator -

complexnum operator-(complexnum a)

{ return complexnum(p - a.p, q - a.q); }

 

// addition a complex number and a double

// overloaded operator -

complexnum operator-(double a)

{ return complexnum(p - a, q); }

};

 

// display format for complex number

// overloaded operator <<

ostream& operator<<(ostream& s, complexnum r)

{

// if no imaginary part

if(r.imaginarypart() == 0)

// return real part only

return s<<r.realpart();

// if imaginary part < 0, i.e negative

else if(r.imaginarypart() < 0 )

{

// and if no real part

if(r.realpart() == 0)

// return imaginary part only

return s<<r.imaginarypart()<<"i";

else

// return both real and imaginary parts

return s<<r.realpart()<<r.imaginarypart()<<"i";

}

else

{

// and if no real part

if(r.realpart() == 0)

// return imaginary part only

return s<<r.imaginarypart()<<"i";

else

// return both, real and imaginary parts

return s<<r.realpart()<<" + "<<r.imaginarypart()<<"i";

}

}

 

void main(void)

{

double a,b;

 

// prompt for two numbers

cout<<"Enter 2 numbers: ";

// read and store the two numbers

cin>>a>>b;

 

complexnum r = complexnum(a,b);

cout<<"\nThe complex form is r = "<<r<<endl;

complexnum t = complexnum(7.0);

cout<<"\nGiven t = "<<t<<endl;

 

// addition of complex number and constant

cout<<"\nThen, r + t = "<<(r+t)<<endl;

// subtraction of complex number and complex number

cout<<"\nThen, r - (4 + 2i) = "<<(r - complexnum(4,2))<<endl;

// addition of complex number and complex number

cout<<"\nThen, r + (2 + 2i) = "<<(r + complexnum(2,2))<<endl;

}

 

Output example:

 

Enter 2 numbers: 2.2 4.5

The complex form is r = 2.2 + 4.5i

Given t = 7

Then, r + t = 9.2 + 4.5i

Then, r - (4 + 2i) = -1.8 + 2.5i

Then, r + (2 + 2i) = 4.2 + 6.5i

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index