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
main.cppCode: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; }
i just cant figure out how make enums work with classes.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; }
btw, any help non regarding the enum issue is also WELCOME. thanks



LinkBack URL
About LinkBacks



