Which of the following operators may be used to assign one object to another?

A copy constructor is a member function that initializes an object using another object of the same class. In simple terms, a constructor which creates an object by initializing it with an object of the same class, which has been created previously is known as a copy constructor.  

Example:

Which of the following operators may be used to assign one object to another?

Syntax of Copy Constructor

Characteristics of Copy Constructor

1. The copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object.

2. Copy constructor takes a reference to an object of the same class as an argument.

Sample(Sample &t)
{
       id=t.id;
}

3. The process of initializing members of an object through a copy constructor is known as copy initialization.

4. It is also called member-wise initialization because the copy constructor initializes one object with the existing object, both belonging to the same class on a member-by-member copy basis.

5. The copy constructor can be defined explicitly by the programmer. If the programmer does not define the copy constructor, the compiler does it for us.

For Example: 

C++




p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
1

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
2

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
3

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
4
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
5
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
6

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
8
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
9

ID=10
ID=10
0
ID=10
ID=10
1

ID=10
ID=10
2
ID=10
ID=10
3
ID=10
ID=10
4

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
6
ID=10
ID=10
1

ID=10
ID=10
2
ID=10
ID=10
9
ID=10
ID=10
3
ID=10
ID=10
1
ID=10
ID=10
3
ID=10
ID=10
3

ID=10
ID=10
2
ID=10
ID=10
5

ID=10
ID=10
6
ID=10
ID=10
7

ID=10
ID=10
6
ID=10
ID=10
9

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
1

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
4

ID=10
ID=10
2
ID=10
ID=10
9
1001    Ram    10000
1001    Ram    10000
7
1001    Ram    10000
1001    Ram    10000
8

ID=10
ID=10
2
ID=10
ID=10
5

ID=10
ID=10
6
 GFG!
 GFG!
 GFG!
2

ID=10
ID=10
6
 GFG!
 GFG!
 GFG!
4

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
1

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
2
ID=10
ID=10
3
GFG!
GFG!
0
GFG!
GFG!
1
GFG!
GFG!
2

ID=10
ID=10
2
ID=10
ID=10
3
GFG!
GFG!
5
GFG!
GFG!
1
GFG!
GFG!
7

GFG!
GFG!
8

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
3
GFG!
1

ID=10
ID=10
5

ID=10
ID=10
2
GFG!
4
GFG!
5

ID=10
ID=10
2
GFG!
7
GFG!
8

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
2
MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2) 
1

ID=10
ID=10
2
MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2) 
3
MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2) 
4
MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2) 
5

MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2) 
6
MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2) 
7
MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2) 
8
MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2) 
9

ID=10
ID=10
2
MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2) 
3
GeeksQuiz
GeeksQuiz
GeeksQuiz
GeeksforGeeks
2
GeeksQuiz
GeeksQuiz
GeeksQuiz
GeeksforGeeks
3

MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2) 
6
MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2) 
7
GeeksQuiz
GeeksQuiz
GeeksQuiz
GeeksforGeeks
6
GeeksQuiz
GeeksQuiz
GeeksQuiz
GeeksforGeeks
7

ID=10
ID=10
2
GFG!
GFG!
1
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
00

1001    Ram    10000
1001    Ram    10000
1

Output

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

Types of Copy Constructors

1. Default Copy Constructor

An implicitly defined copy constructor will copy the bases and members of an object in the same order that a constructor would initialize the bases and members of the object.

C++




p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
02

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
3

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
4
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
5
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
6

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
8
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
09

ID=10
ID=10
2
ID=10
ID=10
3
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
12

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
6
ID=10
ID=10
1

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
17
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
18
ID=10
ID=10
3
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
20

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
17
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
23
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
24
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
25

GFG!
GFG!
8

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
3
GFG!
1

ID=10
ID=10
5

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
32

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
34

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
36

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
39

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
41
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
42

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
44

ID=10
ID=10
2
GFG!
GFG!
1
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
00

1001    Ram    10000
1001    Ram    10000
1

Output

ID=10
ID=10

2. User Defined Copy Constructor 

A user-defined copy constructor is generally needed when an object owns pointers or non-shareable references, such as to a file, in which case a destructor and an assignment operator should also be written

C++




p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
49

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
50

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
4
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
5
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
6

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
8
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
56

ID=10
ID=10
5

ID=10
ID=10
2
ID=10
ID=10
3
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
12

ID=10
ID=10
2
ID=10
ID=10
6
ID=10
ID=10
1

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
17
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
18
ID=10
ID=10
3
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
68

ID=10
ID=10
2
ID=10
ID=10
5

ID=10
ID=10
6
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
72

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
74

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
76
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
77

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
78 

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
80
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
81

ID=10
ID=10
2
ID=10
ID=10
5

ID=10
ID=10
6
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
85

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
1

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
17
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
90

ID=10
ID=10
2
ID=10
ID=10
5

ID=10
ID=10
6
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
94
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
24
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
96

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
1

GFG!
GFG!
8

ID=10
ID=10
3
GFG!
1

ID=10
ID=10
5

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
32

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
34

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
36

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
78 

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
41
ID=10
ID=10
12

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
44

ID=10
ID=10
2
GFG!
GFG!
1
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
00

1001    Ram    10000
1001    Ram    10000
1

Output

ID=10
ID=10

Which of the following operators may be used to assign one object to another?

 

C++




ID=10
ID=10
19

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
3

ID=10
ID=10
21

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
4
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
5
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
6

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
8
ID=10
ID=10
26

ID=10
ID=10
2
ID=10
ID=10
3
ID=10
ID=10
29

ID=10
ID=10
2
ID=10
ID=10
31

ID=10
ID=10
2
ID=10
ID=10
33
ID=10
ID=10
34

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
6
ID=10
ID=10
1

ID=10
ID=10
2
ID=10
ID=10
39
ID=10
ID=10
3
ID=10
ID=10
41
ID=10
ID=10
33
ID=10
ID=10
43

ID=10
ID=10
2
ID=10
ID=10
45
ID=10
ID=10
46

ID=10
ID=10
2
ID=10
ID=10
5

ID=10
ID=10
6
ID=10
ID=10
50

ID=10
ID=10
6
ID=10
ID=10
52

ID=10
ID=10
6
ID=10
ID=10
54

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
1

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
17
ID=10
ID=10
59

GFG!
GFG!
8

ID=10
ID=10
61
ID=10
ID=10
3
ID=10
ID=10
63
ID=10
ID=10
33
ID=10
ID=10
65

ID=10
ID=10
5

ID=10
ID=10
2
ID=10
ID=10
68

ID=10
ID=10
2
ID=10
ID=10
70

ID=10
ID=10
2
ID=10
ID=10
72

1001    Ram    10000
1001    Ram    10000
1

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
17
ID=10
ID=10
75

ID=10
ID=10
5

ID=10
ID=10
2
ID=10
ID=10
78
ID=10
ID=10
79
ID=10
ID=10
80
ID=10
ID=10
79
ID=10
ID=10
82

1001    Ram    10000
1001    Ram    10000
1

ID=10
ID=10
3
GFG!
1

ID=10
ID=10
5

ID=10
ID=10
2
ID=10
ID=10
88
ID=10
ID=10
89
ID=10
ID=10
90

ID=10
ID=10
2
ID=10
ID=10
92

ID=10
ID=10
2
ID=10
ID=10
94
ID=10
ID=10
95

ID=10
ID=10
2
ID=10
ID=10
97

ID=10
ID=10
2
GFG!
GFG!
1
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
00

1001    Ram    10000
1001    Ram    10000
1

Output

1001    Ram    10000
1001    Ram    10000

When is the copy constructor called? 

In C++, a Copy Constructor may be called in the following cases: 

  • When an object of the class is returned by value. 
  • When an object of the class is passed (to a function) by value as an argument. 
  • When an object is constructed based on another object of the same class. 
  • When the compiler generates a temporary object.

It is, however, not guaranteed that a copy constructor will be called in all these cases, because the C++ Standard allows the compiler to optimize the copy away in certain cases, one example is the return value optimization (sometimes referred to as RVO).

Copy Elision

In copy elision, the compiler prevents the making of extra copies which results in saving space and better the program complexity(both time and space); Hence making the code more optimized.  

Example:

C++




ID=10
ID=10
02

ID=10
ID=10
03

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
3

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
4
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
5
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
6

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
8
ID=10
ID=10
10

ID=10
ID=10
6
ID=10
ID=10
1

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
17
ID=10
ID=10
15
ID=10
ID=10
16
ID=10
ID=10
17

GFG!
GFG!
8

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
3
GFG!
1

ID=10
ID=10
5

ID=10
ID=10
2
ID=10
ID=10
24

ID=10
ID=10
2
ID=10
ID=10
26
ID=10
ID=10
27
ID=10
ID=10
3
ID=10
ID=10
29

ID=10
ID=10
6
ID=10
ID=10
31

ID=10
ID=10
6
ID=10
ID=10
33
ID=10
ID=10
34
ID=10
ID=10
35

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
1

ID=10
ID=10
2
GFG!
GFG!
1
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
00

1001    Ram    10000
1001    Ram    10000
1

Output

 GFG!
 GFG!
 GFG!

Now it is on the compiler to decide what it wants to print, it could either print the above output or it could print case 1 or case 2 below, and this is what Return Value Optimization is. In simple words, RVO is a technique that gives the compiler some additional power to terminate the temporary object created which results in changing the observable behavior/characteristics of the final program.

Case 1:

GFG!
GFG!

Case 2:

GFG!

When is a user-defined copy constructor needed? 

If we don’t define our own copy constructor, the C++ compiler creates a default copy constructor for each class which does a member-wise copy between objects. The compiler-created copy constructor works fine in general. We need to define our own copy constructor only if an object has pointers or any runtime allocation of the resource like a file handle, a network connection, etc.
 

The default constructor does only shallow copy. 

Which of the following operators may be used to assign one object to another?

Deep copy is possible only with a user-defined copy constructor. In a user-defined copy constructor, we make sure that pointers (or references) of copied objects point to new memory locations.  

Which of the following operators may be used to assign one object to another?
 

Copy constructor vs Assignment Operator 

The main difference between Copy Constructor and Assignment Operator is that the Copy constructor makes a new memory storage every time it is called while the assignment operator does not make new memory storage.

Which of the following two statements calls the copy constructor and which one calls the assignment operator? 

MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2) 

A copy constructor is called when a new object is created from an existing object, as a copy of the existing object. The assignment operator is called when an already initialized object is assigned a new value from another existing object. In the above example (1) calls the copy constructor and (2) calls the assignment operator. See this for more details.

Example – Class Where a Copy Constructor is Required 

Following is a complete C++ program to demonstrate the use of the Copy constructor. In the following String class, we must write a copy constructor. 

Example:

C++




ID=10
ID=10
42

ID=10
ID=10
43

ID=10
ID=10
44

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
3

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
4
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
5
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
6

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
8
ID=10
ID=10
51

ID=10
ID=10
0
ID=10
ID=10
1

ID=10
ID=10
2
ID=10
ID=10
55
ID=10
ID=10
56

ID=10
ID=10
2
ID=10
ID=10
3
ID=10
ID=10
59

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
6
ID=10
ID=10
1

ID=10
ID=10
2
ID=10
ID=10
64
1001    Ram    10000
1001    Ram    10000
7
ID=10
ID=10
55
ID=10
ID=10
67
ID=10
ID=10
68

ID=10
ID=10
2
ID=10
ID=10
70
ID=10
ID=10
71
ID=10
ID=10
72
ID=10
ID=10
73

ID=10
ID=10
2
ID=10
ID=10
64
1001    Ram    10000
1001    Ram    10000
7
ID=10
ID=10
77
ID=10
ID=10
46

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
17
ID=10
ID=10
81

ID=10
ID=10
2
ID=10
ID=10
5

ID=10
ID=10
6
ID=10
ID=10
85

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
1
ID=10
ID=10
88

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
17
ID=10
ID=10
91
1001    Ram    10000
1001    Ram    10000
7
ID=10
ID=10
55
ID=10
ID=10
94
ID=10
ID=10
95

GFG!
GFG!
8

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
98

ID=10
ID=10
99

1001    Ram    10000
1001    Ram    10000
00

1001    Ram    10000
1001    Ram    10000
01
1001    Ram    10000
1001    Ram    10000
7
ID=10
ID=10
55
1001    Ram    10000
1001    Ram    10000
04

ID=10
ID=10
5

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
07
1001    Ram    10000
1001    Ram    10000
08
1001    Ram    10000
1001    Ram    10000
09

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
11
1001    Ram    10000
1001    Ram    10000
12
ID=10
ID=10
55
1001    Ram    10000
1001    Ram    10000
14

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
16
1001    Ram    10000
1001    Ram    10000
17

1001    Ram    10000
1001    Ram    10000
1

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
17
1001    Ram    10000
1001    Ram    10000
21
1001    Ram    10000
1001    Ram    10000
7
ID=10
ID=10
55
1001    Ram    10000
1001    Ram    10000
04

ID=10
ID=10
5

ID=10
ID=10
2
ID=10
ID=10
71
1001    Ram    10000
1001    Ram    10000
28

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
07
1001    Ram    10000
1001    Ram    10000
08
1001    Ram    10000
1001    Ram    10000
09

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
11
1001    Ram    10000
1001    Ram    10000
12
ID=10
ID=10
55
1001    Ram    10000
1001    Ram    10000
14

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
16
1001    Ram    10000
1001    Ram    10000
17

1001    Ram    10000
1001    Ram    10000
1

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

1001    Ram    10000
1001    Ram    10000
01
1001    Ram    10000
1001    Ram    10000
7
1001    Ram    10000
1001    Ram    10000
45

ID=10
ID=10
5

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
48

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
11
1001    Ram    10000
1001    Ram    10000
12
ID=10
ID=10
55
1001    Ram    10000
1001    Ram    10000
14

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
16
1001    Ram    10000
1001    Ram    10000
56

1001    Ram    10000
1001    Ram    10000
1

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
3
GFG!
1

ID=10
ID=10
5

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
63
1001    Ram    10000
1001    Ram    10000
64
ID=10
ID=10
43

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
67

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
70
1001    Ram    10000
1001    Ram    10000
71

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
73

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
76
1001    Ram    10000
1001    Ram    10000
77
ID=10
ID=10
43

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
70
1001    Ram    10000
1001    Ram    10000
82

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
73

ID=10
ID=10
2
GFG!
GFG!
1
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
00

1001    Ram    10000
1001    Ram    10000
1

Output

GeeksQuiz
GeeksQuiz
GeeksQuiz
GeeksforGeeks

What would be the problem if we remove the copy constructor from the above code? 

If we remove the copy constructor from the above program, we don’t get the expected output. The changes made to str2 reflect in str1 as well which is never expected. 
 

C++




p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
50

1001    Ram    10000
1001    Ram    10000
90

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
4
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
5
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
6

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
8
1001    Ram    10000
1001    Ram    10000
96

ID=10
ID=10
5

ID=10
ID=10
0
ID=10
ID=10
1

ID=10
ID=10
2
ID=10
ID=10
55
 GFG!
 GFG!
 GFG!
02

ID=10
ID=10
2
ID=10
ID=10
3
ID=10
ID=10
59

ID=10
ID=10
6
ID=10
ID=10
1

ID=10
ID=10
2
ID=10
ID=10
64
1001    Ram    10000
1001    Ram    10000
7
ID=10
ID=10
55
 GFG!
 GFG!
 GFG!
12
ID=10
ID=10
68

ID=10
ID=10
2
ID=10
ID=10
70
ID=10
ID=10
71
 GFG!
 GFG!
 GFG!
17
ID=10
ID=10
73

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
17
 GFG!
 GFG!
 GFG!
21

ID=10
ID=10
2
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
17
ID=10
ID=10
91
1001    Ram    10000
1001    Ram    10000
7
ID=10
ID=10
55
 GFG!
 GFG!
 GFG!
27
ID=10
ID=10
95

GFG!
GFG!
8

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

1001    Ram    10000
1001    Ram    10000
01
1001    Ram    10000
1001    Ram    10000
7
ID=10
ID=10
55
 GFG!
 GFG!
 GFG!
34

ID=10
ID=10
5

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
07
1001    Ram    10000
1001    Ram    10000
08
1001    Ram    10000
1001    Ram    10000
09

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
11
1001    Ram    10000
1001    Ram    10000
12
ID=10
ID=10
55
 GFG!
 GFG!
 GFG!
44

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
16
1001    Ram    10000
1001    Ram    10000
17

1001    Ram    10000
1001    Ram    10000
1

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
98

ID=10
ID=10
99

 GFG!
 GFG!
 GFG!
52

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
17
1001    Ram    10000
1001    Ram    10000
21
1001    Ram    10000
1001    Ram    10000
7
ID=10
ID=10
55
 GFG!
 GFG!
 GFG!
34

ID=10
ID=10
5

ID=10
ID=10
2
ID=10
ID=10
71
1001    Ram    10000
1001    Ram    10000
28

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
07
1001    Ram    10000
1001    Ram    10000
08
1001    Ram    10000
1001    Ram    10000
09

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
11
1001    Ram    10000
1001    Ram    10000
12
ID=10
ID=10
55
 GFG!
 GFG!
 GFG!
44

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
16
1001    Ram    10000
1001    Ram    10000
17

1001    Ram    10000
1001    Ram    10000
1

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
3
GFG!
1

ID=10
ID=10
5

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
63
1001    Ram    10000
1001    Ram    10000
64
ID=10
ID=10
43

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
67

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
70
1001    Ram    10000
1001    Ram    10000
71

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
73

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
76
1001    Ram    10000
1001    Ram    10000
77
ID=10
ID=10
43

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
70
1001    Ram    10000
1001    Ram    10000
82

ID=10
ID=10
2
1001    Ram    10000
1001    Ram    10000
73

ID=10
ID=10
2
GFG!
GFG!
1
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
00

1001    Ram    10000
1001    Ram    10000
1

Output: 

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
0

Can we make the copy constructor private? 

Yes, a copy constructor can be made private. When we make a copy constructor private in a class, objects of that class become non-copyable. This is particularly useful when our class has pointers or dynamically allocated resources. In such situations, we can either write our own copy constructor like the above String example or make a private copy constructor so that users get compiler errors rather than surprises at runtime.

Why argument to a copy constructor must be passed as a reference? 

A copy constructor is called when an object is passed by value. Copy constructor itself is a function. So if we pass an argument by value in a copy constructor, a call to the copy constructor would be made to call the copy constructor which becomes a non-terminating chain of calls. Therefore compiler doesn’t allow parameters to be passed by value.

Why argument to a copy constructor should be const?

One reason for passing const reference is, that we should use const in C++ wherever possible so that objects are not accidentally modified. This is one good reason for passing reference as const, but there is more to it than ‘Why argument to a copy constructor should be const?’

This article is contributed by Shubham Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write your article at write.geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.

Can you use an assignment operator (=) to copy one object to another object of a same class?

You cannot use the = operator to assign one object's values to another object, unless you overload the operator.

Which of the following operators can be used to create a copy of an object?

The assignment operator (operator=) is used to copy values from one object to another already existing object. The purpose of the copy constructor and the assignment operator are almost equivalent -- both copy one object to another.

Which type of function is not a member of a class but has access to the private members of the class?

A friend function is a function that isn't a member of a class but has access to the class's private and protected members.

Is a special function that is called whenever a new object is created and initialized with another object's data?

A copy constructor is a special constructor that is called whenever a new object is created and initialized with another object's data. ★ Sometimes the default memberwise assignment behavior in C++ is perfectly acceptable.