Hi,
@Avilius
No worries. I am sure you are not the only one which that has happened to - I am in that list too .... :+)
I had a bit of a Plan in mind for Chay(The OP), showing a way to create classes with private member variables, and not needing getters / setters.
Starting out simple, then adding in extra stuff as we go along. A Player with a
std::vector
of resources. A Resources Base class, and some derived classes. Add a Resources into the Player's vector. Then some kind of Shop class, with transfer of objects from that to the player. The possibility of a Transaction class. Then perhaps a component model framework.
@Ch1156
So the thinking behind this so far, is we have a player and some resources, which the Player can just grab, without any cost or anything. Think of Tomb Raider, where Lara can arrive at a location and pick up whatever object is there.
I don't have a compiler at the moment, so this is just typed in - hopefully not any errors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
//CPlayer.hpp
class CPlayer
{
private:
std::string m_Name;
std::vector<CResource *> pGear ; // Possessions, Equipment, stuff whatever
// Uses a smart pointer
public:
CPlayer(std::string Name); // put the definition of this into a .cpp file using an initialiser list
AcquireResource(CResource *pTheResource); // in the definition push_back the resource into the vector
};
|
Now the Resources Base class:
1 2 3 4 5 6 7 8 9
|
// CResource.hpp
class CResource
{
public:
virtual void PrintDetails() = 0; // pure virtual function must be defined in derived classes
};
|
A derived Resource class:
1 2 3 4 5 6 7 8 9 10 11 12
|
// CBag.hpp
class CBag : public CResource // aka Rucksack, Back Pack etc
{
private:
unsigned short m_Capacity; // Volume in litres
std::string m_Material; // PVC, Canvas etc could use an enum
public:
CBag (unsigned short Capacity, std::string Material); // put defintion in cpp file with initialiser list
void PrintDetails(); // std::cout each member variable
};
|
Now create another derived resource class that has some attributes to print out, don't for get to define a PrintDetails function which will do that.
Create a cpp file with main() in it. Create a Player Object (as unique smart pointer), and some instances of your derived Resource classes. Use the
Player->AddResource
function to add these resources into the player. The argument for this will be a smart pointer to the particular resource.
Next you can use the
PrintDetails
function to show information. Ideally this would be done from a new function in CPlayer that would use a range based for loop to iterate through the Gear vector, calling the PrintDetails function for each object in the vector.
I hope this enough to get you started, and we all look forward to seeing your code.