Add,Subtract,Multiply,Divide Complex Numbers in C++

Problem:

Write a C++ program to add two complex numbers. Write a C++ program to subtract two complex numbers. Write a C++ program to multiply two complex numbers. Write a C++ program to divide two complex numbers.

C++ Program / Source Code:

Here is the source code of C++ program to add, subtract, multiply and divide two complex numbers
/* Aim: Write a C++ program to add two complex numbers.

Write a C++ program to subtract two complex numbers

Write a C++ program to multiply two complex numbers

Write a C++ program to divide two complex numbers */

#include<iostream>
using namespace std;

class complex
{
 float a,b;

 public:
 complex()
 {
  a=b=1;
 }
 
 complex(float x, float y)
 {
  a=x;b=y;
 }
 
 complex operator + (complex c2)
 {
  complex tmp;

  tmp.a=a+c2.a;
  tmp.b=b+c2.b;
  
  return tmp;
 } 

 complex operator - (complex c2)
 {
  complex tmp;

  tmp.a=a-c2.a;
  tmp.b=b-c2.b;
  
  return tmp;
 } 

 complex operator * (complex c2)
 {
  complex tmp;

  tmp.a=a*c2.a-b*c2.b;
  tmp.b=a*c2.b+c2.a*b;

  return tmp;
 }

 complex operator / (complex c2)
 {
  complex tmp;

  tmp.a=(a*c2.a+b*c2.b)+(c2.a*b-a*c2.b);
  tmp.b=(c2.a*c2.a)+(c2.b*c2.b);

  return tmp;
 }

 void display()
 { 
  if(b>=0)
   cout<<" Complex number : "<<a<<"+"<<b<<"i"<<endl;
  else
   cout<<" Complex number : "<<a<<b<<"i"<<endl;
 }

 void display(char op)
 { 
  cout<<a<<"i"<<"/"<<b<<endl;
 }

};

int main()
{
 complex c1(7,-3),c2,c3;

 c1.display();
 c2.display();
 cout<<" Multiplication is ";
 c3=c1*c2;
 c3.display(); 
 cout<<" Addition is ";
 c3=c1+c2;
 c3.display();  
 cout<<" Subtraction is ";
 c3=c1-c2;
 c3.display();
 cout<<" Division is ";
 c3=c1/c2;
 c3.display('d'); 
 return 0;
}

/* Output of above code:-

 Complex number : 7-3i
 Complex number : 1+1i
 Multiplication is  Complex number : 10+4i
 Addition is  Complex number : 8-2i
 Subtraction is  Complex number : 6-4i
 Division is -6i/2

*/