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

A complex number has two parts: a real part and an imaginary part. These numbers

ID: 3565924 • Letter: A

Question

A complex number has two parts: a real part and an imaginary part.
These numbers are written A + Bi where i =sqrt{-1}.
For example 3+4i and 1-2i are complex numbers. Write a
class declaration (without defining the member functions) that
enables multiplication, addition, subtraction, input and output of
complex numbers using regular arithmetic operators as follows:

int main()

{

   Complex a(3, 4);
   Complex b(1, -2);
   Complex c;

   c = a + b;

   cout << "a + b = " << c << endl;
   c = a - b;
   cout << "a - b = " << c << endl;
   c = a * b; cout << "a * b = " << c << endl;
   return 0;
}

Explanation / Answer

//-----------------------------------
// Complex Number
//-----------------------------------
#include<iostream>
#include<math.h>

using namespace std;

//---------------------------
// class Complex
//---------------------------
class Complex{
private:
float real;
float img;
public:
Complex(float x , float y) : real(x) ,img(y){ }
Complex(){
real = 0;
img = 0;
}
Complex operator+(Complex c2);
Complex operator-(Complex c1);
Complex operator-();
Complex operator*(Complex c1);
void showData();
};

//----------------------------------------------
// overloadin + operator for Complex class
//-----------------------------------------------
Complex Complex::operator+(Complex c2)
{
float real2=0,img2=0;
real2 = real + c2.real;
img2 = img + c2.img;
Complex c3(real2,img2);
return c3;
}

//--------------------------------------------------------
// overloading -(binary) operator for Complex class
//--------------------------------------------------------
Complex Complex::operator-(Complex c1)
{
float real2=0,img2=0;
real2 = real - c1.real;
img2 = img - c1.img;
Complex c3(real2,img2);
return c3;
}

//---------------------------------------------------------
// overloading -(unary) operator for Complex class
//---------------------------------------------------------
Complex Complex::operator-()
{
float real2=0,img2=0;
real2 = -real;
img2 = -img;
Complex c3(real2,img2);
return c3;
}


//-----------------------------------------------------------
// overlaoding * operator for Complex class
//-----------------------------------------------------------
Complex Complex::operator*(Complex c1)
{
float real2=0, img2=0;
real2 = (real*(c1.real)) - (img*(c1.img));
img2 = ((real*(c1.img)) + (img*(c1.real)));
Complex c3(real2,img2);
return c3;
}

//-----------------------------------------------------------
// function to show dat of Complex class
//-----------------------------------------------------------
void Complex::showData()
{
if(img > 0)
{
cout << real << " + " << img << "i"<< endl;
}
else
{
cout << real << " - " << -img << "i" << endl;
}
}

//------------------------------------------
// MAIN FUNCTION
//------------------------------------------
int main()

{

Complex a(3, 4);
Complex b(1, -2);
Complex c;

c = a + b;

cout << "a + b = " ;
c.showData();
cout << endl;
c = a - b;
cout << "a - b = " ;
c.showData();
cout << endl;
c = a * b;
cout << "a * b = " ;
c.showData();
cout << endl;
return 0;
}

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