-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path34_Copy_Constructor.cpp
56 lines (36 loc) · 1.23 KB
/
34_Copy_Constructor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
using namespace std;
class Number{
int a;
public:
// We have to always create a default constructor like this. Without it there will be an error while creating objects.
Number(){
a = 0; // Default value of a..!!
}
Number(int num){
a = num;
}
// COPY CONSTRUCTOR...!!
// When no copy constructor is found, complier supplies its own copy constructor.
Number(Number &obj){
cout << "Copy constructor called. ";
a = obj.a;
}
void display (){
cout << "The numbers for this object is: " << a <<endl;
}
};
int main()
{
Number x, y(21), z(42), z2;
// x = Number(2);
x.display();
y.display();
z.display();
// I want to create a "z1" objects which should exactly resembles z (or x) (or y)
Number z1(z); // Copy Constructor is Invoked.
z1.display();
z2 = z; // Copy Constructor is NOT Invoked. As already bane huye object ko assign kar diya "z" se --> It will not work.
Number z3 = z; // Copy Constructor is Invoked. Invoke ho raha hai because ye ekhi line me object ban raha hai so copy constructor call ho jayega.
return 0;
}