2. Define, implement and test a Complex class, which has: (a) At least two const
ID: 3736027 • Letter: 2
Question
2. Define, implement and test a Complex class, which has: (a) At least two constructors. (b) A destructor. (c) Several Methods with functionality as described below: . Returns real part. Returns imaginary part. . Displays the number. . Computes magnitude. Computes polar co-ordinates. . Overloads operators +,. *and/, with the corresponding operations. Overloads the operator~for complex conjugate. Overloads the operators andfor comparison. The methods must make appropriate use of the reference variables, for arguments and re- turn types, as described in Prata, Chapter 10. For more details on operator overloading, refer to Prata, Chapter 11. (6 points)Explanation / Answer
ANS:-
Given that,
, implement and test a Complex class, which has: (a) At least two constructors. (b) A destructor. (c)Several Methods with functionality as described below: . Returns real part. Returns imaginary part. . Displays the number. . Computes magnitude.
program:-
//main.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
int main()
{
Complex a(3.0, 4.0);
Complex c;
Complex temp;
cout << "Enter a Complex number (q to quit): ";
while (cin>>c)
{
cout << "c is " << c << endl;
temp = ~c;
cout << "Complex conjugate is " <<temp << endl;
cout << "a is " << a << endl;
temp = a+c;
cout << "a + c is " << temp << endl;
temp = a-c;
cout << "a - c is " << temp<< endl;
temp = a*c;
cout << "a * c is " << temp << endl;
temp = c*2;
cout << "c * 2 is " << temp << endl;
cout << "Enter a Complex number (q to quit): ";
}
cout << "Done! ";
return 0;
}
-------------------------------------------------------------
//Complex.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
Complex::~Complex()
{
//do nothing
}
Complex Complex::operator+(Complex &c)
{
Complex sum;
sum.real = real + c.real;
sum.imag = imag + c.imag;
return sum;
}
Complex Complex::operator-(Complex &c)
{
Complex diff;
diff.real = real - c.real;
diff.imag = imag - c.imag;
return diff;
}
Complex Complex::operator*(double num) const
{
Complex prod;
prod.real = (real * num);
prod.imag = (imag * num);
return prod;
}
Complex operator*(Complex &c1,Complex &c2)
{
Complex prod;
prod.real = (c1.real * c2.real) - (c1.imag * c2.imag);
prod.imag = (c1.real * c2.imag) + (c1.imag * c2.real);
return prod;
}
Complex Complex::operator~()
{
Complex conj;
conj.real = real;
conj.imag = imag * (-1);
return conj;
}
std::ostream& operator<<(std::ostream &os,Complex &c)
{
os << "("<<c.real<<","<<c.imag<<"i)"<<endl;
}
std::istream& operator>>(std::istream &is, Complex &c)
{
cout<<"Real:";
is>>c.real;
cout<<"Imaginary:";
is>>c.imag;
}
-------------------------------------------------
//Complex.h
#ifndef Complex_H
#define Complex_H
class Complex{
private:
int real;
int imag;
public:
Complex()
{
real = 0;
imag = 0;
}
Complex(int r, int i)
{
real = r;
imag = i;
}
~Complex();
Complex operator+(Complex &c);
Complex operator-(Complex &c);
Complex operator*(double num) const;
Complex operator~();
friend Complex operator*(Complex &c1, Complex &c2);
friend std::ostream& operator<<(std::ostream &os,Complex &c);
friend std::istream& operator>>(std::istream &is,Complex &c);
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.