Prog 34 : Write a program for addition of two complex value using multiple constructor in a class.
#include<iostream.h>
#include<conio.h>
class complex
{
int x,y;
public:
complex()
{}
complex(int a,int b)
{
x=a;
y=b;
}
complex(int c)
{
x=y=c;
}
void display()
{
cout<<endl<<x<<" +i"<<y;
}
friend complex sum(complex,complex);
};
complex sum(complex c1,complex c2)
{
complex c3; c3.x=c1.x+c2.x; c3.y=c1.y+c2.y; return c3;
}
void main()
{
getch();
complex A(5);
complex B(2,3);
complex C;
C=sum(A,B);
A.display();
B.display();
C.display();
getch();
}
Output:
5 +i5
2 +i3
7 +i8
No comments:
Post a Comment