Help Involving Vectors/Class Inheritance

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
TheLividePianoist
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Thu Jan 29, 2009 5:48 pm

Help Involving Vectors/Class Inheritance

Post by TheLividePianoist »

Hello guys. So, I have a problem involving something that is over my head which inevitably has forced me to ask questions. I have a player class and a weapon class and for a while I've been trying to figure out how to make the first tie in with the latter using class inheritance. I've solved that but have run into something. I want my player class to be able to automatically draw/use information from the weapon class based on the current weapon the player has equipped. Take for example:

Real code mixed with pseudo code.

Code: Select all

player.pCurrWeap.SetCurrWeap(Glock); 
In my player.h file I declared Weapon pCurrWeap; as a public member.

Code: Select all

Glock.clipSize = 17; 
cout << player.pCurrWeap.GetClipSize(); << endl; 
I want it to tell me the ammo information which in this case is the amount of ammo in the Glock gun that is equipped without me having to set it using a function. Obviously, I would set all the variable information for the Glock weapon beforehand like say I made Glock a derived class of Weapon (which I wouldn't).

Code: Select all

class Glock : public Weapon
{
private:
           //all the information for the gun
public:
          Glock(); 
          ~Glock();
} 

Glock::Glock()
{
    x = 100;
    y = 100;
    //etc. 
} 
Well I'm sure your ways of doing it would probably involve vectors and pointers, a change up of the class for better inheritance...whatever you choose. I'd just really like to get past the stumbling block. I was also wondering whether or not I should make a vector for weapons but am not sure how I'd use that and work that in to use this. Like this:

Code: Select all

player.pCurrWeap(vector<Weapon> currWeap).GetClipSize() //get clip size for currently equipped weapon 
Not sure if I can even do this.

Anyways, thanks for your help.
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: Help Involving Vectors/Class Inheritance

Post by X Abstract X »

Hey there. I'm not completely sure I understand what you need but I hope this is what you meant. Good luck.

Code: Select all

#include <iostream>

enum WeaponType {
    GLOCK
};


class Weapon {
private:
    WeaponType _type;
    unsigned int _clipSize;

public:
    Weapon(WeaponType type, unsigned int clipSize) : _type(type),
                                                     _clipSize(clipSize) {}

    ~Weapon() {}

    unsigned int getClipSize() {
        return (_clipSize);
    }

    WeaponType getType() {
        return (_type);
    }
};


class Glock : public Weapon {
public:
    Glock() : Weapon(GLOCK, 16) {}
    ~Glock() {}
};


class Player {
private:
    Weapon* _weapon;

public:
    Player() : _weapon(0) {}

    ~Player() {
        delete _weapon;
    }

    void setWeapon(WeaponType type) {
        if (_weapon != 0) {
            if (_weapon->getType() == type) {
                return;
            }

            delete _weapon;
            _weapon = 0;
        }

        switch (type) {
            case GLOCK: _weapon = new Glock;
                        break;
        }
    }

    void printOutClipSizeOfCurrentWeapon() {
        if (_weapon != 0) {
            std::cout << "Clip size: " << _weapon->getClipSize() << "\n";
        } else {
            std::cout << "You aren't wielding a weapon dikhed!\n";
        }
    }
};
Last edited by X Abstract X on Mon Jul 05, 2010 4:30 pm, edited 1 time in total.
TheLividePianoist
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Thu Jan 29, 2009 5:48 pm

Re: Help Involving Vectors/Class Inheritance

Post by TheLividePianoist »

Thanks for replying. Two hours after doing my initial post, I figured it out and then two hours after that I ran into a problem again. Hopefully, I can use your code to help figure out my problem. I'll re-explain my problem just in case others would like to give it a try but what you have looks goods.

Example:

Code: Select all

Weapon Glock(14); //An instance of the Weapon class and the first parameter of the constructor is wClipSize.
player.pCurrWeap.SetCurrWeap(Glock); 
player.pCurrWeap.GetClipSize(); //Tells me the current clip size of the currently equipped weapon
Instead of me having to do:

Code: Select all

Weapon Glock(14)
player.pCurrWeap.SetCurrWeap(Glock);
player.pCurrWeap.GetClipSize(Glock); 
This is pseudo code but I wouldn't work cause the weapon the player holds is likely to be constantly changing when he picks up and drops a weapon. So I can't manually always have to put it in. Doing it this way would probably make me have to do something like this:

Code: Select all

if (player.pCurrWeap = Glock)
{
     //find out the weapon information for glock; 
}
And do that for every single weapon in the game.
What I did to get it to work is this (very messy, unprofessional code):

Code: Select all

//main.cpp
    Weapon Standard; 
    Standard.SetClipSize(17); 
    Standard.SetBulletDmg(4);

    player.pCurrWeap.GiveWeap(Standard);

    cout << player.pCurrWeap.GetClipSize() << endl;
    cout << player.pCurrWeap.GetBulletDmg() << endl;

//weapon.cpp
int Weapon::GetClipSize() 
{
    Weapon currWeap; //notice I have to declare this in both the Get functions. This is causing my next problem.
    return currWeap.wClipSize; 
} 
void Weapon::SetBulletDmg(int bulletDmg)
{
     wBulletDmg = bulletDmg;
}
int Weapon::GetBulletDmg()
{
       Weapon currWeap;
       return currWeap.wBulletDmg;
}
void Weapon::GiveWeap(Weapon currWeap) 
{ 
Weapon pCurrWeap = currWeap; 
}
The problem I'm having now is that when in main.cpp, I have to state this:

Code: Select all

player.pCurrWeap.GiveWeap(Standard);
before every get function I have or else it wont work. Like, It would work for the first get function because I declared that once but it wont work for the second unless I put that line of code in again.
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: Help Involving Vectors/Class Inheritance

Post by wearymemory »

Why are you giving your weapons to your weapons?
TheLividePianoist
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Thu Jan 29, 2009 5:48 pm

Re: Help Involving Vectors/Class Inheritance

Post by TheLividePianoist »

I have no idea except that I thought that in order for pCurrWeap to equal something, It would have to equal a Weapon variableexample;. I'm not sure what I should give it to? Should I declare a variable in my weapon class to take it?
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Help Involving Vectors/Class Inheritance

Post by mv2112 »

Is this what you mean?

Code: Select all

class Weapon
{
public:
      Weapon(int csize) {clipsize=csize;}
      Weapon() {}
      void SetWep(Weapon*wep) { *this=*wep;}
      void SetDamage(int d){damage=d;}
      int GetClipSize(){return clipsize;}
      ~Weapon(){}
protected:
      int clipsize,damage;
};

class Player
{
public:
Weapon CurrentWep;
};

int main()
{
Weapon Glock(16);
Player P;
P.CurrentWep.SetWep(&Glock);
std::cout<<P.CurrentWep.GetClipSize()<<"\n"; //this will print 16, i tested it
}
Last edited by mv2112 on Mon Jul 05, 2010 4:56 pm, edited 1 time in total.
TheLividePianoist
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Thu Jan 29, 2009 5:48 pm

Re: Help Involving Vectors/Class Inheritance

Post by TheLividePianoist »

Works perfectly MV, thanks a lot! To think, I was making it a lot more complicated than it called for.
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Help Involving Vectors/Class Inheritance

Post by xiphirx »

TheLividePianoist wrote:Works perfectly MV, thanks a lot! To think, I was making it a lot more complicated than it called for.
Yes, you just needed to include a pointer to a Weapon in your Player class :P
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Help Involving Vectors/Class Inheritance

Post by mv2112 »

xiphirx wrote:
TheLividePianoist wrote:Works perfectly MV, thanks a lot! To think, I was making it a lot more complicated than it called for.
Yes, you just needed to include a pointer to a Weapon in your Player class :P
Actually he had a weapon in his player class, but he wasnt setting it's values correctly.
*this=wep
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Help Involving Vectors/Class Inheritance

Post by XianForce »

Only suggestion I'll add is to have the SetWep function take a reference or pointer, rather than a copy. Optimizations ftw?
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Help Involving Vectors/Class Inheritance

Post by mv2112 »

XianForce wrote:Only suggestion I'll add is to have the SetWep function take a reference or pointer, rather than a copy. Optimizations ftw?
touche, edited the code i posted
TheLividePianoist
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Thu Jan 29, 2009 5:48 pm

Re: Help Involving Vectors/Class Inheritance

Post by TheLividePianoist »

Thanks more but can someone explain to me the reason for the pointers and or other stuff you added. What exactly does it do?
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: Help Involving Vectors/Class Inheritance

Post by wearymemory »

TheLividePianoist wrote:I have no idea except that I thought that in order for pCurrWeap to equal something, It would have to equal a Weapon variableexample;. I'm not sure what I should give it to? Should I declare a variable in my weapon class to take it?
It would make more sense to set your current Weapon to another using a method in your Player class. This was demonstrated in the code that X Abstract X posted.

Have you Googled "pointers" and searched the forums first?
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Help Involving Vectors/Class Inheritance

Post by XianForce »

TheLividePianoist wrote:Thanks more but can someone explain to me the reason for the pointers and or other stuff you added. What exactly does it do?
I'm going to assume this question is about the parameter change to reference or pointer.

So basically, anytime you pass a variable as a parameter, your not actually passing that variable. Your just passing a copy of that variable.

So here's a quick example to demonstrate:

Code: Select all

void function(int x)
{
     x = 3;
     std::cout << x << std::endl;
}

int main()
{
     int x = 2;
     std::cout << x << std::endl;

     function(x);

     std::cout << x << std::endl;

     return 0;
}
So at first glance, you'd probably expect the output to look something like:

Code: Select all

2
3
3
Which makes sense considering we assigned the value of 3 to x in function(int x). Too bad we didn't actually pass x, we passed 2. So the output should actually be:

Code: Select all

2
3
2
After function ends, the variable x that was set to 3 is removed from the stack. This whole idea is commonly known as passing by value.

So let's say you wanted to actually change the value of x, you can solve this by passing a pointer or reference to the object, rather than the object itself.

This means, a new object won't be created just to pass a parameter (which will probably give some undesired results in many cases anyways).


So it's more efficient to use references/pointers when passing objects, not too sure about primitive types (int, char, bool, float, etc).

Hope that helps.
Post Reply