A copy constructor is a type of constructor that creates a copy of another object. If we want one object to resemble another, we can use a copy constructor. If no copy constructor is written in the program, the compiler will supply its own copy constructor.
The copy constructor is used to −
classname (const classname &obj) {
// body of constructor
}
#include<iostream>
using namespace std;
class Number{
int a;
public:
Number(){
a = 0;
}
Number(int num){
a = num;
}
// When no copy constructor is found, compiler supplies its own copy constructor
Number(Number &obj){
cout<<"Copy constructor"<<endl;
a = obj.a;
}
void display(){
cout<<"The number for this object is "<< a <<endl;
}
};
int main()
{
Number x, y, z(45), z2;
x.display();
y.display();
z.display();
Number z1(z); // Copy constructor invoked
z1.display();
z2 = z; // Copy constructor not called
z2.display();
Number z3 = z; // Copy constructor invoked
z3.display();
// z1 should exactly resemble z or x or y
return 0;
}
The number for this object is 0
The number for this object is 0
The number for this object is 45
Copy constructor
The number for this object is 45
The number for this object is 45
Copy constructor
The number for this object is 45
A shallow copy is made by copying the data of all member variables as they are. In contrast, a deep copy is made by copying the data of another object along with the values of memory resources that reside outside the object but are handled by that object.
Shallow copy creates an object by simply copying the data of all variables from the original object. This is useful if none of the object’s variables are defined in the heap section of memory. If some variables are allocated memory dynamically from the heap section, then copied object variables will also reference the same memory location.
The dangling pointer will cause ambiguity and run-time errors. Because both objects will refer to the exact memory location, any changes made by one will be reflected in the other. The shallow copy will not serve our purpose because we wanted to create a replica of the object.
#include <iostream>
using namespace std;
class Test
{
public:
int a;
int *p;
Test (int x)
{
a = x;
p = new int[a];
}
Test (Test & t)
{
a = t.a;
p = t.p;
}
};
int main()
{
Test t (5);
t.p[0] = 1;
Test t2 (t);
cout << "t: " << t.a << " " << t.p[0] << endl;
cout << "t2: " << t2.a << " " << t2.p[0] << endl;
}
t: 5 1
t2: 5 1
#include <iostream>
using namespace std;
class Test
{
public:
int a;
int *p;
Test (int x)
{
a = x;
p = new int[a];
}
Test (Test & t)
{
a = t.a;
p = new int[a];
if (p)
{
for (int i = 0; i < a; i++)
{
p[i] = t.p[i];
}
}
}
};
int main()
{
Test t (5);
t.p[0] = 1;
Test t2 (t);
cout << "t: " << t.a << " " << t.p[0] << endl;
cout << "t2: " << t2.a << " " << t2.p[0] << endl;
}
t: 5 1
t2: 5 1
Note: also read about References in C++
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…