Explain copy constructer with suitable example give proper class definition including the definition of copy constructor. How will this constructor be inwoked?
Answers
Answered by
0
hello frnd ,
copy constructor is the form of classname (classname &) and used for the initialization of an object from another object of same type
for example,
class fun
{
private:
float x,y;
public:
fun()
{ } // default constructor
fun(float a,float b) //constructor
{
x=a;
y=b;
}
fun(fun &f) // copy constructor
{
cout<<" copy constructor is at work\n";
x=f.x;
y=f.y;
}
void display(void)
{
cout <<x<<" "<<y<<end;
}
};
Here we have 3 constructors, one a default constructor, second a parameterized constructor for assignment of initial values given and third copy constructor for copying data values of a fun object to another .
copy constructor is the form of classname (classname &) and used for the initialization of an object from another object of same type
for example,
class fun
{
private:
float x,y;
public:
fun()
{ } // default constructor
fun(float a,float b) //constructor
{
x=a;
y=b;
}
fun(fun &f) // copy constructor
{
cout<<" copy constructor is at work\n";
x=f.x;
y=f.y;
}
void display(void)
{
cout <<x<<" "<<y<<end;
}
};
Here we have 3 constructors, one a default constructor, second a parameterized constructor for assignment of initial values given and third copy constructor for copying data values of a fun object to another .
Answered by
1
Copy constructor is used to copy the value of an object into another object .
Similar questions