-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeep_copy.cpp
63 lines (60 loc) · 1.27 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
class Hero{
public:
int health;
char level;
char *name; //char pointer
Hero(){
cout<<"simple constructor called"<<endl;
name = new char[100];
}
Hero(Hero &temp){
cout<<"custom copy constructor called"<<endl;
char *ch = new char[ strlen(temp.name)+1 ];
strcpy(ch,temp.name);
this->name = ch;
this->health = temp.health;
this->level = temp.level;
}
int getHealth(){
return health;
}
char getLevel(){
return level;
}
void setHealth(int h){
health = h;
}
void setLevel(char ch){
level = ch;
}
void setName(char nm[]){
strcpy(this->name,nm);
}
void print(){
cout<<"health: "<<this->health<<endl;
cout<<"level: "<<this->level<<endl;
cout<<"name: "<<this->name<<endl;
}
};
int main() {
Hero h1;
h1.setHealth(100);
h1.setLevel('A');
char a[5] = "Abhi";
h1.setName(a);
h1.print();
cout<<endl;
// call custom copy constructor
Hero h2(h1);
h2.print();
cout<<endl;
//NO change in dynamic array name
h1.setHealth(90);
h1.setLevel('B');
h1.name[0] = 'R';
h2.print();
cout<<endl;
return 0;
}