Enum issue

This is a discussion on Enum issue within the C++ Programming forums, part of the General Programming Boards category; I read a few tutorials on enum types, thought it'd be easy to use them until i had this idea ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    Enum issue

    I read a few tutorials on enum types, thought it'd be easy to use them until i had this idea of practicing it with classes, here's what i came up with:

    person.h
    Code:
    using namespace std;
    
    class Person
    {
        public:
            enum Sex {FEMALE, MALE};
            // constructors
            Person();
            Person(string, string, enum Sex, int);
    
            // getters
            string GetFullName() const { return string(FirstName+" "+LastName); }
            string GetFirstName() const { return string(FirstName); }
            int GetAge() const { return (2010 - YearBorn); }
    
            string GetSex(string, string) const;
    
            // setters
            void SetFirstName(string str) {this->FirstName = str;}
            void SetLastName(string str) {this->LastName = str;}
            //void SetSex  stuck
    
        private:
    
            string FirstName;
            string LastName;
            Sex Sex;
            int YearBorn;
    
    };
    
    Person::Person()
    {
        cout<<"PARAMETERLESS CONSTRUCTOR SHOULD NOT BE CALLED."<<endl;
    }
    
    Person::Person(string fName, string lName, enum Sex sex, int yBorn)
        : FirstName(fName), LastName(lName), Sex(sex), YearBorn(yBorn)
        {
    
        }
    
    string Person::GetSex(string strMale="man", string strFemale="woman") const
    {
        string returnStr;
        returnStr = (this->Sex) ? strMale : strFemale;
        return returnStr;
    }
    main.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "person.h";
    
    using namespace std;
    
    int main()
    {
        Person p1("John","Willians",MALE,1989);
        cout<<p1.GetFullName()<<" is a "<<p1.GetSex("guy","chick");
    
        return 0;
    }
    i just cant figure out how make enums work with classes.

    btw, any help non regarding the enum issue is also WELCOME. thanks

  2. #2
    Registered User blackcoder41's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    14
    Code:
    Person p1("John","Willians",Person::MALE,1989);
    Since your enum is inside the Person class then it's under the scope of that class.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,766
    SourceForge.net: Do not remove parameter names - cpwiki
    Aside from that, some other heads up:

    >> string GetFullName() const { return string(FirstName+" "+LastName); }
    >> string GetFirstName() const { return string(FirstName); }
    Casting to a string is not necessary here.

    >> void SetFirstName(string str) {this->FirstName = str;}
    >> void SetLastName(string str) {this->LastName = str;}
    The "this->" is not necessary here.

    Furthermore, it is bad practice to use using namespace in headers, because that would force the using namespace on anyone using the header. Suggest you avoid that and use explicit qualification, eg std::string instead. Using namespace is fine in source files.
    Also, why do you have a parameterless constructor if you don't want anyone to call 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.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    Person::Person()
    {
        cout<<"PARAMETERLESS CONSTRUCTOR SHOULD NOT BE CALLED."<<endl;
    }
    As to parameterless constructor, just remove it from the class, and the compiler will make sure that it won't be called.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    You can declare the enum outside the class if you don't want to have to write Person::FEMALE or Person::MALE

    Code:
    enum Sex {FEMALE, MALE};
    Your enumeration is wrong. Gender is a spectrum. Use float instead.
    Last edited by QuadraticFighte; 10-23-2010 at 03:19 PM.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,766
    I can't tell if that's a joke or not, because that makes no sense...
    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.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by Elysia View Post
    I can't tell if that's a joke or not, because that makes no sense...
    I have a strange sense of humor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. enum [tag] [: type] Is this non-standard?
    By HyperShadow in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2007, 09:29 PM
  3. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 12:16 PM
  4. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 03:51 AM
  5. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM

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