Class/Object Array?

This is a discussion on Class/Object Array? within the C++ Programming forums, part of the General Programming Boards category; I am just now learning about classes and objects and arrays and well, everything, and I need to know how ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    6

    Question Class/Object Array?

    I am just now learning about classes and objects and arrays and well, everything, and I need to know how to make an array out of a set of class member variables. I used to program in qbasic and I did it there but I cant remember how it has been too long...here is a snipet of code so I can clarify what I am asking:


    class Unit
    {
    private:
    float Id;
    int TypeId;
    int MaxHp;
    int Hp;
    int Range;
    int Power;
    int Move;
    int Cost;
    int Type;

    public:
    //to create a new unit
    void CreateUnit(int TypeId)

    //to delete an old unit
    void DeleteUnit(float Id)
    };

    Bassically I want to be able to do this (what I did in qbasic):

    Unit[1].Hp = Unit[1].Hp - 20

    as an example of unit id number 1 having its hp decreased by 20 points... then still have 2,3,4,5,6 and so on, with access to all of the member variables. I have no way to compile right now(am at work) so I figured I would post and see If I get an answer before I try to code something.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Well all you do is this declare an array of objects like so
    Code:
    class Unit
    {
    public://needed to change to public so you can access them would 
    be better to make accessor functions but i didn't want to take the time :)
    float Id;
    int TypeId;
    int MaxHp;
    int Hp;
    int Range;
    int Power;
    int Move;
    int Cost;
    int Type;
    
    
    //to create a new unit
    void CreateUnit(int TypeId)
    
    //to delete an old unit
    void DeleteUnit(float Id)
    };
    
    int main(void)
    {
      unit myClass[10];
      myClass[1].hp=myClass[1].hp-20;
      return 0;
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    6
    So I just declare the array outside the class...could I do it inside the class? Also is there a way I can gradually increase the array size without rewriting a new array in its place (thus keeping all the info already stored)?

    Thanks...

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Well I don't see why you would need to declare it inside of the class itself. As for increasing the array size I'm sure there is a way but ill leave that up to more expericanced people of that region than me(probably something in the STL). The way it looks though I would just make a maximum unit amount that you can have and then check if the player(user) has reached that maximum.
    Woop?

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    6
    Yeah that was my fist idea, bit I figured that I dont really want a max limit cause it will either be reached(in where no more units can be created), or never be reached, (and just use up extra memory). can you declare 2 arrays like:

    array[0 to 9] and another [10 to 20] after it reaches# 9, or would that erase 0 to 9?

    That prolly wont work exactly like I have it thats just how in worked in QB, I assume you get the idea though....if the maximum is reached it would sortof overflow into another of itsef increasing the size...Ihave no Idea if that would work

  6. #6
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    http://cslibrary.stanford.edu/103/

    or, somebody besides me could show you how to use a vector

    maybe i should learn some of this c++ garbage
    .sect signature

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    As you have it currently defined, a unit object should consist of a single unit's attributes. A unit object should not internally collect together an array of units.

    I would probably look at a wrapper class that consists of an STL container storing your multiple Unit members. This would be a better place to put your CreateUnit and DeleteUnit member functions as well.

    Code:
    //Stores information about a single Unit
    class Unit
    {
    private:
    float Id;
    int TypeId;
    int MaxHp;
    int Hp;
    int Range;
    int Power;
    int Move;
    int Cost;
    int Type;
    
    public:
    };
    
    //Stores information about a collection of many Unit objects
    class Units
    {
    private:
        vector<Unit> m_Units;
    
    public:
    //to create a new unit and stores it in the vector
    void CreateUnit(int TypeId)
    
    //to delete an old unit from the internal vector
    void DeleteUnit(float Id)
    };
    A vector, or other suitable STL container, i.e. perhaps a deque or set, will be able to grow dynamically to suit your needs. The Units class would store all of your Unit objects and also have the public member functions to create and delete new Unit objects from this vector.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21