what is the copy constructor in c++ with example programme
Pls helpe me?
Copy constructor is
a constructor function with the same name as the class
used to make deep copy of objects.
There are 3 important places where a copy constructor is called.
When an object is created from another object of the same type
When an object is passed by value as a parameter to a function
When an object is returned from a function
example:
class B //With copy constructor
{
private:
char *name;
public:
B()
{
name = new char[20];
}
~B()
{
delete name[];
}
//Copy constructor
B(const B %26amp;b)
{
name = new char[20];
strcpy(name, b.name);
}
};
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment