-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy.cpp
More file actions
104 lines (68 loc) · 1.37 KB
/
copy.cpp
File metadata and controls
104 lines (68 loc) · 1.37 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<iostream>
#include<string.h>
using namespace std;
class Hero
{
public:
int health;
char level;
char *name;
static int power;
Hero()
{
cout<<"simple called"<<endl;
name = new char[100];
}
static int random()
{
return power;
}
//copy constructor
Hero(Hero &h2){
cout<<"constructor called"<<endl;
char *ch = new char[strlen(h2.name) + 1];
strcpy(ch , h2.name);
this->name = ch;
this->health = h2.health;
this->level = h2.level;
}
void setName(char name[])
{
strcpy(this->name , name);
}
void print()
{
cout<<this->name<<endl;
cout<<health<<endl;
cout<<level<<endl;
}
~Hero()
{
cout<<"Descructor Called"<<endl;
}
};
int Hero::power = 10;
int main()
{
cout<<Hero::random()<<endl;
// static
// Hero h1
// dynamically
// Hero *h2 = new Hero();
// delete h2;
// cout<<sizeof(h1)<<endl;
// h1.health = 12;
// h1.level = 'g';
// char name[] = "babbar";
// h1.setName(name);
// h1.print();
// Hero h2(h1);
// h2.print();
// h1.name[0] = 'M';
// h1.print();
// h2.print();
// Copy Assignment operator
// h1 = h2;
// h1.print();
// h2.print();
}