1. Engineering
  2. Computer Science
  3. c write a class for complex numbers a complex number...

Question: c write a class for complex numbers a complex number...

Question details

C++

Write a class for complex numbers. A complex number has the form a = bi where a and b are real numbers and i is the square root of -1. We refer to a as the real part and b as the imaginary part of the number. The class should have two data members to represent the real and imaginary numbers; the constructor takes two arguments to set these members. Discuss and implement other appropriate operators for this class. Use the header file complex_number.h and the test program test_complex.cpp.

Header File:

#ifndef _COMPLEX_NUMBER_H_
#define _COMPLEX_NUMBER_N_

#include <iostream>

class complex_number
{
public:
complex_number (double r = 0.0, double i = 0.0);
// postcondition: complex with given components has been created
double get_real_part () const;
// returned: real part of complex number
double get_imag_part () const;
// returned: imaginary part of complex number
private:
double real_part;
double imag_part;
};


complex_number operator + (const complex_number& c1, const complex_number& c2);
// returned: sum of c1 and c2

complex_number operator - (const complex_number& c1, const complex_number& c2);
// returned: difference of c1 and c2

complex_number operator * (const complex_number& c1, const complex_number& c2);
// returned: product of c1 and c2

complex_number operator / (const complex_number& c1, const complex_number& c2);
// precondition: c2 is not the zero of complex numbers
// returned: quotient of c1 and c2

complex_number conjugate (const complex_number& c);
// returned: conjugate of c

double complex_modulus (const complex_number& c);
// returned: modulus of c

bool operator == (const complex_number& c1, const complex_number& c2);
// returned whether c1 and c2 are equal to each other

bool operator != (const complex_number& c1, const complex_number& c2);
// returned whether c1 and c2 are not equal to each other

std::ostream& operator << (std::ostream& output, const complex_number& c);
// postcondition: c has been put on the output stream output
// returned: modified output stream output

#endif

Test Program:

#include <iostream>
#include <cstdlib>
#include "complex_number.h"
#include <cassert>
#include <float.h>

using namespace std;

int main()
{
  complex_number c1(2.3, 4.8);
  assert (c1.get_real_part() == 2.3);
  assert (c1.get_imag_part() == 4.8);

  complex_number c2 (2.6);
  assert (c2.get_real_part() == 2.6);
  assert (c2.get_imag_part() == 0.0);
  
  complex_number c3;
  assert (c3.get_real_part() == 0.0);
  assert (c3.get_imag_part() == 0.0);

  assert (conjugate(c1).get_real_part() == 2.3);
  assert (conjugate(c1).get_imag_part() == -4.8);

  assert (abs (complex_modulus (c1) - 5.322593353) < FLT_EPSILON);
  
  complex_number c4 (2.3, 4.8);
  assert (c1 == c4);

  assert (c1 != c2);

  complex_number c5 (1.3, -4.1);

  complex_number c6 = c4 + c5;
  assert (abs (c6.get_real_part() -  3.6) < FLT_EPSILON);
  assert (abs (c6.get_imag_part() - 0.7) < FLT_EPSILON);

  complex_number c7 = c4 - c5;
  assert (abs (c7.get_real_part() - 1.0) < FLT_EPSILON);
  assert (abs (c7.get_imag_part() - 8.9) < FLT_EPSILON);

  complex_number c8 = c4 * c5;
  assert (abs (c8.get_real_part() - 22.67) < FLT_EPSILON);
  assert (abs (c8.get_imag_part() + 3.19) < FLT_EPSILON);

  complex_number c9 = c4 / c5;
  assert (abs (c9.get_real_part() + 0.902162162) < FLT_EPSILON);
  assert (abs (c9.get_imag_part() - 0.847027027) < FLT_EPSILON);

  cout << "all tests passed - do end zone touchdown dance!" << endl;
  return EXIT_SUCCESS;
}
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution