Thread: Use a class or structure?

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Use a class or structure?

    Im making a text based rpg game. I have several options of races of players to choose from (human, enchanter,brute). Each of these class's have different amount of hitpoints, mana and strength. I will add to these by putting equipment, expierence and levels for the player at a later time. My question is, would it be better for me to use a class or a structure to house this information? Instead of assigning the values outright like this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int HumanBaseHP = 500;
    int HumanBaseMP =200;
    int HumanBaseSTR = 15;
    
    int main()
    {
               //code here
    }
    I think it would be better if I used a class or structure for each race I have. If I am confusing anyone let me know and I will try to explain it better.
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    It doesn't matter. Classes and structures are identical except for their default access level. Classes are private by default and structures are public by default, but you can change that with both of them by using an access modifier:
    Code:
    // Same as a structure
    class C {
    public:
      // stuff
    };
    Code:
    // Same as a class
    struct S {
    private:
      // stuff
    };
    Just pick the one that has the default access that you want.
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Im making a text based rpg game. I have several options of races of players to choose from (human, enchanter,brute). Each of these class's have different amount of hitpoints, mana and strength. I will add to these by putting equipment, expierence and levels for the player at a later time. My question is, would it be better for me to use a class or a structure to house this information? Instead of assigning the values outright like this:
    If you're thinking about players with information, then a class would be more appropriate, though you could use a structure and have the player class contain the structure. If, howver, you're only thinking data, then a struct is more appropriate. But like Narf said, in C++ only a few difference exist in how the compiler treates structs and classes, so what I said above is more a cue to the person reading the source than to the compiler.

  4. #4
    1479
    Join Date
    Aug 2003
    Posts
    253
    Thank you both.
    Knowledge is power and I want it all

    -0RealityFusion0-

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    As I was recently reminded (by Daved, I think), there is one other difference:
    Code:
    struct foo : base // equivalently public base
    {
       int x; // equivalently public: int x;
    };
    
    class bar : base // equivalently private base
    {
       int x; // equivalently private: int x;
    };
    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    I was under the impression that structs and classes were the same except for the fact that strcuts arew by default public and classes are by default private. Seeing as good programming calls for the implementation of both public accessors and private information. Thus which you used would not matter, although classes are usually used.

  7. #7
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    there is one other difference
    Yea, but that falls under the "public by default" or "private by default" deal, all access applies. But then we get into the question of whether omitting the access specifier is a good idea anyway since there's a distinct and not well known semantic difference for inheritance.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Structure vs. Class
    By bobbelPoP in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2008, 09:44 AM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  5. Converting a C structure into a C++ class
    By deadpoet in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2004, 02:06 PM