//C++ Made Easy HD 26 - Introduction to Classes
//Michael LeCompte
#include <iostream>
#include <string>
using namespace std;
class Player {
public:
Player() {
cout << "1st overload" << endl;
}
Player(int Health){
cout << "2nd overload" << endl;
this->health = Health;
}
Player(int Health, int attPow){
cout << "3nd overload" << endl;
this->health = Health;
attackPower = attPow;
}
~Player() {
cout << "Player instance destroyed" << endl;
}
//Mutators
void setHealth(int val) {
health = val;
}
void setAttPow(int val) {
attackPower = val;
}
void setDef(int val) {
defense = val;
}
void setXp(int val) {
xp = val;
}
//Accessors
int healthVal() {
return health;
}
int attPowVal() {
return attackPower;
}
int defVal() {
return defense;
}
int xpVal() {
return xp;
}
private:
int health;
int attackPower;
int defense;
int xp;
};
int main() {
Player player[4] = {Player(2, 5), Player(65, 2), Player(2), Player(1)};
cout << player[0].healthVal() << endl;
cout << player[1].healthVal() << endl;
cout << player[2].healthVal() << endl;
system("pause");
return 0;
}
From the code above the lines I am focusing on are the this->health = Health
lines. I'm wondering why I need to use this->health
instead of health = Health
. I know it has something to do with the fact that I'm creating new Player
objects with an array (I'm doing a tutorial on it). I just don't understand why it makes it so I have to use this->
or how it works. Thanks.