-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault_constructor.cpp
More file actions
49 lines (44 loc) · 943 Bytes
/
default_constructor.cpp
File metadata and controls
49 lines (44 loc) · 943 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
// Section 13 - The Default Constructor
#include <iostream>
#include <string>
class Player {
private:
std::string name;
int health;
int xp;
public:
void set_name(std::string name_val) {
name = name_val;
}
std::string get_name() {
return name;
}
// overloaded constructors
Player() {
// default
name = "New Player";
health = 100;
xp = 0;
}
Player(std::string name_val, int health_val, int xp_val) {
// three arg constructor
name = name_val;
health = health_val;
xp = xp_val;
}
~Player() {
// destructor
std::cout << name << " has died." << std::endl;
}
};
int main() {
{
Player player1;
player1.set_name("Devere");
std::cout << "Player 1: " << player1.get_name() << std::endl;
Player player2("Tuitty", 500, 0);
std::cout << "Player 2: " << player2.get_name() << std::endl;
}
std::cout << "\nGAME OVER!" << std::endl;
return 0;
}