Classes help

This is a discussion on Classes help within the C++ Programming forums, part of the General Programming Boards category; I want to consolodate these variables into classes and I don't know how. Variables are grouped for the classes. Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105

    Classes help

    I want to consolodate these variables into classes and I don't know how.
    Variables are grouped for the classes.

    Code:
    string choice;
    
    /*Class stats*/
    int hp;
    int weapon;
    int attack;
    int dmg;
    int gold;
    int startpiont;
    
    /*Class enemy*/
    int enemyhp;
    int enemy;
    string enemyname;
    int dodge;
    int enemymaxdmg;
    int weakness;
    
    /*Class easteregg*/
    bool computer;
    bool motherboard;
    bool floppydrive;
    bool compucase;
    bool demodisk;
    bool guessdisk;
    bool quizdisk;
    bool calculatedisk;
    bool rpsdisk;
    bool stickdisk;
    
    /*Class meats*/
    int bacon;
    int rbeef;
    int bbqpork;
    int bbqbeef;
    int chicken;
    int porkchop;
    int steak;
    
    /*Class fruit*/
    int  apple;
    int orange;
    int pear;
    int plum;
    int pinapple;
    int peach;
    int papaya;
    int mango;
    
    /*Class otherfood*/
    int smsand;
    int lgsand;
    int cheese;
    int candy;
    
    /*Class items*/
    bool broom;
    bool rope;
    bool grapple;
    bool goldplate;
    bool goldgoblet;
    bool goldbowl;
    bool goldflatware;
    bool bow;
    bool handgrenade;
    bool stairboots;
    
    /*Class traps*/
    int trapdmg;
    int spikepit;
    This is just the variables, so you can see why I want to consolodiate them.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by DarkAlex View Post
    I want to consolodate these variables into classes and I don't know how.
    Well, what do you know about classes? Have you read the tutorial on classes on this site, or any of the hundred or so that you can get to from a web search, or what your C++ book has to say about them?

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    I just read it and it's really confusing. I just want the variables to take up less space in my program and don't know how to set up the classes to do that.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you just want to group them together, a struct is all you need:
    Code:
    struct stats
    {
        int hp;
        int weapon;
        int attack;
        int dmg;
        int gold;
        int startpiont;
    };
    If you want to create real objects out of them, put them in a class and add member functions to the class that manipulate those member variables:
    Code:
    class stats
    {
    public:
        // Add some functions here
    
    private:
        int hp;
        int weapon;
        int attack;
        int dmg;
        int gold;
        int startpiont;
    };

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    I just want the variables to take up less space in my program and don't know how to set up the classes to do that.
    The goal of classes is not to reduce your program size, but to help you manage the complexity of your program. (In the words of Stroustrup, "to help you organize your code and to reason about your programs".)

    Consequently, what classes you write and how they relate to each other depends on what problem you are trying to solve and what aspects of that problem you are trying to model.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    I just want it to be less confusing for anyone looking at my code. Would I use a special method to use the variables in a struct?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    I just want it to be less confusing for anyone looking at my code.
    That's good. The problem I see is that your variable names sound like class names, so combining them into a single class looks suspect.

    That is why I am asking about what problem you are trying to solve. Without knowing the context, all we can do is to tell you to place what you think is related into a POD (Plain Old Data) struct. It is tough to suggest how to design a class because it sounds like you want a class hierarchy, and if you do not, we do not know what member functions are needed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Actually, a struct is what I'm looking for. Do I need to use a special method to use variables in a srtuct or can I just call them normally?

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Like this:
    Code:
    stats myStats;
    myStats.hp = 10;
    myStats.weapon = 3;
    ...

  10. #10
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Ok, thanks!

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    But from what it sounds like, your app could use some classes. Is this some sort of RPG-type, simple game?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Yeah, It's a RPG dungeon adventure based on an RPG I made in VBScript. I'ts going to have easter eggs with demos of other games I've written in it.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Then it might actually be a good time to start learning how to use classes since your project will benefit from it!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by DarkAlex View Post
    Yeah, It's a RPG dungeon adventure based on an RPG I made in VBScript. I'ts going to have easter eggs with demos of other games I've written in it.
    You actually made an RPG game in VBScript?!! Wow, now that's something I'd like to see!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 11:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

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