-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticdynamic.cpp
More file actions
55 lines (45 loc) · 991 Bytes
/
staticdynamic.cpp
File metadata and controls
55 lines (45 loc) · 991 Bytes
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
#include <iostream>
using namespace std;
class Hero
{
private:
int health;
public:
char level;
// getter and setter
void setLevel(char ch)
{
level = ch;
}
void setHealth(int h)
{
health = h;
}
char getLevel()
{
return level;
}
int getHealth()
{
return health;
}
};
int main()
{
// static allocation
Hero h1;
h1.setHealth(100);
h1.setLevel('A');
cout << "Health of hero h1 is : " << h1.getHealth() << endl;
cout << "Level of hero h1 is : " << h1.getLevel() << endl;
// dynamic allocation
Hero *h2 = new Hero;
(*h2).setHealth(90);
(*h2).setLevel('B');
// one way
cout << "Health of hero h2 is : " << (*h2).getHealth() << endl;
cout << "Level of hero h2 is : " << (*h2).getLevel() << endl;
// another way
cout << "Health of hero h2 is : " << h2->getHealth() << endl;
cout << "Level of hero h2 is : " << h2->getLevel() << endl;
}