enum command

hello, i've only just starting looking at the enum command, and whilst i can see how it could be really useful, in every example i've seen it's been used in the main() section of the program along side all the class defintions,member functions, everything really. I have now started splitting my code in to header files ,implementaion files,etc.. and not sure how i use it now . Is it be avoided? .For instance if i define a type weather and want to use it in the class , how do i go about it??. where does the enum decleration go in the code? In the header,main,???. Doesn't it make the class a bit too unusable for others??

enum weather { Sunny,Snowy,Raining };


class Days
{
private :
weather today= Sunny;
};

appolgies if my example is poor, but i think you'll get the point.


You can define it, like you did, in the global scope, meaning it can be used by any other part of your program. Or you could make local to the class by doing something among these lines (untested):
1
2
3
4
5
class Days
{
private:
    enum {Sunny, Snowy, Raining} today;
}
Also, defining today to be Sunny in the class body is not legit, do it inside a constructor.
okay, i see, thanks.

But is it common practise to define them in main() and then taylor the classes in the header files to suit these user defined types?
OOP is about data-hiding.
What does this mean? This means that you only ask what you need. If you don't need weather to be used anywhere else, then it's good practice to define them inside the class body. If you want to use them in the class, just as somewhere else, do it the way you described in your first post.

EDIT:
Note that the enumeration I showed in my example is anonymous, a better way would be:
enum weather {Sunny, Snowy, Raining} today;
Last edited on
okay, thanks for your help.
You're welcome.
Topic archived. No new replies allowed.