C++ question help please!! Exercise 2: You need to write an interface file and a
ID: 3567589 • Letter: C
Question
C++ question
help please!!
Exercise 2:
You need to write an interface file and an implementation file for the class definition. Also you need to write a test driver program for testing.
Define a class for complex numbers. A complex number is a number of the form
a + b * i
where, for our purposes, a and b are numbers of type double, and i is a number that represents the quantity ?-1 (square root of -1). Represent a complex number as two values of type double. Name the member variables real and imaginary. (The variable fro the number that is multiplied by i is the one called imaginary.) Call the class Complex.
Include a constructor with two parameters of type double that can be used to set the member variables of an object to any values. Also include a constructor that has only a single parameter of type double; call this parameter real_part and define the constructor so that the object will be initialized to real_part+0*i. Also include a default constructor that initializes an object to 0 (that is, to 0+0*i). Overload all of the following operators so that they correctly apply to the type Complex: ==, +, -, *, >>, and <<. You should write a test program to test your class.
Hint: To add or subtract two complex numbers, you add or subtract the two member variables of type double. The product of two complex numbers is given by the following formula:
(a + b * i) * (c + d * i) == (a * c
Explanation / Answer
//file: complex.h
#ifndef _COMPLEX_H
#define _COMPLEX_H
#include <cmath>
#include <iostream>
using namespace std;
class complex
{
public:
complex (double r = 0, double i = 0): re (r), im (i) { }
double real () const { return re; }
double imag () const { return im; }
private:
double re, im;
friend double real (const complex&) ;
friend double imag (const complex&) ;
friend complex operator + (const complex&, const
complex&);
friend complex operator - (const complex&,
const complex&);
friend complex operator * (const complex&,
const complex&);
friend complex operator / (const complex&,
const complex&);
friend bool operator == (const complex&,
const complex&);
friend bool operator != (const complex&,
const complex&);
friend complex polar (double, double);
friend istream& operator>> (istream&, complex&);
friend ostream& operator<< (ostream&, const complex&);
};
double norm (const complex& x);
#endif
-----------------------------------------------------------------------------
//file: complex.cpp
//This is the implementation file for members of the
//class complex. The class interface is given in complex.h
#include "complex.h"
#include <iostream>
ostream& operator<< (ostream& o, const complex& c)
{
o << "(" << c.re << ", " << c.im << ")";
return o;
}
//limited checking of format is done here.
istream& operator>> (istream& ins, complex& z)
{
double r, i;
char ch;
ins >> ch;
if ('(' != ch) //if the complex number isn't in
//required form, complain and exit.
{
cout << " Bad complex form: found "
<< ch << ", need(for complex input; "
<< "A complex must be of the form (re, im) ";
exit(1);
}
//We have '(' -- now get the real part
ins >> r;
//and get the comma
ins >>ch;
if (',' != ch) //complex number must have a comma next,
//if not, complain and exit.
{
cout << " Bad complex form: found "
<< ch << ", need comma for complex input; "
<< "A complex must be of the form (re, im) ";
exit(1);
}
//now get the imaginary part
ins >> i;
//and get the close parenthesis
ins >> ch;
if(')' != ch)//complex number must have a ')' last,
//If not, complain and exit.
{
cout << " Bad complex form: found "
<< ch << ", need)for complex input; "
<< "A complex must be of the form (re, im) ";
exit(1);
}
z = complex(r, i);
return ins;
}
double imag (const complex& x)
{
return x.imag ();
}
double real (const complex& x)
{
return x.real ();
}
complex operator + (const complex& x, const complex& y)
{
return complex (real (x) + real (y), imag (x) + imag (y));
}
complex operator - (const complex& x, const complex& y)
{
return complex (real (x) - real (y), imag (x) - imag (y));
}
complex operator * (const complex& x, const complex& y)
{
return complex (real (x) * real (y) - imag (x) * imag (y),
real (x) * imag (y) + imag (x) * real
(y));
}
complex operator / (const complex& x, double y)
{
return complex (real (x) / y, imag (x) / y);
}
bool operator == (const complex& x, const complex& y)
{
return real (x) == real (y) && imag (x) == imag (y);
}
bool operator != (const complex& x, const complex& y)
{
return real (x) != real (y) || imag (x) != imag (y);
}
double abs (const complex& x)
{
return sqrt(norm(x));
}
complex conj (const complex& x)
{
return complex (real (x), -imag (x));
}
double norm (const complex& x)
{
return real (x) * real (x) + imag (x) * imag (x);
}
//Divide overloading: There is a possible bug here.
//The usual tool for complex division, num/den =
//num*conj(den)*(1/(den * conj(den)), causes an infinite
//recursion.
//Exercise: How and why?
complex operator / (const complex& num, const complex& den)
{
return(num * conj(den) * (1/norm(den)));
}
//file: tstcmplx.cpp
//To test complex.h and complex.cpp class, members and
//friends
#include "complex.h"
#include "cmath"
using namespace std;
//compile command: g++ testcomplex.cpp complex-io.cpp
int main()
{
// test constructors
complex x, y(3), z(-3.2, 2.1);
cout <<"x = " << x << " y = " << y
<< " z = " << z << endl << endl;
x = complex(3, -4);
cout << "testing members and support functions as "
<< " well as output operator: "
<< "complex number x = " << x << endl
<< "real part: " << x.real() << endl
<< "real part from friend real(x): "
<< real(x) << endl
<< "imaginary part: " << x.imag() << endl
<< "imaginary part from friend imag(x) : "
<< imag(x) << endl
<< "norm: " << norm(x) << endl << endl;
cout << "test complex arithmetic and output"
<< " routines: ";
y = complex (1, -1);
cout << "x = " << x << " y = " << y
<< " z = " << z << endl << endl;
z = x + y;
cout << "z = x + y = " << z << endl;
z = x * y;
cout << "z = x * y = " << z << endl;
z = x - y;
cout << "z = x - y = " << z << endl;
z = x / y;
cout << "z = x / y = " << z << endl << endl;
//test of automatic conversion double -> complex by the
//constructor.
double d(2.0);
cout << "d: " << d << " x: " << x <<endl;
cout << "x+d: " ;
z = x + d;
cout << z << endl;
z = x - d;
cout << "x-d: " ;
cout << z << endl;
z = x * d;
cout << "x*d: ";
cout << z << endl;
z = x / d;
cout << "x/d: " ;
cout << z << endl;
z = d + x;
cout << "d+x: " ;
cout << z << endl;
z = d - x;
cout << "d-x: " ;
cout << z << endl;
z = d * x;
cout << "d*x: " ;;
cout << z << endl;
z = d / x;
cout << "d/x: " ;;
cout << z << endl;
//test whether double/complex and complex/complex
//give same result:
complex two(2,0);
cout << "two/x: ";
cout << two/x << endl;
cout << " Getting data from standard input: ";
cin >> x >> y;
cout << "data read is: x = " << x
<< " y = " << y << endl << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.