-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32CopyConstructor.cpp
More file actions
51 lines (40 loc) · 874 Bytes
/
Copy path32CopyConstructor.cpp
File metadata and controls
51 lines (40 loc) · 874 Bytes
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
#include <iostream>
using namespace std;
class number
{
int a;
public:
number() {}
number(int num)
{
a = num;
}
//when no copy constructor is found, compiler supplies its own copy constructor
// that is, the code will run even without the function mentioned below
number(number &obj)
{
cout<<"copy constructor called "<<endl;
a = obj.a;
}
void display()
{
cout << "the number for this object is " << a << endl;
}
};
int main()
{
number x, y, z(45), z3;
z.display();
y.display();
x.display();
// z1 should exaclty resemble z or x or y
number z1(x);
z1.display();
number z2(z1);
z2.display();
z3 = z;
z3.display(); // here copy constructor is not called
number z4 = z;
z4.display(); // here copy constructor is called
return 0;
}