Arithmetic operation on complex numbers using function overloading

Program Description

The program demonstrates the implementation of complex number class and performs simple arithmetic operation using operator overloading.The program supports only floating point value for demonstration purposes.
The program also uses the math library for power function;therefore make sure to link it as well while compiling.
The command to compile this file would be of the form:
g++ complex.cpp -o complex -lm
where complex.cpp is the name of the source file.
Since we are using math library in the program we need to explicitly link it using the -l flag at the command line.

Program
/******************************************************************
* 	code.cheraus.com
*	
*   Arithmetic operation on complex numbers using function overloading	
*
********************************************************************/


#include<iostream>
#include<cmath>

using namespace std;


//class definition for complex number
class complex{
 private:
                float real;    //stores the real part
                float img;     //stores the complex part
 public:
                void getdata();   //takes user input
                void display();   //display the complex number
				
				//constructor for initialization
                complex()
				{  
				   real=img=0;
				}
				
				//function declarations(prototypes)
                complex operator+ (complex c1);
                complex operator- (complex c1);
                complex operator* (complex c1);
                complex operator/ (complex c1);
};


//Function definitions for the declared functions in the class

/*
Function definition for display of complex number

This function displays the complex complex number in a+bi form,where "a" is real part
and "b" is the imaginary part.

*/

void complex::display()
{
   cout<<"("<<real<<")"<<"+"<<"("<<img<<")"<<"i";
}


/*
Function definition for getdata()

This function takes the data from the user to store in the complex number
object.

*/

void complex::getdata()
{
    cout<<"Enter the real and img of complex no.\n";
    cout<<"Real : ";
    cin>>real;
    cout<<"Img : ";
    cin>>img;
}


/*
Function definition for operator "/"

This overloads the "/" operator to perform division of two complex numbers.
It returns the resultant complex number.

*/

complex complex::operator/ (complex c1)
{
   complex div;
   div.real = ((real*c1.real)+(img*c1.img))/(pow(c1.real,2)+pow(c1.img,2));
   div.img = ((img*c1.real)-(real*c1.img))/(pow(c1.real,2)+pow(c1.img,2));
     return(div);
}






/*
Function definition for operator "*"

This overloads the "*" operator to perform multiplication of two complex numbers.
It returns the resultant complex number.

*/

complex complex::operator* (complex c1)
{
   complex mul;
   mul.real = ((real*c1.real)-(img*c1.img));
   mul.img = ((real*c1.img)+(c1.real*img));
     return(mul);
}



/*
Function definition for operator "-"

This overloads the "*" operator to perform subtraction of two complex numbers.
It returns the resultant complex number.

*/
complex complex::operator- (complex c1)
{
   complex sub;
   sub.real = real - c1.real;
   sub.img = img - c1.img;
     return(sub);
}


/*
Function definition for operator "+"

This overloads the "*" operator to perform addition of two complex numbers.
It returns the resultant complex number.

*/
complex complex::operator+ (complex c1)
{
   complex add;
   add.real = real + c1.real;
   add.img = img + c1.img;
     return(add);
}



//start of main function
int main()
{
   complex a,b,c;  //creation of objects
    int opt,opt1=1;
    while(opt1==1)
    {
    a.getdata();   //taking data from user
    b.getdata();
	
    //displaying the menu for the user
	cout<<"\n\t\t\t--Main Menu--\n\t1.Addition\n\t2.Subtraction\n\t3.Multiplication";
    cout<<"\n\t4.Division\n\t5.Exit\n\t\t---Enter your choice-->";
	
	  //taking user choice
     cin>>opt;
	 
	 //handling the user option
     switch(opt)
      {
          case 1://using the operator overloading to perform addition
                 c = a+b;    
                 cout<<"\n\n";
				 
				 //displaying the output in user friendly format
                 a.display();
                 cout<<" + ";
                 b.display();
                 cout<<" = ";
                 c.display();
                 break;
				 
          case 2://using the operator overloading to perform subtraction
                 c = a-b;
				 
				 //printing the output in user friendly format
                 cout<<"\n\n";
                 a.display();
                 cout<<" - ";
                 b.display();
                 cout<<" = "; 
                 c.display(); 
                 break;
				 
          case 3://using the operator overloading to perform multiplication
                 c = a*b;
				 
				 //printing the output in user friendly format
                 cout<<"\n\n";
                 a.display();
                 cout<<" * ";
                 b.display();
                 cout<<" = "; 
                 c.display(); 
                 break;
          case 4://using the operator overloading to perform division
                 c = a/b;
				 
				 //printing the output in user friendly format
                 cout<<"\n\n";
                 a.display();
                 cout<<" / ";
                 b.display();
                 cout<<" = "; 
                 c.display(); 
                 break;
				 
				 
          case 5: return 0;
          
          default: cout<<"\nInvalid choice.....try again\n";
                   break; 		  
      }
       cout<<"\n\n\nDo you wish to continue(Press 1 to continue)";
        cin>>opt1;
      }
	  
   return 0;
}

Output

Enter the real and img of complex no.
Real : 1
Img : 2
Enter the real and img of complex no.
Real : 3
Img : 4
--Main Menu--
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
---Enter your choice-->1

(1)+(2)i + (3)+(4)i = (4)+(6)i

Do you wish to continue(Press 1 to continue)1
Enter the real and img of complex no.
Real : 1
Img : 2
Enter the real and img of complex no.
Real : 3
Img : 4


--Main Menu--
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
---Enter your choice-->2


(1)+(2)i - (3)+(4)i = (-2)+(-2)i


Do you wish to continue(Press 1 to continue)1
Enter the real and img of complex no.
Real : 1
Img : 2
Enter the real and img of complex no.
Real : 3
Img : 4

--Main Menu--
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
---Enter your choice-->3

(1)+(2)i * (3)+(4)i = (-5)+(10)i

Do you wish to continue(Press 1 to continue)1
Enter the real and img of complex no.
Real : 2
Img : 3
Enter the real and img of complex no.
Real : 4
Img : 5
--Main Menu--
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
---Enter your choice-->4

(2)+(3)i / (4)+(5)i = (0.560976)+(0.0487805)i


Do you wish to continue(Press 1 to continue)2

Comments
comments powered by Disqus