Constructor Overloading in C++ with Example
#include<iostream.h> #include<conio.h> using namespace std; class complex { float x, y; public: complex(){} complex(float){ x=y=a;} complex(float real, float imag) {x=real; y=imag;} friend complex sum (complex, complex); friend void show(complex); }; complex sum(complex c1, complex c2) { complex c3; c3.x=c1.x+c2.x; c3.y=c1.y+c2.y; return(c3); } void show(complex c) { cout<<c.x<<"+j"<<c.y; } int main() { complex A(2.5, 5.5); complex B(3.5, 4.7); complex C; C=sum(A, B); cout<<"A=";show(A); cout<<"B=";show(B); cout<<"B=";show(C); return 0; }
Output:
A=2.5+j5.5 B=3.5+j4.5 C=6.0+j10.2