The goal of the project is to write an implementation for a complex number class. The class will include:
1) Three constructor functions to initialize complex numbers with default values or specified real and imaginary parts.
2) Overloaded operators for addition, subtraction, multiplication, and division of complex numbers. Division will throw an exception if the divisor is 0.
3) Accessor functions to return the real and imaginary parts of a complex number.
4) Other member functions to calculate the conjugate, modulus, and argument of a complex number. Argument will throw an exception if both parts are 0.
5) A friend function to calculate powers of a complex number.
The interface and partial implementation are provided, with
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Please help! The goal of this project is to write the interface and im.pdf
1. Please help! The goal of this project is to write the interface and implementation for the
complex number class. The public interface for the complex number class should provide
the following member functions: constructors - There will be three versions of the
constructor function: THE INSTRUCTIONS ARE BELOW (IN BOLD):
a. Complex() : the default constructor sets both real and imag, pri- vate member variables, to 0.
The default constructor is implemented for you.
b. Complex(double r) : the second constructor sets real to r, the double, and imag to 0.
c. Complex(double r, double i) : the third constructor sets real to r and imag to i.
operators: +, -, / and * will be overloaded as non-member friend functions. For the / operator,
when both the real and imaginary parts of the divisor are 0, an exception occurs. The operator
must throw an integer, -1. The << operator has already been implemented for you. Make sure
you understand how it works. Also, the + non-member operator has been implemented for you.
accessors: implement two accessor member functions:
a. getReal(): this function returns the real part of the complex number. This member function has
been implemented for you.
b. getImag() : this function returns the imaginary part of the complex number.
Other member functions :
a. conjugate() : this member function returns a complex number that is the conjugate of the
object used to invoke it. This function has been implemented for you.
b. modulus() : this member function returns the magnitude of the complex number. The square
root function, sqrt, is defined in the math library.
c. argument() : this member function returns the polar angle of a complex number. Given a
complex number z = x + iy, it returns arctan(y/x). Note the arctan function, atan2(y,x), is already
defined in the math library. However, the function must throw an integer, -2, when an exception
occurs. An exception occurs when both y and x are 0.
Define the function pow(const Complex& z, int n) as a friend function of the Complex class. The
function returns a new complex number equivalent to Zn.
Please complete the below STARTER functions (some variables need to be defined) ALL
BOLDED SECTIONS CORRESPOND WITH ONE ANOTHER:
complex.h:
/**
* public interface for the complex number class.
2. * File: complex.h
* </pre>
*/
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
/* YOUR TASK HERE IS TO GIVE THE COMPLETE DEFINITION OF
THE class Complex. BE SURE TO INCLUDE A DESCRIPTION OF EACH OF THE
FUNCTIONS.
YOU WILL PROVIDE ONLY THE PUBLIC INTERFACE OF THE MEMBER
AND FRIEND FUNCTIONS, NOT THEIR DEFINITIONS.
THE MEMBER AND FRIEND FUNCTIONS WILL BE DEFINED IN THE
IMPLEMENTATION FILE.
DEFINE THE CLASS BELOW.
*/
#endif
complex.cpp:
/**
* Implementation for the complex class
* File: complex.cpp
* </pre>
*/
#include <cmath>
#include <cstdlib>
#include "complex.h"
/* SOME FUNCTION HAVE BEEN IMPLEMENTED. IMPLEMENT
ALL OTHER FUNCTIONS.
*/
Complex::Complex()
{
real = 0.0;
imag = 0.0;
}
double Complex::getReal() const
{
4. else
out<<"-i";
}
else
{
if (z.imag != 1)
out<<"+"<<z.imag<<"i";
else
out<<"+i";
}
return out;
}
completest.cpp
/**
* A program to test the complex class implementation
* <pre>
* File: complextester.cpp
* </pre>
*/
#include <iostream>
#include "complex.h"
#include <cstdlib>
using namespace std;
int main(int argc, char **argv)
{
Complex z0(3.6,4.8);
Complex z1(4,-2);
Complex z2(-4,2);
Complex z3(-4,-3);
Complex z4(3,-4);
Complex z5;
Complex z6;
Complex z7;
double ang;
cout<<"z0 = "<<z0<<" and re(z0) = "<<z0.getReal()<<" and "
<<"im(z0) = "<<z0.getImag()<<"."<<endl;
cout<<"z1 = "<<z1<<endl;
cout<<"z2 = "<<z2<<endl;
cout<<"z3 = "<<z3<<endl;
cout<<"z4 = "<<z4<<endl;
5. cout<<"z2 x z3 = "<<z2*z3<<endl;
cout<<"z1 + z2 = "<<z1+z2<<endl;
cout<<"z3 = "<<z3<<endl;
cout<<"z4 = "<<z4<<endl;
cout<<"z3-z4 = "<<z3-z4<<endl;
cout<<"((z2+z3)x(z3-z4)) = ";
cout<<(z2+z3)*(z3-z4)<<endl;
cout<<"z1^3 = "<<pow(z1,3)<<endl;
cout<<"1/z2^2 = "<<pow(z2,-2)<<endl;
cout<<"z3^0 = "<<pow(z3,0)<<endl;
try
{
cout<<"((z2+z3)x(z3-z4))/z3 = ";
z6 = ((z2+z3)*(z3-z4))/z3;
cout<<z6<<endl;
cout<<"z3 / z4 = "<<(z3/z4)<<endl;
cout<<"Conj(z4) = "<<z4.conjugate()<<endl;
cout<<"The real part of the conjugate of z4 is "<<(z4.conjugate()).getReal()<<endl;
cout<<"The imaginary part of the conjugate of z4 is "<<(z4.conjugate()).getImag()<<endl;
cout<<"The argument of the conjugate of z4 is "<<(z4.conjugate()).argument()<<"."<<endl;
cout<<"The modulus of the conjugate of z4 is "<<(z4.conjugate()).modulus()<<"."<<endl;
cout<<"(z3*conj(z4))/(z4*conj(z4))= ";
cout<<(z3*z4.conjugate())/(z4*z4.conjugate())<<endl;
/* Add statement here to compute ((z1-z2).(z3/z4))/(z4/z3)
and print the result:*/
ang = (z1+z2).argument();
cout<<"angle = "<<ang<<endl;
/* Put a comment here explaining what happened and why?
*/
}
catch(int e)
{
if (e == -1)
cerr<<"DivideByZero Exception in / operator"<<endl;
else if (e == -2)
cerr<<"DivideByZero Exception in argument function"<<endl;