class Bunny {
private:
enum Sex {Male = 0, Female};
enum Color{White = 0, Brown, Black, Spotted};
int age;
bool radioactive;
std::string name;
public:
Bunny();
Sex getSex();
void setSex(Sex);
Color getColor();
void setColor(Color);
int getAge();
void setAge(int);
std::string getName();
void setName(std::string);
bool getRadioactive();
};
Bunny.cpp
1 2 3 4 5 6 7 8 9 10
Bunny::Bunny(){
srand(time(NULL));
age = rand() % 11;
Sex sex = Sex(rand() & 2);
Color color = Color(rand() % 4);
}
Bunny::Sex Bunny::getSex(){
return sex;
}
And the problem is
1 2 3
Bunny::Sex Bunny::getSex(){
return Sex;
}
It's telling me type name is now allowed for Sex in my return statement.
The syntax for defining an enum is very similar to the syntax for defining a class. You're defining the types but you don't have any objects in your class of that type.
Mmm... that's about right, though I never used the usual constructor notation for enums like that. I'd always use a ternary operator (or if statements) and two or more possible values of the enum object. I find it helps a lot with readability.
@LB
Sex is a physical property. Gender is mental property.
That's because the enums are basically just a way to do away with so-called magic numbers (numbers that are given an unnatural meaning in a program) and #defined constants. The compiler strips away the fancy names at compile-time.
You might want to consider a switch statement for printing out a string associated with each value.
Ah hmm, well thats annoying. I guess I dont need to do that now, I just wrote a quick function to test everything. Values are right, I just dont get words. Thanks though!