Using getters and setters vs friend and others
Sep 21, 2013 at 2:14am UTC
Ok so i have a game and im unsure of what to do here, I have been told that there are many different ways of approaching a problem with classes but im not sure which one is best, i dont even know what some of the ways do and why. When should i use public memebers vs private, frendship and getters and setters?
main.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
#include <iostream>
#include "Player_Class.h"
#include "Weapon_Class.h"
#include "Prototypes.h"
using namespace std;
Player::Player(string PLAYERNAME, bool ISFIRSTSTARTUP): playerName(PLAYERNAME),
isFirstStartup(ISFIRSTSTARTUP)
{
}
Player::~Player()
{
}
int main()
{
Player player("Default" , false );
Weapon weapon;
SaveGame(player, weapon);
return 0;
}
Player_Class.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ifndef PLAYER_CLASS_H_INCLUDED
#define PLAYER_CLASS_H_INCLUDED
#include <string>
class Player
{
public :
Player(std::string PLAYERNAME, bool ISFIRSTSTARTUP);
~Player();
private :
std::string playerName;
bool isFirstStartup;
};
#endif // PLAYER_CLASS_H_INCLUDED
prototypes.h
1 2 3 4 5 6 7 8 9 10
#ifndef PROTOTYPES_H_INCLUDED
#define PROTOTYPES_H_INCLUDED
#include "Weapon_Class.h"
void CheckStartup(Player player, Weapon weapon);
void SaveGame(Player player, Weapon weapon);
void LoadGame(Player player, Weapon weapon);
#endif // PROTOTYPES_H_INCLUDED
GameSetup.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <fstream>
#include "Player_Class.h"
#include "Weapon_Class.h"
using namespace std;
void SaveGame(Player &player, Weapon &weapon)
{
ofstream SaveFile;
SaveFile.open("Data.txt" );
player.isFirstStartup = true ;
SaveFile << player.playerName << endl;
SaveFile << player.isFirstStartup << endl;
}
void LoadGame(Player &player, Weapon &weapon)
{
ifstream LoadFile;
LoadFile.open("Data.txt" );
}
Weapon_Class.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef WEAPON_CLASS_H_INCLUDED
#define WEAPON_CLASS_H_INCLUDED
#include <string>
class Weapon
{
public :
Weapon();
~Weapon();
private :
std::string weaponType;
std::string ammunitionType;
int ammunitionPrice;
};
#endif // WEAPON_CLASS_H_INCLUDED
Topic archived. No new replies allowed.