How can i use getters and setters with math?

Pages: 1234
Hi,

Most of what I had to say in this post still applies:

http://www.cplusplus.com/forum/beginner/152947/2/#msg795099


I wouldn't bother putting "class" or "constructor" in the name of your files, just name files the same as the class name, for example I would have CResource.hpp and CResource.cpp

Also, I wouldn't bother with the Shop or Game class just yet, just concentrate on achieving out initial goal of a player owning some stuff. Then we can add other things in later.

Ch1156 wrote:
Ok so I made some classes, they are a little different from what you had but they are basically the same.


Well they are a little different - there are a number of things to add to get it to being similar to my code. I was hoping you could have started with my code, then add in your own definitions of the functions etc , and get that working.

Have to run .......
Hi, Sorry for yet another late response, I'm completely free now though so no more interruptions. I'll get rid of those classes and look over that post and get back to you.
Cool, good to see you are back :+D
I have a question from this line in your post: " Create a Player Object (as unique smart pointer)," What do you mean by smart pointer?
Hi,

There is lots on Google, here are the first few:

http://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one
http://en.wikipedia.org/wiki/Smart_pointer
http://en.cppreference.com/w/cpp/memory/shared_ptr
http://en.cppreference.com/w/cpp/memory/unique_ptr
http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique


For the CPlayer object, Imagine you will need a std::unique_ptr, as there will only be one player.

If you happen to have a compiler that does C++14 - you can use the std::make_unique function. Otherwise you can create one using new

If you don't have C++14, then I strongly suggest getting hold of clang++ version 3.5 which is part of llvm. Not sure how that is going fly with VS though (is that what you are using, from memory?). In my perfect world, I would steer away from VS and Microsoft. That's just my opinion, but I mention it because I think folks might be better off.

Hope all goes well :+)
ok, are you sure all these classes and stuff are the absolute most easiest rout to take? To be honest I'm just not feeling it, it's really hard to get back into programming right now for some reason. I want to do it but when I open the compiler I just cant bring myself to write anything :(
Last edited on
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.

Ok, so before I add anymore code I want to be completly sure. I have all of the classes you gave me in a prevous post plus a DerResource class with a void PrintDetails(); function in it. when you said derived resource class did you mean a class like i made named DerResource or a class that inherits from resource?

So from there i add some variables to print in the resource class, and then in main I add ???

Sorry It's hard for me to comprehend directions sometimes.


Maybe we could do some simpler classes, by that I mean something that has a clear hierarchy chain like animals or cars and go from there. Sorry but that would be much easier. Maybe we could do animals or cars and then there could be like a car dealer/pet shop etc, that would be easier to understand. I can make the pet shop/car dealer class and the animal classes and we can go from there, what do you think?
Last edited on
Maybe we could do some simpler classes, by that I mean something that has a clear hierarchy chain like animals or cars and go from there.


The classes I have shown have a clear hierarchy, and are simple because there is no interaction between any of the resources. If we were to make up some other hierarchy, it wouldn't be any any easier. So I still think it is worth it to carry on, seen as have already written the classes.

Sorry It's hard for me to comprehend directions sometimes.


Even though you have this struck out, I am still going to comment on it.

Just concentrate on 1 paragraph at a time, and 1 sentence at a time.

I have all of the classes you gave me in a prevous post plus a DerResource class with a void PrintDetails(); function in it. when you said derived resource class did you mean a class like i made named DerResource or a class that inherits from resource?


Ok, great - can you post them all here?

The base class is CResource, the others are as I outlined in items 1 to 3 in my previous post.

Just wondering if you could keep the same names as I had for everything ? That way we can be on the same page.

Hope this helps :+)

oh ok, here is what I have so far:

Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

#include "CResource.h"
#include "Backpack.h"
#include "CPlayer.h"
#include "Prototypes.h"

using namespace std;

Player::Player(string name)
{

}

int main()
{

    return 0;
}



Resource.h

1
2
3
4
5
6
7
8
9
10
#ifndef RESOURCE_CLASS_H_INCLUDED
#define RESOURCE_CLASS_H_INCLUDED

class CResource
{
    public:
        virtual void PrintDetails() = 0;
};

#endif // RESOURCE_CLASS_H_INCLUDED 



CPlayer.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef PLAYERCLASS_H_INCLUDED
#define PLAYERCLASS_H_INCLUDED

#include <vector>

class CPlayer
{
    private:
        std::string m_Name;
        std::vector<Resource *> pGear;

    public:
        CPlayer(std::string Name);
        void AcquireResource(Resource *pTheResource);
};

#endif // PLAYERCLASS_H_INCLUDED 



Backpack.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef BACKPACKCLASS_H_INCLUDED
#define BACKPACKCLASS_H_INCLUDED

#include "Resource.h"
#include <string>

class Backpack : public Resource
{
    private:
        unsigned short m_Capacity;
        std::string m_Material;

    public:
        Backpack(unsigned short Capacity, std::string Material);
        void PrintDetails();
};
#endif // BACKPACKCLASS_H_INCLUDED 



DerResource.h

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef DERRESOURCE_H_INCLUDED
#define DERRESOURCE_H_INCLUDED

class DerResource
{
    public:
        void PrintDetails();

    private:

};

#endif // DERRESOURCE_H_INCLUDED 
Ok, I will show some editing :

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
28
29
30
31
#include <iostream>
#include <string>

#include "CResource.h" // I name my headers with .hpp => header with cpp code
#include "Backpack.h"
#include "CPlayer.h"
#include "Prototypes.h"

using namespace std; // put std:: before every std thing
// the following goes into CPlayer.cpp
Player::Player(string name)
{

}

// Create all your other classes first, before dealing with this stuff

int main() {

    // seen as this will be a small program for now, just create object on the stack
    
    // create a CPlayer object that calls the right constructor

    // create some resources, and push_back into a std::vector

   // use the CPlayer AcquireResource function in a loop to add all the resources into the Gear container 

   // print out the details of all resources in the Gear container
    
    return 0;
}


With the filenames , always name them the same as the class name - so Resource.h should be CResource.hpp
Apart from that, that file is good as it is.

Get rid of Backpack.h and DerResource.h

You have #ifndef PLAYERCLASS_H_INCLUDED which probably means you named the file PlayerClass.h - rename it to CPlayer.hpp. Then you won't have problems when you include it somewhere.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef CPLAYER_HPP_INCLUDED
#define CPLAYER_HPP_INCLUDED

#include <string>
#include <vector>

class CPlayer
{
    private:
        std::string m_Name;
        std::vector<CResource *> Gear;  // must be consistent with class names 

    public:
        CPlayer(std::string Name);
        void AcquireResource(CResource *pTheResource);
};

#endif // CPLAYER_HPP_INCLUDED  


Right oh, now you can create the resource classes as I outlined previously in items 1 to 3 in my second to last post.

Also create .cpp files for each of the classes you already have, and define a constructor for each.

I will create a project of my own, just to make sure all the code works :+)


Ok i got all the files re named and got rid of the ones that werent needed, what do you mean by this?

// create a CPlayer object that calls the right constructor

I assume you mean just create a constructor prototype in the classes and then add the constructor with a body in a .cpp file but i just want to be sure.
Last edited on
Hi,

Ok, I imagine you have created all of your resource classes, this needs to happen before you do anything in main().

That way, the intellisense will work for you, you are working with things that exist, rather than things that don't.

I assume you mean just create a constructor prototype in the classes and then add the constructor with a body in a .cpp file but i just want to be sure
.

Yes, there needs to be a constructor declaration in the .hpp, and a implementation of it in the .cpp file.

// create a CPlayer object that calls the right constructor

This means create an actual object in main(), of type CPlayer, that calls the constructor with a std::string name as an argument, so that the m_Name is initialised.
I have one resource class which is CResource, what others do i need?
http://www.cplusplus.com/forum/beginner/152947/3/#msg809045


These are the classes:

TheIdeasMan wrote:
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 {};



Read the rest of that post to remind yourself of the member variables and other details.

:+)


oh sorry, guess I should learn to read better before I start programming :P So all those classes go inside their own .hpp so medical gores in medical.hpp vehicle.hpp etc correct? or does it all go under CResource? I understand the part where you said: "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 ." So thats no problem, So I just make that a pure virtual function in CResource right?
So all those classes go inside their own .hpp so medical gores in medical.hpp vehicle.hpp etc correct?


Yes, almost :+) name the files CMedical.hpp, CVehicle.hpp etc, so we keep the exact same naming convention for all our file names, That is - same as the class name, with .cpp or .hpp

So I just make that a pure virtual function in CResource right?


The PrintDetails pure virtual function goes into the CResource class as a declaration, but it is a pure virtual function so it has no implementation - that is: there is no CResource::PrintDetails in CResource.cpp. We do need a getName function though. Call this function as CResource::getName(); in the other classes. The destructor needs to be virtual for this class too.

However, all the classes at the bottom of the inheritance tree (leaf classes) need to have their own implementation of PrintDetails. That is what having a pure virtual function means - an implementation of it must be provided in a derived class. derived classes.

For example CFourWheelDrive::PrintDetails() should exist in it's .cpp file. Just have this function, and the others std::cout the values of each member variable, and any from any class higher up the tree. CFourWheelDrive::PrintDetails() needs to print the values of m_EngineCpacity and m_NumOfPassengers, and m_Name in CResource

You will need to provide get functions in the CVehicle class so you can get at these values () as they are private:. Same for the other similar classes.

The member variable m_Size in CMedical should be an enum, so we can have small, medium & large Medipacks & adrenalin shots. I like to use the C++11 enum class, as they are better than the plain old enum. Look in the reference about them. Although an ordinary enum would be perfectly fine.

Hope all goes well :+)
Last edited on
Ok so this is what I have so far, it wont seem to let me create a print details function in the other classes that derive from CResource gfor some reason, either that or i just forgot how to do it.

Also my compiler (Code::Blocks) wasnt taking to kindly to the .hpp extension so I had to keep it .h.

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

#include "CResource.hpp"
#include "CPlayer.hpp"

using namespace std;

int main()
{



    return 0;
}



CVehicle.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef CVEHICLE_HPP_H
#define CVEHICLE_HPP_H

#include "CResource.h"

class CVehicle : public CResource
{
    public:
        CVehicle();
        ~CVehicle();

    private:

};

#endif // CVEHICLE_HPP_H 



CVehicle.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "CVehicle.h"

CVehicle::CVehicle()
{
    //ctor
}

CVehicle::~CVehicle()
{
    //dtor
}



CMedical.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef CMEDICAL_HPP_H
#define CMEDICAL_HPP_H

#include "CResource.h"

class CMedical : public CResource
{
    public:
        CMedical();
        ~CMedical();

    private:
};

#endif // CMEDICAL_HPP_H 


CMedical.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "CMedical.h"

CMedical::CMedical()
{
    //ctor
}

CMedical::~CMedical()
{
    //dtor
}



CPlayer.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef CPLAYER_HPP_INCLUDED
#define CPLAYER_HPP_INCLUDED

#include <string>
#include <vector>
#include "CResource.h"

class CPlayer : public CResource
{
    private:
        std::string m_Name;
        std::vector<CResource *> Gear;

    public:
        CPlayer(std::string Name);
        void AcquireResource(CResource *pTheResource);
};

#endif // CPLAYER_HPP_INCLUDED 


CPlayer.cpp

1
2
3
4
5
6
#include "CPlayer.h"

CPlayer::CPlayer(std::string m_name)
{

}



CResource.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef CRESOURCE_H_INCLUDED
#define CRESOURCE_H_INCLUDED

class CResource
{
    public:
        virtual void PrintDetails() = 0;
        CResource();
        virtual ~CResource();

        void GetName();
};

#endif // RESOURCE_CLASS_H_INCLUDED 


CResource.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "CResource.h"

CResource::CResource()
{

}

CResource::~CResource()
{

}
Last edited on
Hi,

Sorry for the brief reply, it's late at my end :+)


Ok so this is what I have so far, it wont seem to let me create a print details function in the other classes that derive from CResource gfor some reason, either that or i just forgot how to do it.

Also my compiler (Code::Blocks) wasnt taking to kindly to the .hpp extension so I had to keep it .h.


What are the compiler errors when you try to make a PrintDetails() function? Do the classes like CVehicle & CMedical & CWeapon have a PrintDetails() function that does nothing? They should. Then the leaf classes have a PrintDetails() that std::cout all the values of the member variables.

There seems to be a bit of confusion regarding the .hpp files: The header guards imply that you had a .hpp file, but in some places you include a .h file & others a .hpp file. Must be consistent with names of everything. Did you use the new file wizard? Near the bottom left there is a place to name the header file extension.

CPlayer does not inherit from CResource. Public inheritance means a "IS A kind of" relationship. What we are going to do is have a CResource as a member variable (which you have) and that means a "HAS A" relationship.

I will try to get some time over the weekend to show some of my code - yet to write.

Regards
Hi,

I wrote some code using Code::Blocks 12.11 for you to look at, and add to.

CResource.hpp

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
28
29
30
31
32
#ifndef CRESOURCE_HPP
#define CRESOURCE_HPP

#include <string>



class CResource
{
    public:

        CResource(const std::string& Name);

        virtual ~CResource();

        /** Access m_Name
         * \return The current value of m_Name
         */
         const std::string& Getm_Name() const;

        virtual void PrintDetails() = 0  ;




    protected:

    private:
        std::string m_Name; //!< Member variable "m_Name"
};

#endif // CRESOURCE_HPP 


CResource.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>

#include "CResource.hpp"

CResource::CResource(const std::string& Name) :
    m_Name(Name)
{
    //ctor
}

CResource::~CResource()
{
    //dtor
}


 const std::string& CResource::Getm_Name() const  {
    return m_Name;
    }


CVehicle.hpp

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
28
29
30
31
32
33
34
35
36
37
#ifndef CVEHICLE_HPP
#define CVEHICLE_HPP

#include "CResource.hpp"


class CVehicle : public CResource
{
    public:


        /** Default destructor */
        virtual ~CVehicle();

        /** Access m_EngineCapacity
         * \return The current value of m_EngineCapacity
         */
         unsigned Getm_EngineCapacity() const;

        /** Access m_NumSeats
         * \return The current value of m_NumSeats
         */
         unsigned short Getm_NumSeats() const;

         virtual void PrintDetails()  ;

    protected:
 /** Default constructor */
        CVehicle(const std::string& Name,
                 const unsigned EngineCapacity,
                 const unsigned short NumSeats);
    private:
        unsigned m_EngineCapacity; //!< Member variable "m_EngineCapacity"
        unsigned short m_NumSeats; //!< Member variable "m_NumSeats"
};

#endif // CVEHICLE_HPP 


CVehicle.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
#include "CVehicle.hpp"

CVehicle::CVehicle(const std::string& Name,
                   const unsigned EngineCapacity,
                   const unsigned short NumSeats)
                   :
    CResource::CResource(Name),
    m_EngineCapacity(EngineCapacity),
    m_NumSeats(NumSeats)
{
    //ctor
}

CVehicle::~CVehicle()
{
    //dtor
}

 unsigned CVehicle::Getm_EngineCapacity() const {
     return m_EngineCapacity;
     }

 unsigned short CVehicle::Getm_NumSeats() const{
     return m_NumSeats;
     }
void CVehicle::PrintDetails()  {}


CFourWheelDrive.hpp
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
28
29
30
31
32
33

#ifndef CFOURWHEELDRIVE_HPP
#define CFOURWHEELDRIVE_HPP

#include "CVehicle.hpp"


class CFourWheelDrive : public CVehicle
{
    public:
        /** Default constructor */
        CFourWheelDrive(const std::string& Name,
                        const unsigned EngineCpacity,
                        const unsigned short NumSeats,
                        const bool DiffLock);

        /** Default destructor */
        virtual ~CFourWheelDrive();

        /** Access m_DiffLock
         * \return The current value of m_DiffLock
         */
        bool Getm_DiffLock() { return m_DiffLock; }

        virtual void PrintDetails()  override;
    protected:

    private:

        bool m_DiffLock; //!< Member variable "m_DiffLock"
};

#endif // CFOURWHEELDRIVE_HPP 



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
28
29
30
31
32
33
34
#include "CFourWheelDrive.hpp"
// #include "CResource.hpp"


#include <iostream>



CFourWheelDrive::CFourWheelDrive(const std::string& Name,
                                  const unsigned EngineCapacity,
                                  const unsigned short NumSeats,
                                  const bool DiffLock)
                                  :

    CVehicle::CVehicle(Name, EngineCapacity, NumSeats),
    m_DiffLock(DiffLock)
{
    //ctor
}

CFourWheelDrive::~CFourWheelDrive()
{
    //dtor
}


 void CFourWheelDrive::PrintDetails()  {
     //class CResource;

    std::cout << "Name " << CResource::Getm_Name() << "\n";
    std::cout << "Engine Capacity " << CVehicle::Getm_EngineCapacity() << "\n";
    std::cout << "Number of Seats " << CVehicle::Getm_NumSeats() << "\n";
    std::cout << "DiffLock " << m_DiffLock <<  "\n\n";
}


CPlayer.hpp
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
28
29
30
31
32
33
34
35
#ifndef CPLAYER_HPP
#define CPLAYER_HPP

#include <string>
#include <vector>

#include "CResource.hpp"


class CPlayer
{
    public:
        /** Default constructor */
        CPlayer(  std::string Name);

        /** Default destructor */
        ~CPlayer();

        /** Access m_Name
         * \return The current value of m_Name
         */
        const std::string& GetName() const;
         bool AcquireResource( CResource* Resource) ;

         void ShowResources() ;

    protected:

    private:
        std::string& m_Name; //!< Member variable "m_Name"

        std::vector< CResource *> Gear;
};

#endif // CPLAYER_HPP 


CPlayer.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
28
29
30
31
#include <iostream>
#include "CPlayer.hpp"


CPlayer::CPlayer( std::string Name)
    :
    m_Name  (Name)
{}

CPlayer::~CPlayer()
{
    //dtor
}

const std::string& CPlayer::GetName() const {
     return m_Name;
     }

     bool CPlayer::AcquireResource( CResource* Resource) {

        Gear.push_back(Resource); // should use std::move here
        return true;
     }

void CPlayer::ShowResources()  {

    for ( CResource * GearItem : Gear ) {
        GearItem->PrintDetails();
    }

}


DinosaurArena.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 "CFourWheelDrive.hpp"
#include "CPlayer.hpp"


int main() {

    CFourWheelDrive MyFourby("MyFourby", 4200, 7, true);
    CFourWheelDrive YourFourby("YourFourby", 5000, 7, false);



    CPlayer Me("TheIdeasMan");

    Me.AcquireResource(&MyFourby);
    Me.AcquireResource(&YourFourby);

    Me.ShowResources();


return 0;
}









So now you can make your classes for CMedical, CMediPack, CAdrenalinShot, CWeapon, CRifle, CGrenade .

See what I did for the classes I have shown, and do something similar for the ones you create. There is no need to change anything in CResources or CPlayer. Create some new resources in main(), and use the AcquireResources function.

If you have any questions, just ask :+)

If anyone else has any suggestions, they would be welcome. I would have liked to const references more(so now I have pointers :+( ), but had trouble doing that. Will try with a different compiler and see what happens.





Pages: 1234