Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. (10 points) A complex number, (A jB), contains two parts: a real part, A, and

ID: 3755214 • Letter: 5

Question

5. (10 points) A complex number, (A jB), contains two parts: a real part, A, and an imaginary part, B (where A and B are floating point numbers). Write the specification for a complex number Abstract Data Type (ADT). It should include the value definition and definitions of the following three operators: a constructor for creating the complex number, an operator for addition of two complex numbers, and an operator for multiplication of two complex numbers. Recall that, CA jB) (C + jD) -(A+C) + j (B+D) B*C+ AD

Explanation / Answer

//complex_.h

#include <iostream>

using namespace std;

class Complex{

private:

double real, imag;

  

public:

Complex(double r = 0, double i =0);

friend ostream & operator << (ostream &out, const Complex &c);

friend istream & operator >> (istream &in, Complex &c);

Complex operator + (const Complex c);

Complex operator * (const Complex c);

};

//complex_.cpp

#include "complex_.h"

Complex::Complex(double r, double i)

{ real = r; imag = i; }

//operator + overloading function

Complex Complex::operator + (const Complex c){

Complex ob;

ob.real = real + c.real;

ob.imag = imag + c.imag;

return ob;

}

//operator * overloading function

Complex Complex::operator * (const Complex c){

Complex ob;

ob.real = real*c.real - imag * c.imag ;

ob.imag = real * c.imag + imag*c.real;

return ob;

}

//operator << overloading function

ostream & operator << (ostream &out, const Complex &c)

{

out << c.real;

out << " + "<<c.imag << " i"<<endl;

return out;

}

//operator >> overloading function

istream & operator >> (istream &in, Complex &c)

{

cout << "Enter Real Part ";

in >> c.real;

cout << "Enter Imagenory Part ";

in >> c.imag;

return in;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote