Thread: Using enumerations in classes

  1. #1
    The larch
    Join Date
    May 2006
    Posts
    3,573

    Using enumerations in classes

    In another thread it was suggested to use an enum for gender in Human class.

    Is this how you would implement the idea:
    Code:
    #include <iostream>
    
    class Human
    {
        public:
            enum gender {male, female}; //public, so they would be accessible in main()
            Human(gender g): m_gender(g) {}
            ~Human() {};
            gender getGender() //or should it return int?
            {
                return m_gender;
            }
        private:
            gender m_gender; actual gender, therefore private
    };
    
    int main()
    {
        Human man(Human::male); //is this right?
        Human woman(Human::female);
        std::cout << man.getGender() << " " << woman.getGender() <<
        std::endl;
        std::cin.get();
    }

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Does it work? It looks ok to me.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    It shows 0 and 1
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Sure, that looks good. ...but depending on how you further implement, if gender is just going to be an enum, then there really would be no difference between Human::male and Dog::male, so why not just make it global? I generally only use class enums for protected and private situations. In this case, it just seems unnessasary to me to have it in class scope, but I could be wrong about this logic.
    Sent from my iPadŽ

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, you have the scoping of the class. Also, a global gender is not necessarily the same: if you apply the gender to words, you'd have to introduce a third value "neuter", which does not make sense for humans.

    Yes, it outputs the numeric value of the enum value. You usually solve this by using a lookup table on the values.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Some people are biologically born with parts of both genders and do not want to be called "male" or "female", one way or the other... so you may want to add "neutral" or "N/A" to the enum to be politically correct. And if this is for user input, some people might not want to disclose their genders for some reason.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by CornedBee
    Well, you have the scoping of the class. Also, a global gender is not necessarily the same: if you apply the gender to words, you'd have to introduce a third value "neuter", which does not make sense for humans.
    Have you ever heard of a vasectomy? That was my point. As long as the enum accounts for all global possibilities then making it public to the class rather than global seems pointless to me. However, if thought about more, the only condition humans really can't fill in gender is Asexual. So global may be impossible here, but again, it really depends on where his program is going.
    Quote Originally Posted by jafet
    Some people are biologically born with parts of both genders and do not want to be called "male" or "female", one way or the other... so you may want to add "neutral" or "N/A" to the enum to be politically correct.
    Bah... I've never played a politically correct RPG in my life then because all (that I know of) computer or console RPGs only offer Male and Female as gender choices. Now, do you think Oblvion would have used a global gender enum or enums local to each of their races even though they all were male and female? However, if you are talking about some form of Database or something that does take input from users about themselves, then I'd say N/A is a good option.
    Last edited by SlyMaelstrom; 10-09-2006 at 05:08 AM.
    Sent from my iPadŽ

  8. #8
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    #include <iostream>
    
    class Human
    {
        public:
            enum gender {male, female}; //public, so they would be accessible in mai
    n()
            Human(gender g): m_gender(g) {}
            ~Human() {};
            gender getGender() //or should it return int?
            {
                return m_gender;
            }
        private:
            gender m_gender; // actual gender, therefore private
    };
    
    std::ostream& operator<<(std::ostream& os, const Human::gender gender)
    {
        switch( gender )
        {
            case Human::male:
                os << "Male";
                break;
            case Human::female:
                os << "Female";
                break;
            default:
                os << "Unknown";
                break;
        };
    
        return(os);
    }
    
    int main()
    {
        Human man(Human::male); //is this right?
        Human woman(Human::female);
        std::cout << man.getGender() << " " << woman.getGender() <<
        std::endl;
        std::cin.get();
    }

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by SlyMaelstrom
    Now, do you think Oblvion would have used a global gender enum or enums local to each of their races even though they all were male and female?
    I very much doubt that big RPG games even have any classes beyond Character, and perhaps a distinction between PCs and NPCs. They tend to be completely driven by data loaded at runtime from data files.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by CornedBee
    I very much doubt that big RPG games even have any classes beyond Character, and perhaps a distinction between PCs and NPCs. They tend to be completely driven by data loaded at runtime from data files.
    Oh, come on... it was an example, and I said would have, not did. Considering that each race has almost exactly the same functionality in oblivion, I agree with you, however the point was made in that post and you're really just pulling my example out of context there.
    Sent from my iPadŽ

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    OK, let's assume there's a hierarchy there, then. I'd still put the enum into the base-most class where it makes sense.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    Look at what I have started...
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You actually have started something interesting. Two points of view that are both right. It's a matter of preference mostly (although some design choices may make one a bad choice while others may make it a better a choice).

    It's in defending or seeing others defeding their points of view that we learn most.

    Personally I'm more inclined towards Sly's. An enum definition simply defines a type. The "OMG-Don't-make-globals!" doesn't apply here. If we have several base classes that have somewhere inside a gender enum we are complicating things if later on we need to add a new gender ("neuter", "asexual", etc...). Having one central global enum definition makes things easier to maintain.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I'm not about avoiding globals, I'm about associating concepts with concepts they depend upon.

    In the case of gender, you are right, it's for a large part preference, but I still have a good reason to prefer my way.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by CornedBee
    I'm not about avoiding globals, I'm about associating concepts with concepts they depend upon.

    In the case of gender, you are right, it's for a large part preference, but I still have a good reason to prefer my way.
    in this case a happy middle ground might be to define gender inside a namespace where all the different character classes reside (especially if you don't want a big inheritence hierarchy).
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  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, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM