Back to Code List
c++

Complex Number Handling

This is a sample class and example outputs to work with complex number arithmetic. It handles several basic operators plus a few other functions.

   Main.cpp

#include "Complex.h"
#include <iostream>

int main() {
	std::cout << "Init class (a) with two doubles." << std::endl;
	Complex a(3.5,8.1);
	std::cout << a << std::endl << std::endl;

	std::cout << "Init class (b) with one double." << std::endl;
	Complex b(2.2);
	std::cout << b << std::endl << std::endl;

	std::cout << "Init class (c) with two integers." << std::endl;
	Complex c(5, 11);
	std::cout << c << std::endl << std::endl;
	
	std::cout << "Init class (d) with one integer." << std::endl;
	Complex d(5);
	std::cout << d << std::endl << std::endl;

	std::cout << "Init values into class (a) via user input." << std::endl;
	std::cout << "Input two numbers (separated by a space) to fill in the blanks:"
		<< std::endl << "  __+__i" << std::endl;
	std::cin >> a;
	std::cout << a << std::endl << std::endl;

	std::cout << "Begin arithmetic operations." << std::endl;
	std::cout << "(a + b)   " << (a+b) << std::endl;
	std::cout << "(a - c)   " << (a-c) << std::endl;
	std::cout << "(a * b)   " << (a*b) << std::endl;
	std::cout << "(a / d)   " << (a/d) << std::endl;
	std::cout << "(a += b)  " << (a+=b) << std::endl;
	std::cout << "(a -= 5)  " << (a-=5) << std::endl;
	std::cout << "(a *= -6) " << (a*=-6) << std::endl;
	std::cout << "(a /= c)  " << (a/=c) << std::endl;
	std::cout << std::endl;

	std::cout << "Begin comparison operations." << std::endl;
	std::cout << "(a < b)  " << (a<b) << std::endl;
	std::cout << "(a > b)  " << (a>b) << std::endl;
	std::cout << "(a <= b) " << (a<=b) << std::endl;
	std::cout << "(a >= b) " << (a>=b) << std::endl;
	std::cout << "(a == b) " << (a==b) << std::endl;
	std::cout << "(a != b) " << (a!=b) << std::endl;

	return 0;
}    

   Complex.cpp

#include "Complex.h"

/* Constructors */
	Complex::Complex(double r,double i)
	  :m_real(r)
	  ,m_imaginary(i)
	{};
	Complex::Complex (double r)
	  :m_real(r)
	  ,m_imaginary(0.0)
	{};
	Complex::Complex (int r, int i)
	  :m_real(static_cast<double>(r))
	  ,m_imaginary(static_cast<double>(i))
	{};
	Complex::Complex (int r)
	  :m_real(static_cast<double>(r))
	  ,m_imaginary(0.0)
	{};


/* Comparison */
	bool Complex::operator==(Complex const & rhs) const
	{
		if(m_real != rhs.m_real) return false;
		return m_imaginary==rhs.m_imaginary;
	};
	bool Complex::operator!=(Complex const & rhs) const
	{
		return ! operator==(rhs);
	};
	bool Complex::operator<(Complex const & rhs) const
	{
		return m_real<rhs.m_real
			&& m_imaginary<rhs.m_imaginary;
	};
	bool Complex::operator>(Complex const &rhs) const
	{
		return m_real>rhs.m_real
			&& m_imaginary>rhs.m_imaginary;	
	};
	bool Complex::operator<=(Complex const &rhs) const
	{
		return m_real<=rhs.m_real
			&& m_imaginary<=rhs.m_imaginary;
	};
	bool Complex::operator>=(Complex const &rhs) const
	{
		return m_real>=rhs.m_real
			&& m_imaginary>=rhs.m_imaginary;
	};



/* Arithmetic */
	Complex& Complex::operator+=(Complex const & rhs)
	{
		m_real += rhs.m_real;
		m_imaginary += rhs.m_imaginary;
		return *this;
	};
	Complex& Complex::operator-=(Complex const & rhs)
	{
		m_real -= rhs.m_real;
		m_imaginary -= rhs.m_imaginary;
		return *this;
	};
	Complex& Complex::operator*=(Complex const & rhs)
	{
		m_real = m_real*rhs.m_real - m_imaginary*rhs.m_imaginary;
		m_imaginary = m_real*rhs.m_imaginary + m_imaginary*rhs.m_real;
		return *this;
	};
	Complex& Complex::operator/=(Complex const & rhs)
	{
		m_real = (m_real*rhs.m_real + m_imaginary*rhs.m_imaginary) / (rhs.m_real*rhs.m_real + rhs.m_imaginary*rhs.m_imaginary);
		m_imaginary = (m_imaginary*rhs.m_real - m_real*rhs.m_imaginary) / (rhs.m_real*rhs.m_real + rhs.m_imaginary*rhs.m_imaginary);
		return *this;
	};

	/*Complex Complex::operator+(Complex const & rhs) const
	{
		// ALWAYS do the plus operator this way
		Complex result(*this);
		result += rhs;
		return result;
	};*/
	Complex Complex::operator-(Complex const & rhs) const
	{
		Complex result(*this);
		result -= rhs;
		return result;
	};
	Complex Complex::operator*(Complex const & rhs) const
	{
		Complex result(*this);
		result *= rhs;
		return result;
	};
	Complex Complex::operator/(Complex const & rhs) const
	{
		Complex result(*this);
		result /= rhs;
		return result;
	};

/* Other */
	double Complex::real() const
	{
		return m_real;
	};
	double Complex::imaginary() const
	{
		return m_imaginary;
	};
	void Complex::setVals(double r, double i)
	{
		m_real = r;
		m_imaginary = i;
	};


/* Outside Functions */
	/*Complex& operator+(double lhs,Complex const & rhs)
	{
		return lhs+rhs;
	};*/

	// the above operator+ could also be rewritten to be commutative by 1) removing add in the class and 2) adding this vvvvv
	Complex operator+(Complex lhs, Complex const & rhs)
	{
		return lhs+=rhs;
	};

	std::ostream& operator<<(std::ostream& out, Complex const & c)
	{
		out << c.real() << " + " << c.imaginary() << "i";
		return out;
	};
	std::istream& operator>>(std::istream& in, Complex & c)
	{
		double real;
		double imaginary;
		in >> real;
		in >> imaginary;
		c.setVals(real, imaginary); 
		return in;
	};    

   Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>

class Complex
{
public:
	Complex(double,double);
	Complex(double);
	Complex(int,int);
	Complex(int);

	bool operator==(Complex const &) const;
	bool operator!=(Complex const &) const;
	bool operator<(Complex const &) const;
	bool operator>(Complex const &) const;
	bool operator<=(Complex const &) const;
	bool operator>=(Complex const &) const;

	Complex& operator+=(Complex const &);
	Complex& operator-=(Complex const &);
	Complex& operator*=(Complex const &);
	Complex& operator/=(Complex const &);

	//Complex operator+(Complex const &) const; // const after the member function says that it cannot change the state of the member on which it is called
	Complex operator-(Complex const &) const;
	Complex operator*(Complex const &) const;
	Complex operator/(Complex const &) const;

	double real() const; // will give real portion of number
	double imaginary() const;
	void setVals(double, double);

private:
	double m_real;
	double m_imaginary;

};

// for commutative addition (e.g. b=2.0+a)
//Complex& operator+(double,Complex const &);
Complex operator+(Complex, Complex const &);

std::ostream& operator<<(std::ostream&, Complex const &);
std::istream& operator>>(std::istream&, Complex &);

#endif