Skip to content

Instantly share code, notes, and snippets.

@zachlatta
Created November 17, 2012 18:53
Show Gist options
  • Save zachlatta/4098820 to your computer and use it in GitHub Desktop.
Save zachlatta/4098820 to your computer and use it in GitHub Desktop.
Character Class
#include "../Header/Character.h"
#include <iostream>
#include <fstream>
/********************
* PRIVATE METHODS *
********************/
void Character::setExperience(int experience)
{
this->experience = experience;
}
/*******************
* PUBLIC METHODS *
*******************/
std::string getName()
{
return name;
}
std::string getRace()
{
return race;
}
int Character::getExperience()
{
return experience;
}
int Character::getLevel()
{
// TODO: Add logic to calculate level from experience
return 0;
}
void Character::addExperience(int experience)
{
this->experience = experience;
}
void Character::save()
{
std::ofstream saveFile;
saveFile.open("save.dat");
// Writes data from class into file
saveFile << experience << std::endl;
saveFile.close();
}
void Character::load()
{
std::ifstream saveFile;
std::string buffer;
saveFile.open("save.dat", std::ios::in);
// Reads data from file into class
getline(saveFile, buffer);
experience = atoi(buffer.c_str());
saveFile.close();
}
#include <iostream>
#ifndef CHARACTER_H
#define CHARACTER_H
class Character
{
private:
std::string name;
std::string race;
int experience;
void setExperience(int);
public:
std::string getName();
std::string getRace();
int getExperience();
int getLevel();
void addExperience(int);
void save();
void load();
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment