Implement a class Complex which represents the Complex Number data type. Implement the following operations

 Implement a class Complex which represents the Complex Number data type. Implement the following operations:

1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overloaded operator+ to add two complex numbers.
3. Overloaded operator* to multiply two complex numbers.
4. Overloaded << and >> to print and read Complex Numbers.

#include
OUTPUT FOR THE PROGRAM
using namespace std;
class complex
{
   public:
         float real,img;
         complex()
         {
           real = 0;
           img = 0;
         }
     
     
        friend ostream & operator <<(ostream& ,complex& );
        friend istream & operator >>(istream& ,complex& );
        friend complex operator +(complex &c1,complex&a1);
        friend complex operator -(complex &c2,complex&a2);
        friend complex operator *(complex &c3,complex&a3);
        friend complex operator /(complex &c4,complex&a4);
};

  istream & operator >>(istream&in ,complex&obj )
  {
    cout<<"Enter the real part: ";
    in>>obj.real;
    cout<<"Enter the imaginary part: ";
    in>>obj.img;
    return in;
  }

  ostream & operator <<(ostream& out ,complex& obj )
  {
    cout<<"Complex No is :";
    out<    out<<"+"<<"("<    return out;
  }

  complex operator +(complex &c1,complex&a1)
  {
    complex r1;
    r1.real=c1.real+a1.real;
    r1.img=c1.img+a1.img;
    return r1;
  }

  complex operator -(complex &c2,complex&a2)
  {
    complex r2;
    r2.real=c2.real-a2.real;
    r2.img=c2.img-a2.img;
    return r2;
  }

  complex operator *(complex &c3,complex&a3)
  {
    complex r3;
    r3.real=c3.real*a3.real-c3.img*a3.img;
    r3.img=c3.real*a3.img+c3.img*a3.real;
    return r3;
  }

  complex operator /(complex &c4,complex&a4)
  {
    complex r4;
    r4.real=(c4.real*a4.real+c4.img*a4.img) / (a4.real*a4.real+c4.img*c4.img);
    r4.img=(-c4.real*a4.img+c4.img*a4.real) / (a4.real*a4.real+c4.img*c4.img);
    return r4;
  }
int main()
{
    complex s,a,b,add,sub,mul,div;
    cout<    cout<<"Enter First Complex No:\n";
    cin>>a;
    cout<<"Enter Second Complex No:\n";
    cin>>b;
  int ch;
  char ans ,y,Y;
   do
   {
        cout<<"\nMENU:"<        cout<<"1.Addition"<        cout<<"2.Substraction"<        cout<<"3.Multiplication"<        cout<<"4.Division"<        cout<<"Enter your Choice: ";
        cin>>ch;

     switch(ch)
     {
        case 1:
            add=a+b;
            cout<<"Addition of ";
            cout<            break;
        case 2:
            sub=a-b;
            cout<<"Substraction of ";
            cout<            break;
        case 3:
           mul=a*b;
           cout<<"Multiplication of ";
           cout<           break;
        case 4:
            div=a/b;
            cout<<"Division of ";
            cout<
            break;
       default:
            cout<<"Please Enter valid choice";
            break;
     }
     cout<<"\n Do you want to continue (y/n): ";
     cin>>ans;
  }
  while(ans=='y'||ans=='Y');

  return 0;
}


Post a Comment

Previous Post Next Post