-
Class example
Iam pretty new to OOP, but for a text game, would this be ok for
the player class? any suggestions appricaiated:
Code:
class Player
{
public:
Player(); // constructor
int health;
string name;
char weapon[15];
private:
bool palaive;
bool pdead;
};
Player::Player()
{
health = 100;
name;
weapon = "sword";
}
// in main
Player play
// call member
cout << Weapon is: " << play.weapon;
cout << "health is: " << play.health;
return 0;
}
-
Code:
#include <string>
#include <iostream>
using namespace std;
class Player
{
public:
Player(); // constructor
int health;
string name;
string weapon;
private:
bool palaive;
bool pdead;
};
Player::Player()
{
health = 100;
name;
weapon = "sword";
}
int main()
{
Player play;
cout << "Weapon is: " << play.weapon;
cout << "health is: " << play.health;
return 0;
}
-
1. You should be trying to make all your data private to the class, and provide access functions to manipulate that data.
2. char weapon[15];
Use std::string unless you have a good reason not to.
3. bool palaive;
bool pdead;
So if alive and dead are both true, what does that mean (zombie?)
They seem mutually exclusive states to me.
-
yes i understand, i think il put health and any integers in a private acsessor then use functions to acsess the data after initalizing them using a constructor