Monday, January 20, 2014

C++ Program for Adding Two Complex Numbers using Constructor Overloading

with 0 Comment
#include<iostream.h>
#include<conio.h>
class complex
{
float real,imag;
public:
complex()
{
real=0.0;
imag=0;
}
complex(float r1,float i1)
{
real=r1;
imag=i1;
}
complex(float r)
{
real=imag=r;
}
complex(complex &c)
{
real=c.real;
imag=c.imag;
}
friend complex add(complex c1,complex c2);
friend void display(complex c);
};
complex add(complex c1,complex c2)
{
complex c3;
c3.real=c1.real+C2.real;
c3.imag=c1.imag+c2.imag;
return(c3);
}
void display(complex c)
{
cout<<"\n Result:<<c.real<<"+i"<<c.imag<<"\n";
}

void main()
{
float a,b,c,d,e;
clrscr();
cout<<"\t Constructor Overloading \n";
cout<<"\t \t ------------------\n";
cout<<"\n Enter the Single Real Value:";
cin>>a;
cout<<"\n Enter the Real and imag values:";
cin>>b>>c;
cout<<"\n\n Using Parameterized Constructor \n";
cout<<"\n\n ----------------------------\n";
complex A(a);
complex B(b,c);
complex C=add(A,B);
display(C);
cout<<"\n";
cout<<"\n Copy Constructor \n";
cout<<"\n --------------- \n";
cout<<"\n Enter the Values of Real and Imag :";
cin>>d>>e;
complex D(d,e);
complex E=D;
complex F=add(D,E);
display(F);
getch();
}


-------------
OUTPUT:
-------------
Constructor Overloading
------------------------------
Enter the Single Real Value:6.2

Enter the Real and imag values:4.1
8.9

Using Parameterized Constructor
----------------------------------------

Result = 10.29+i15.09

Copy Constructor
---------------------
Enter the Values of Real and imag :8.6
4.6

Using Parameterized Constructor
----------------------------------------
Result = 17.20+i9.2

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive