OPTION A (Basic): Complex N A complex number, c, is an ordered pair of real numb
ID: 3790749 • Letter: O
Question
OPTION A (Basic): Complex N A complex number, c, is an ordered pair of real numbers doubles For example, for any two real numbers, s and t, we can form the complex number: This is only part of what makes a complex number complex. Another important aspect is the definition of special rules for adding, multiplying, dividing, etc. these ordered pairs. Complex numbers are more than simply x-y coordinates because of these operations. Examples of complex numbers in this format are (3, 3), (-1, 5), (1.034, 77.5) and (99.9, 108.5). We will build a class of complex numbers called Complex. One important property of every complex number, c, is its length, or modulus, defined as follows. If c (s, t) then: modulus(c) For example: (-4,3) modulus(-4,3) Create a class of Complex numbers. Private Data double real, double imag These two doubles define the Complex number and are called therealand imaginary parts. Think of each Complex number as an ordered pair, (real, imag) of doubles. Public Instance Methods Constructors Allow Complex objects to be constructed with zero, one or two double arguments. The zero-parameter constructor initializes the complex number to (0, 0). The one-parameter constructor should set the real member to the argument passed and set the imag member to 0. The two-parameter constructor should set both real and imag according to the parameters. DO ALL THREE VERSIONS INA SINGLE CONSTRUCTOR USING DEFAULT PARAMETERS. Accessors/Mutators The usual two mutators, two accessors. double modulus() This will return the double lcl, i e., the modulus, of the complex number. If the Complex object, c, represents the ordered pair (s, t), then the formula above will give the double value to return Complex reciprocal() This defined in the division operator definition, below. toString() This will return a string like "(-23.4, 8.9)" to the client. Do not provide a show() or display() method. You will do this using your insertion operator as shown below. operators We will describe several operators that you must implement for the class. Some will be defined externally (which I w call friend operators. However, you may, or may not really need to declare them to be friends all you need to know is that if I say "friend I mean implement the operator as a non-member. Otherwise, implement it as a member. Exception ClassExplanation / Answer
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
class complex
{
private:
double real; // Real Part
double imag; // Imaginary Part
public:
complex(double r=0.0,double im=0.0) // CONSTRUCTOR
{
real=r;
imag=im;
}
complex add(complex c)
{
complex tmp;
tmp.real=this->real+c.real;
tmp.imag=this->imag+c.imag;
return tmp;
}
complex subtract(complex c)
{
complex tmp;
tmp.real=this->real - c.real;
tmp.imag=this->imag - c.imag;
return tmp;
}
complex multiply(complex c)
{
complex tmp;
tmp.real=(real*c.real)-(imag*c.imag);
tmp.imag=(real*c.imag)+(imag*c.real);
return tmp;
}
complex divide(complex c)
{
double div=(c.real*c.real) + (c.imag*c.imag);
complex tmp;
tmp.real=(real*c.real)+(imag*c.imag);
tmp.real/=div;
tmp.imag=(imag*c.real)-(real*c.imag);
tmp.imag/=div;
return tmp;
}
complex getreciprocal()
{
complex t;
t.real=real;
t.imag=imag * -1;
double div;
div=(real*real)+(imag*imag);
t.real/=div;
t.imag/=div;
return t;
}
double getmodulus()
{
double z;
z=(real*real)+(imag*imag);
z=sqrt(z);
return z;
}
void setdata()
{
cout << "Enter the real and imaginary parts : ";
cin >> this->real;
cin >> this->imag;
}
complex getdata()
{
complex t;
t.real=real;
t.imag=imag ;
return t;
}
double getreal()
{
return real;
}
double getimaginary()
{
return imag;
}
void printComplex(void)
{
cout << "Real : " << this->real << endl
<< "Imaginary : " << this->imag << endl;
}
};
int main()
{
complex a, b, c, d;
cout << "Setting first complex number " << endl;
a.setdata();
cout << "Setting second complex number " << endl;
b.setdata();
/* Adding two complex numbers */
cout << "Addition of a and b : " << endl;
c = a.add(b);
c.printComplex();
/* Subtracting two complex numbers */
cout << "Subtraction of a and b : " << endl;
d = a.subtract(b);
a.printComplex();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.