subtract of complex number using operator overloading

subtract of  complex number as well as integer using operator overloading in a single program

Solution:

#include <iostream>
using namespace std;
class sub
{
  public:
  int x;
 
  int A,B;
   int n=2;  //n=1 for numbers and n=2 for imaginary num
 
    void input1()
    {
 
      if(n==1)
      {
        cout<<"enter num";
        cin>>A;
      }
      if(n==2)
      {
        cout<<"enter real and imaginary parts:";
      cin>>A>>B;
      }
    }
     sub operator -(sub a2)
     {
        sub temp;
        temp.n=n;
       if(n==1)
       {
       
      temp.A=A-a2.A;

       }
       if (n==2)
       {
      temp.A=A-a2.A;
      temp.B=B-a2.B;
       }
      return temp;
     }
     void output1()
     {
       if(n==1)
       {
         cout<<"sub of two number:"<<A;
       }
       if(n==2)
       {
         cout<<"sub of two imaginary numbers:"<<A<<"+i"<<B;
       }
     }
 
};
int main()
{

  sub a1,a2,result;
 
  a1.input1();
  a2.input1();
  result=a1-a2;
  result.output1();
 
  return 0;
}

Output:




0 comments: