ok, are you sure all these classes and stuff are the absolute most easiest rout to take? |
Yep, what we are trying to do is quite simple. There are going to be some resources classes, but they are all pretty trivial, then there is the
CPlayer
class, and that's it for now. I am trying to demonstrate something that is simple, and simple to extend - so that we can create more types of objects and more of them, but the the way we deal with them is still the same.
Try not to think that more classes represents a whole lot of complexity - it doesn't have to be that way. Think of kitchen appliances, in ours we have an oven, microwave, fridge, cutlery, crockery, dishwasher & blender. So each of those things is simple when you think of just one of them at a time. The way they interact with each other is pretty simple too. One can look at the overall picture where each thing is a "black box", then examine each thing on it's own.
So that is how we think about (design) our classes too. Make a list of 2 or 3 different types of things with 2 or 3 examples of each. I had Rifle, Grenade, Medipack, Adrenaline, FourWheelDrive, and JetSki.
Now separate these things into groups. It's helpful to do this on pen & paper - draw boxes for each class & lines between them to show how they are related. I have named the classes with code below (because I couldn't draw a diagram here), but hold off writing any code yet we have to get a few things sorted first :+)
1. Rifle & grenade are weapons and a weapon is a resource, so we have:
class CResource {};
1 2 3
|
class CWeapon : public CResource {};
class CRifle : public CWeapon {};
class CGrenade : public CWeapon {};
|
2. Medipack & Adrenaline are medical things, so:
1 2 3
|
class CMedical : public CResource {};
class CMedipack : public CMedical {};
class CAdrenaline : public CMedical {};
|
3. FourWheelDrive, and JetSki are vehicles, so:
1 2 3
|
class CVehicle : public CResource {};
class CFourWheelDrive : public CVehicle {};
class CJetSki : public CVehicle {};
|
So now that you have a diagram drawn up of this inheritance tree, you can look at each of these classes individually to decide about 2 things: 1 what attributes (member variables) will it have? ; 2 What functions (interface) will it have ? Just choose 2 or 3 attributes for now to keep with our idea of being simple.
Now identify what attributes there are in common. Now they all could have a
Name
. Also the
CFourWheelDrive
and
CJetSki
both have an
EngineCpacity
and
NumOfPassengers
.
CMediPack
and
CAdrenaline
both could have small, medium and large sizes - so we have a
Size
variable , plus an enum for small, medium and large.
Next is to push the common names as high up the tree as we can. So
Name
could go all the way up to
CResource
.
EngineCpacity
and
NumOfPassengers
goes into
CVehicle
and
Size
goes into
CMedical
.
OK, now we are onto the functions. Each class needs a constructor which takes arguments for each of it's member variables and all the member variables for all it's parent classes too. Do this with an initialiser list, make sure to call all the parent class constructors (in order), all the way to the top of the tree. That way, we are going to make a complete object.
For the
CWeapon
,
CMedical
, and
CVehicle
classes, we don't want to be able to create an object of that type because one of those on it's own doesn't make sense, they are just place holders, so make their construcotrs
protected:
. The other way to do this is to provide a pure virtual function, but there isn't really an appropriate one for these classes.
For our example we are going to have a
PrintDetails
function, so each class needs one of these too. Just have it
std::cout
the value of each member variable, and all the parent class member variables too.
One can analyse functions too, any common ones should be pushed up the tree as well. The only one of those we have is kind of
PrintDetails
. The name is the same but each one of them is different and we also want to ensure that we have an easy way to call these functions. So we make the function a pure virtual, put this line into the
CResource
header file:
virtual void PrintDetails() = 0; // pure virtual function with = 0
Read the tutorial about pure virtual functions.
I haven't described the CPlayer class in detail, but I gave an outline of it previously.
Right, now that we have analysed the attributes and functions, we are ready to write our code. I hope I have given good instructions and that you can have a go at doing this.
It's late at my end, I have work tmrw early - so I wish you good luck - any problems just post :+D
Btw, I am not the only one that can help you - there are plenty of others that are way more knowledgeable than me.