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

Define a class for Complex Numbers. Represent Complex Number as 2 values of type

ID: 3679663 • Letter: D

Question

Define a class for Complex Numbers. Represent Complex Number as 2 values of type double real and imag. Call the class Complex.

Include a constructor with 2 arguments R and I, this constructor should set the values of real and imag to the passed arguments.

Overload the following operators: +, -, *, >>, <<.

<< should print the complex number object in the folloing format "a+bi"

>> should read the complex number object in the folloing format "a+bi"

+ should add 2 complex numbers objects and return an complex number object

- should subtract 2 complex numbers objects and return an complex number object

* should multiply 2 complex numbers objects and return an complex number object

Also write a test program to test your class.

Explanation / Answer

#include <iostream>

#include<conio.h>

#include<string.h>

#include<stdio.h>

using namespace std;

/*creating a class complex

*/

class complex

{

double img,real;

public:

/*creating constructor*/   

   complex(){

         real = 0;

         img = 0;

      }

//parametrized constructor

      complex(double r, double i){

         real = r;

         img = i;

      }

//operator overloading <<

       friend ostream &operator<<( ostream &output,

                                       const complex &f )

      {

          output << " = " << f.real << "+" << f.img<<"i";

         return output;

      }

//operator overloading >>

      friend istream &operator>>( istream &input, complex &f )

      {

         input >> f.real >> f.img;

         return input;

      }

void display()

{

cout<<" = "<<real<<"+"<<img<<"i";

}

//operator overloading +

complex operator+(complex a2)

{

complex a;

a.real=real+a2.real;

a.img=img+a2.img;

return a;

}

//operator overloading-

complex operator-(complex a2)

{

complex a;

a.real=real-a2.real;

a.img=img-a2.img;

return a;

}

//operator overloading *

complex operator*(complex a2)

{

complex a;

a.real=(real*a2.real)-(img*a2.img);

a.img=(real*a2.img)+(img*a2.real);

return a;

}

};

int main()

{

int ch;

//clrscr();

complex a,b,c,f;

do

{

cout<<" 1.Addition 2.Substraction";

cout<<" 3.Mulitplication 4.Exit ";

cout<<" Enter the choice :";

cin>>ch;

switch(ch)

{

case 1:

       cout<<" Enter The First Complex Number:";

       cin >> a;

       cout << "number is :" << a << endl;

       cout<<" Enter The Second Complex Number:";

       cin>>b;

       c=a+b;

       c.display();

       break;

case 2:

         cout<<" Enter The First Complex Number:";

       cin >> a;

       cout << "number is :" << a << endl;

       cout<<" Enter The Second Complex Number:";

       cin>>b;

       c=b-a;

       c.display();

       break;

case 3:

         cout<<" Enter The First Complex Number:";

       cin >> a;

       cout << "number is :" << a << endl;

       cout<<" Enter The Second Complex Number:";

       cin>>b;

     c=a*b;

       c.display();

       break;

default: break;

}

}while(ch!=4);

getch();

}

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