-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstructor intro in class.cpp
65 lines (51 loc) · 1.24 KB
/
Constructor intro in class.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
64
65
#include <iostream>
using namespace std;
class Hero {
private:
int health=100;
char level='A';
public:
Hero (){
cout<<"Constructor called\n";
}
int getHealth(){
return health;
}
void setHealth(int h){
health=h;
}
char getLevel(){
return level;
}
void setLevel(char l){
level=l;
}
};
int main(){
//static allocation
cout<<"Printing from static allocation\n";
Hero Nikhil;
cout<<Nikhil.getLevel()<<"\n";
Nikhil.setLevel('B');
cout<<Nikhil.getLevel()<<"\n";
cout<<Nikhil.getHealth()<<"\n";
Nikhil.setHealth(1000);
cout<<Nikhil.getHealth()<<"\n";
//dynamic allocation
cout<<"Printing from dynamic allocation\n";
Hero *b = new Hero;
cout<<(*b).getLevel()<<"\n";
(*b).setLevel('C');
cout<<(*b).getLevel()<<"\n";
cout<<(*b).getHealth()<<"\n";
(*b).setHealth(10000);
cout<<(*b).getHealth()<<"\n";
//dynamic allocation
cout<<"Printing from dynamic allocation\n";
cout<<(*b).getLevel()<<"\n";
b->setLevel('D');
cout<<(*b).getLevel()<<"\n";
cout<<(*b).getHealth()<<"\n";
b->setHealth(100000);
cout<<(*b).getHealth()<<"\n";
}