Copy Constructor In C++
Copy Constructor In C++
What is copy constructor ?
In the C++ programming language, a copy constructor is a special constructor for creating a new object as a copy of an existing object. Copy constructors are the standard way of copying objects in C++, as opposed to cloning, and have C++-specific nuances.
**I AM CONTINUOUSLY IMPROVING THIS ARTICLE**
#include<iostream>
using namespace std;
class sample
{
int x;
public:
sample(int p) // Parametrized Constructor because of int p
{
x=p;
}
void disp()
{
cout<<x<<endl;
}
};
int main()
{
sample s1(10);
sample s2(s1);
s2=s1;
/* another way is that we can directly
write 'sample s2=s1;' */
s1.disp();
s2.disp();
return 0;
};
Comments
Post a Comment