File tree Expand file tree Collapse file tree 1 file changed +97
-0
lines changed Expand file tree Collapse file tree 1 file changed +97
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ // SHALLOW AND DEEP
3
+
4
+
5
+ #include < iostream>
6
+ #include < string.h>
7
+ using namespace std ;
8
+
9
+ class Hero {
10
+
11
+ // properties
12
+ private:
13
+ int health;
14
+
15
+ public:
16
+ char level;
17
+ char *name;
18
+
19
+ // Constructor
20
+ Hero () {
21
+ cout << " Constructor called " << endl;
22
+ name = new char [100 ];
23
+ }
24
+
25
+ // Paramterised constructor
26
+ Hero (int health) {
27
+ cout << " this -> " << this <<endl;
28
+ this -> health = health;
29
+ }
30
+
31
+ Hero (int health, char level) {
32
+ this -> level = level;
33
+ this -> health = health;
34
+ }
35
+
36
+ // Copy constructor
37
+ Hero (Hero& temp) {
38
+
39
+ char *ch = new char [strlen (temp.name ) + 1 ];
40
+ strcpy (ch, temp.name );
41
+ this ->name = ch;
42
+
43
+ cout<<" Copy constructor called " << endl;
44
+ this ->health = temp.health ;
45
+ this ->level = temp.level ;
46
+ }
47
+
48
+ void print (){
49
+ cout<< endl;
50
+ cout << " Name is: " << this ->name << endl;
51
+ cout << " Health is: " << this ->health << endl;
52
+ cout << " Level is: " << this ->level << endl;
53
+ cout<< endl;
54
+ }
55
+
56
+ int getHealth () {
57
+ return health;
58
+ }
59
+
60
+ char getLevel () {
61
+ return level;
62
+ }
63
+
64
+ void setHealth (int h) {
65
+ health = h;
66
+ }
67
+
68
+ void setLevel (char ch) {
69
+ level = ch;
70
+ }
71
+
72
+ void setName (char name[]) {
73
+ strcpy (this ->name , name);
74
+ }
75
+
76
+ };
77
+
78
+ int main (){
79
+
80
+ Hero hero1;
81
+ hero1.setHealth (12 );
82
+ hero1.setLevel (' D' );
83
+ char name[7 ] = " Nikhil" ;
84
+ hero1.setName (name);
85
+
86
+ Hero hero2 (hero1);
87
+
88
+ hero1.name [0 ] = ' G' ;
89
+ hero1.print ();
90
+ hero2.print ();
91
+
92
+ hero1 = hero2;
93
+ hero1.print ();
94
+ hero2.print ();
95
+ return 0 ;
96
+ }
97
+
You can’t perform that action at this time.
0 commit comments