Tuesday, 2 August 2016

Practical No:01 (Complex No)

#include<iostream>
using namespace std;
class complex
{
public:
    float real,img;
    complex()
    {
        real=0;
        img=0;
    }
    complex(float r,float i)
    {
        real=r;
        img=i;
    }
    complex operator+ (complex obj)
    {
        complex temp;
        temp.real=real+obj.real;
        temp.img=img+obj.img;
        return(temp);
    }
    complex operator- (complex obj)
    {
        complex temp;
        temp.real=real-obj.real;
        temp.img=img-obj.img;
        return(temp);
    }
    complex operator* (complex obj)
    {
        complex temp;
        temp.real=real*obj.real-img*obj.img;
        temp.img=img*obj.real+real*obj.img;
        return(temp);   
    }
    complex operator/ (complex obj)
    {
        complex temp;
        float new_temp;
        new_temp=(obj.real*obj.real)+(obj.img*obj.img);
        temp.real=((real*obj.real)+(img*obj.img))/new_temp;
        temp.img=new_temp;
        return(temp);
    }
};
int main()
{
complex a(2,6);
complex b(4,1);
cout<<"\n The first complex num is..";
cout<<a.real<<" and "<<a.img<<"i";
cout<<"\n The second complex num is..";
cout<<b.real<<" and "<<b.img<<"i";
cout<<"\n\n\t\t *******Arithmetic operations******";
complex c=a+b;
cout<<"\n Addition of two complex nums is... ";
cout<<c.real<<" and "<<c.img<<"i"<<endl;
 c=a-b;
cout<<"\n Subtraction of two complex nums is... ";
cout<<c.real<<" and "<<c.img<<"i"<<endl;
 c=a*b;
cout<<"\n Multiplication of two complex nums is... ";
cout<<c.real<<" and "<<c.img<<"i"<<endl;
c=a/b;
cout<<"\n Division of two complex nums is... ";
cout<<c.real<<" and "<<c.img<<"i"<<endl;
return 0;
}


No comments:

Post a Comment