-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deep_copy.cpp
50 lines (44 loc) · 908 Bytes
/
Deep_copy.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
#include <iostream>
#include <string.h>
using namespace std;
class Person
{
int health;
char level;
public:
char *name = new char[20];
Person(int health, char level, char name[])
{
this->health = health;
this->level = level;
this->name = name;
}
// Deep copy
Person(Person &p)
{
char *ch = new char[strlen(p.name) + 1];
strcpy(ch, p.name);
this->name = ch;
this->health = p.health;
this->level = p.level;
}
void print()
{
cout << "Name:" << name << " "
<< "Health:" << health << " "
<< "Level" << level << " " << endl;
cout << endl;
}
};
int main()
{
char name[20] = {'N', 'A', 'M', 'E', '\0'};
Person p(10, 'A', name);
p.print();
Person p1(p);
p1.print();
p.name[0] = 'G';
p.print();
p1.print();
return 0;
}