Making a human class...not sure what to do

This is a discussion on Making a human class...not sure what to do within the C++ Programming forums, part of the General Programming Boards category; I wanna make a class called Human. Then I would like to have a little menu in main that says: ...

  1. #1
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60

    Making a human class...not sure what to do

    I wanna make a class called Human. Then I would like to have a little menu in main that says:

    1. Gender
    2. Age
    3. Hieght

    And things like that. What I need help with is the making of the class, I can make a menu to access whats in the class easily. The only class I have made is the one in the tutorial on this site. Ive only modified it a little. Can someone explain to me what it would look like?

    Code:
    class Human{
         public:
              Human();
              ~Human();
    
               // Im guessing you would put something like void gender(); here
               // Along with int feMale(); meaing female or male...I dont know. Something like
               // that is my guess.
         protected:
              // What would go here?
         private:
              // Would there be anything here?
    };
    
    // Lol, no clue what would go here either...im such a noob.
    Thats just what I know about classes...well their basic structure anyways. As you can tell, I have no clue where things would go.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    class Human {
      public:
        // Constructors, destructors, methods and public variables
        // Those can be accessed by anyone
      protected:
        // protected is only needed if you inherit from class Human, it says
        // that you can only access these members/functions if you are a child
        // of this class. You most probably won't need this section.
      private:
        // nobody can access this except class Human
        // might want to have your gender defined as follow
        enum Gender { MALE, FEMALE } gender;
    };

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    So you design some sort of human interface.

    Code:
    class Human{
         public:
              Human();
              ~Human();
    
               // Public interface, 'getters and setters'
    
               int getAge();
               char getGender();
               int getHeight();
    
               void setGender(char g);
               void setAge(int a);
               void setHeight(int h);
    
               
         protected:
              // Are human's to be derived from (specialized)? 
              // Transhumans? Probably no need for protected.
    
         private:
              int height, age;
              char gender; // m or f
              
    };
    You implement those methods now. Outside of that, you say stuff like return-type class-name::method-name(param-list) { }. For example, the setGender method might be implemented like;

    Code:
    void Human::setGender(char g)
    {
            gender = g;
    }

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  4. #4
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    Thanks for posting, but you didnt really help me heh. I know the basics of WHAT is supposed to go there. I guess I should have asked my question differently. Hmm...

    Can anyone type some sample code that shows what might go in pblic,protected and private. If someone could do that I could probably work off the example and figure out the rest on my own.

    Also, Desolation...whats enum?



    EDIT: Lol, Tonto posted right before I made this. Thanks for posting that and you can ignore this post now. Heh
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  5. #5
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    Well I have got it where it shows the age and height. But I cant figure out how to set gender to either male or female and have it appear where I want it.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Code:
    void Human::setGender(char g) 
    {
        gender = g; // where gender is a private member
    }
    
    char Human::getGender() 
    {
        return gender; // where gender is, again, a private member
    }
    Simple, and cut down, main -

    Code:
    int main()
    {
    	Human person;
    
    	// Get the person's info with cin and whatnot
    
    	person.setGender( Gender ); // You have cin'd a character variable called Gender
    
    	std::cout<< person.getGender(); // Returns the gender
    }
    Last edited by twomers; 10-07-2006 at 12:58 PM.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Why have a gender as functions setGender() and getGender()? Can't you just have a public gender variable? Also gender is a good thing to be hold in a BOOL data type.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Quote Originally Posted by maxorator
    Why have a gender as functions setGender() and getGender()? Can't you just have a public gender variable?
    Sure you can. But the moment you want to store the gender in some different way, or log a message whenever the gender is changed (because laws against cross-gender operations have been passed, for example) or want to enforce some restrictions, you're pretty much screwed with a variable. Getters/setters are future-oriented programming, and thanks to inlining, they're usually free in terms of runtime performance.

    Also gender is a good thing to be hold in a BOOL data type.
    No, it's not. First, nothing is a good thing to hold in a BOOL data type, because that's a Win32 "C has no bool" type. If anything, use a proper C++ bool.
    It's also conceptually wrong. is_male could be boolean, or is_female. gender cannot be boolean - what does true mean? Make's me think of Austin Powers.
    Form: Sex?
    Austin: Yes, please!
    An enum is by far the best choice for gender. It's an enumeration of a finite and small set of possible values, namely "male" and "female".
    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

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Yes, there's no compromise possible when holding it in the bool data type so you must decide which of them (true and false) is male and which is female.

    This is a C++ forum and C++ has bool so there's nothing wrong with it.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by CornedBee
    It's also conceptually wrong. is_male could be boolean, or is_female. gender cannot be boolean - what does true mean? Make's me think of Austin Powers.


    An enum is by far the best choice for gender. It's an enumeration of a finite and small set of possible values, namely "male" and "female".
    A bool would be plain stupid in that case. enum is the best way to go.

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    What is wrong with bool? You don't like that it uses only 1 single bit?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  12. #12
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    He could use an INT? Use 0 and 1.
    Last edited by Queatrix; 10-07-2006 at 02:29 PM.

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    The same thing with bool. True and false ARE 0 and 1, aren't they?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #14
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Because what looks right... human->gender(true); or human->gender(female); ?

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    #define MALE false
    #define FEMALE true
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Page 1 of 3 123 LastLast
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. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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