Multiple constructor and friend function in CPP program.

using namespace std;
class Complex{
float x,y;
Complex(){ //default constructor
//cout<<“Default constructor called!”<<endl;
x = 0; y = 0;
Complex(float a){ // Argument constructor, constructor overloading
//cout<<“Arg1 constructor called!”<<endl;
x = y = a;
Complex(float real, float imag){
//cout<<“Arg2 constructor called!”<<endl;
x = real; y = imag;
friend Complex sum(Complex, Complex);
friend void show(Complex);
//cout<<“Destructor called!”<<endl;
void show(Complex c){
Complex sum(Complex a, Complex b){
Complex t;
t.x = a.x + b.x;
t.y = a.y + b.y;
return t;
int main(){
Complex c1, c2(10), c3(5,6);
cout<<“Sum of 2 Complex:”<<endl;
c1 = sum(c2,c3);
return 0;

In this program Complex indicates complex numbers (x + yi) where a and b r two numbers. x is real part and y is imaginary part of that number. We use multiple constructor (overloading) to create complex object. Also used friend function to add and show complex numbers.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>