-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshallow_Deep_Copy.cpp
More file actions
53 lines (41 loc) · 1.22 KB
/
shallow_Deep_Copy.cpp
File metadata and controls
53 lines (41 loc) · 1.22 KB
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
#include <iostream>
#include <string>
using namespace std;
class Employees {
public:
string name = "ALI";
int* age_ptr; // Pointer to dynamically allocate memory for the age
Employees(string name, int age) {
this->name = name;
age_ptr = new int[5]; // Allocate memory on the heap
*age_ptr = age;
}
/*
// Shallow copy (Default Copy)
Employees(Employees &obj) { // Shallow copy causes issue in case of Dynamic Memory Allocation
this->name=obj.name;
this->age_ptr=obj.age_ptr;
}
*/
// Deep Copy
Employees(Employees &obj) {
this->name=obj.name;
age_ptr = new int{5}; // Again allocate the memory for tackle of shallow copy issue
*age_ptr=*obj.age_ptr;
}
void getinfo() {
cout << "Name: " << name << endl;
cout << "AGE: " << *age_ptr << endl;
}
};
int main() {
Employees e1("Furqan", 20);
e1.getinfo();
Employees e2(e1);
// Change the value of second object
*(e2.age_ptr) = 22;
e2.name = "ALI";
e1.getinfo();
e2.getinfo();
return 0;
}