Question

In: Computer Science

A complex number is a number with two components, namely, the real part (rel) and the...

A complex number is a number with two components, namely, the real part (rel) and the imaginary part (img). For example the complex number (a+bi) has a as the real part and b as the imaginary part. In developing such a class, assume that both of the real and imaginary parts to be floats. The symbol i represent . A declaration for the Complex class is presented next.

class Complex {
public:
   Complex (float r = 0.0, float g= 0.0);
   Complex (const Complex & c); //The copy constructor
void print( ) const; // Print out the class data
Complex & negate ( ); // Multiplies the data members of a complex object by a
// negative one.
Complex addComplex (const Complex & b);
// add two complex numbers, the calling
//object and b and return a new complex //one
Complex subComplex (const Complex &b);
// subtract two complex numbers, the   
// calling object and b and return a new
// complex one

Complex multipComplex ( const Complex &c) const; // multiply two complex
// numbers and return a complex one
Complex assginComplex ( const Complex & c); // assign one complex number to
// another one and return the result in a complex object.
bool isEqual (const Complex & c) const; //compares one complex number to
// another and returns a Boolean value
private:
   float rel;
   float img;

};
Implement and test the class Complex. Write a driver program to test your Complex class. Instantiate several Complex objects. Test that all your member functions work properly.

Hint
•   Addition of two complex numbers: (a+bi) +(c+di) = (a+c) + (b+d) i
•   Subtraction: (a+bi) (c+di) = (a-c) + (b-d) i
•   Multiplication: (a+bi) * (c+di) = (ac-bd) + (ad+bc) i
A sample run is given below:

Please enter the first complex number C1(r,img) ; 1 -5

Please enter the second complex number C2(r,img) ; 2 11

Testing the copy constructor C3= C2 = 2+11i

Testing the negate function C4 = C3.negate() = -2 -11i

Testing the assign function C5 = C3.assignComplex(C4) = -2 -11i

Testing the isEqual function C4.isEqual(C5) = 1

Testing the Add Complex function C6 = C1.addComplex(C2) = 3+6i

Testing the Sub Complex function C7 = C1.subComplex(C2) = -1 -16i

Testing the multiply Complex function C8 = C1.multipComplex(C2) = 57+1i

Solutions

Expert Solution

#include <iostream>
#include<complex>
using namespace std;
class Complex{
private:
float rel;
float img;
public:
Complex (float r, float g){
r=rel;
g=img;
}
Complex (const Complex &c)
{
a=c.rel;
b=c.img;
}
//Print out class data
void print() {
cout<<rel<<"+"<<img<<"i"<<endl
}
//Multiplies data members of a complex object by negative 1
Complex negate(complex<float> &c1){
c1.real(rel);
c1.imag(img);
}
Complex addComplex(complex<float> &c1, complex<float> &c2){
cout<<"The sum of"<<c1<<"and"<<c2<<"complex numbers="<<c1+c2;
}
Complex subComplex(complex<float> &c1, complex<float> &c2){
cout<<"The subtraction of"<<c1<<"and"<<c2<<"complex numbers="<<c1-c2;
}
Complex multipComplex(complex<float> &c1, complex<float> &c2){
cout<<"The multiplication of"<<c1<<"and"<<c2<<"complex numbers="<<c1*c2;
}
Complex assignComplex(complex<float> &c1){
float new_r,new_i;
cout<<"Enter the new real value of complex number";
cin>>new_r;
cout<<"Enter the imaginary value of complex number";
cin>>new_i;
c1.rel=new_r;
c1.img=new_i;
cout<<"New complex number assignment is";
print();
}
bool isEqual(const Complex &c) const;

};


Related Solutions

1. Find the real part, the imaginary part, and the modulus of the complex number 1...
1. Find the real part, the imaginary part, and the modulus of the complex number 1 + 8i 2 + 3i , showing your work. 2. Find all three solutions of the equation 2z 3 + 4z 2 −z −5 = 0. (Hint: First try a few “simple” values of z.) You must show all working.
a)Adoubledata field(private)named realfor real part of a complex number. b)Adoubledata field(private)named imgfor imaginarypart of a complex...
a)Adoubledata field(private)named realfor real part of a complex number. b)Adoubledata field(private)named imgfor imaginarypart of a complex number. c)A no-arg constructor that creates a default complex number with real 0and img 0. d)Auser-defined constructorthat creates a complex number with given 2 numbers. e)The accessor and mutator functions for realand img. f)A constant function named addition(Complex&comp1, Complex&comp2) that returns the sum of two givencomplex numbers. g)Aconstantfunction named subtraction(Complex&comp1, Complex&comp2) that returns the subtractionof two givencomplex numbers. h)A constant function named multiplication(Complex&comp1, Complex&comp2)...
A complex function is called Hermitian, if its real part is even and its imaginary part...
A complex function is called Hermitian, if its real part is even and its imaginary part is odd. Prove Fourier transform of a real function is Hermitian.
A complex electronic system is built with a certain number of backup components in its subsystems.
A complex electronic system is built with a certain number of backup components in its subsystems. One subsystem has four identical components, each with a probability of 0.2 of failing in less than 1000 hours. The subsystem will operate if any two of the four components are operating. Assume that the components operate independently. Find the probability that (a) exactly two of the four components last longer than 1000 hours. (b) the subsystem operates longer than 1000 hours.  
complex number
Find the algebraic form of the following complex number \( (1+i\sqrt{3}) ^{2000} \)
complex number
write polar, carnation,argument, angele , ,and rectangular form of thi Complex number (1+3i)(3+4i)(-5+3i)
Complex number
What are the values of all the cube roots (Z = -4^3 - 4i)?
ASAP (Math: The Complex class) A complex number is a number in the form a +...
ASAP (Math: The Complex class) A complex number is a number in the form a + bi, where a and b are real numbers and i is sqrt( -1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a +...
State at least 2 determinants of each of the components of the AD, namely consumption, investment...
State at least 2 determinants of each of the components of the AD, namely consumption, investment and net exports. Explain briefly how a change in the determinants stated above may change the AD.
In java: A complex number is a number in the form a + bi, where a...
In java: A complex number is a number in the form a + bi, where a and b are real numbers and i is sqrt( -1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a + bi - (c...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT