source code name: constr2.cpp
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class complex
{
float r,i;
public:
complex()
{ }
complex(float a)
{
r=i=a;
}
complex(float x , float y)
{
r=x;
i=y;
}
friend void show(complex p);
friend complex sum(complex q, complex r);
};
void show(complex z)
{
cout<<"\n "<<z.r<<" + j: "<<z.i;
}
complex sum(complex M, complex N)
{
complex D;
D.r=M.r+N.r;
D.i=M.i+N.i;
return D;
}
void main()
{
clrscr();
cout<<"Constructor overloading program: ";
complex A(2.6,3.7);
complex B(4.8);
complex C;
C= sum(A,B);
cout<<" \n object A: "; show(A);
cout<<" \n object B: "; show(B);
cout<<" \n object C: "; show(C);
getch();
}
output:
Constructor overloading program:
object A: 2.6 +j 3.7
object B: 4.8 +j 4.8
object C: 7.4 +j 8.5
0 Comments
Post a Comment