Thursday, 16 March 2017

/ If Copy constructor specified within a class definition by programmer than any explicit call of constructor is not valid 

#include<iostream>
using namespace std;

class Complex
{
int real,img;
public:
Complex()
{
real=0;
img=0;
}
Complex(int,int); //Constructor declaration is also allowed here as like normal member function 
Complex(Complex & x) // Copy constructor specified within class definition 
{
real=x.real;
img=x.img;
}
void putdata()
{
cout<<real<<"+"<<img<<"i"<<endl;
}
};
Complex::Complex(int r,int i) //Syntax to define constructor outside the class instead of inline constructor 
{
real=r;
img=i;
}
int main()
{
Complex c1; //Call default Constructor implicit call
Complex c2= Complex(10,20); //Call constructor with two int parameters (Explicit call). Error as explicit call of constructor 
//will be invalid as programmer also specified copy constructor within class definition. 
Complex c3(c2); 
c1.putdata();
c2.putdata();
c3.putdata();
}

No comments:

Post a Comment