Monday, October 20, 2014

Write a program for use of default argument in the constructor for add two complex values.

Prog 35: Write a program for use of default argument in the constructor for add two complex values.
#include<iostream.h>
#include<conio.h>
class complex
{
int x,y;
public:
complex()
{}
complex(int a,int b=5)
{
x=a;
y=b;
}
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()
{
clrscr(); complex A(3); complex B(4,6); complex C; C=sum(A,B); A.display(); B.display(); C.display(); getch();
}

Output:
3 +i5
4 +i6
7 +i11

No comments: