IMPLEMENTATION OF COMPLEX NUMBER OPERATIONS USING OPERATOR OVERLOADING - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Tuesday, October 26, 2010

IMPLEMENTATION OF COMPLEX NUMBER OPERATIONS USING OPERATOR OVERLOADING


PROGRAM:

#include <math.h>
#include <iostream.h>
#include <iomanip.h>
#include<conio.h>
class complex
{
     private:
                float real;               // Real Part
                float imag;      //  Imaginary Part
   public:
              complex(float,float);
              complex operator +(complex);
              complex operator -(complex);
              complex operator *(complex);
              complex operator /(complex);
              void printdata();

};
//                                        CONSTRUCTOR
                complex::complex(float r=0.0f,float im=0.0f)
                {
                   real=r;
                   imag=im;
                }
                complex complex::operator +(complex c)
                {
                        complex tmp=c;
                        tmp.real=this->real+tmp.real;
                        tmp.imag=this->imag+tmp.imag;
                        return tmp;
                }
                complex complex::operator -(complex c)
                {
                        complex tmp=c;
                        tmp.real=this->real - tmp.real;
                        tmp.imag=this->imag - tmp.imag;
                        return tmp;
                }
                complex complex::operator *(complex c)
                {
                        complex tmp=c;
                        tmp.real=(real*tmp.real)-(imag*tmp.imag);
                        tmp.imag=(real*tmp.imag)+(imag*tmp.real);
                        return tmp;
                }
                complex complex::operator /(complex c)
                {
                        complex tmp=c;
                        float res= (imag*tmp.real)-(real*tmp.imag);
                        float div=(tmp.real*tmp.real)+(tmp.imag*tmp.imag);
                        tmp.real=(real*tmp.real)+(imag*tmp.imag);
                        tmp.real/=div;
                        tmp.imag= res;
                        tmp.imag/=div;
                   return tmp;
                }
                void complex::printdata()
                {
                        cout<<real<<" + ("<<imag<<")j\n";
                }
void main()
{
   complex c1(5,3),c2(3,2),c3;             // Calls Constructor
   clrscr();
   c3=c1+c2;                                        // Calls overloaded +
   cout<<"\nComplex addition:\n";
   c1.printdata();
   c2.printdata();
   c3.printdata();
   cout<<"\nComplex subtraction:\n";
   c3=c1-c2;                                         // Calls overloaded -
   c1.printdata();
   c2.printdata();
   c3.printdata();
   cout<<"\nComplex multiplication:\n";
    c3=c1*c2;                                        // Calls overloaded *
   c1.printdata();
   c2.printdata();
   c3.printdata();
   cout<<"\nComplex division:\n";
    c3=c1/c2;                 // Calls overloaded /
   c1.printdata();
   c2.printdata();
   c3.printdata();
   getch();
}


No comments:

Post a Comment