-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepcopy.cpp
More file actions
89 lines (75 loc) · 1.47 KB
/
deepcopy.cpp
File metadata and controls
89 lines (75 loc) · 1.47 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <string.h>
using namespace std;
class Hero
{
private:
int health;
public:
char level;
char *name;
Hero()
{
cout << "default Construtor called" << endl;
name = new char[100];
}
// copy constructor deep copy
Hero(Hero &temp)
{
cout << "copy constructor called" << endl;
char *ch = new char[strlen(temp.name) + 1];
strcpy(ch, temp.name);
this->name = ch;
int health1 = temp.health;
this->health = temp.health;
this->level = temp.level;
}
void print()
{
cout << endl;
cout << "( "
<< "Health is :- " << this->health << " , ";
cout << "level is :- " << this->level << " , ";
cout << "Name is :- " << this->name << " )";
cout << endl;
}
void setHealth(int health)
{
this->health = health;
}
void setLevel(char level)
{
this->level = level;
}
void setName(char name[])
{
strcpy(this->name, name);
}
int getHealth()
{
return health;
}
char getLevel()
{
return level;
}
};
int main()
{
Hero h1;
h1.setHealth(100);
h1.setLevel('A');
char name[6] = "rahul";
h1.setName(name);
h1.print();
Hero h2(h1);
h2.print();
h1.name[0] = 'n';
h1.setHealth(50);
h1.print();
h2.print();
// copy assignment operator
h1 = h2;
h1.print();
h2.print();
}