I'm still struggling with the enum command and it's scope(i believe it's called..).
In which file(of the three below) do i declare the following enum command ?
enum weather { rainy,cloudy,snowy };
for my example below. Is it in main()?, if so, will the header file then recognise the weather enum type?.
I assume you only declare it once somewhere ?.As i've already posted..,i've only seen enum work when everything is in one file.
Again, apologies for any syntax or other errors. I hope you can see what i'm trying to say , even if i've not explained it too well.
stormType.h // header for class
class stormType
{
public:
stormType( weather w1); // contruc with 1 parameter of enum type weather
protected:
weather var1;
}
stormType.cpp //implementation file for method definition
Since it's used in stomrType.h, it would have to be either in stormType.h or in some header that is included by stormType.h. (or in some header that is always #included before stormType.h is included, but I would avoid doing that as it makes you have to remember the right order to include things, which is a maintenance nightmare).
putting it in main.cpp or in main itself would be no good.
I've put the enum commands in the header file but obvously not in the class. Seems to work fine and permeate through the files because the header is called out and used through out code.
You can put enums anywhere. The only thing that changes is their scope. You can even have them local to functions... or even to blocks inside of functions.
The same is true for any type in C++. enums aren't special or anything.
It seems like this is just a question of how scope rules apply.
Having listening to what you've all said, I think I'll try something else
Instead. Basically I'm trying to write a program which will involve list of days , weeks,etc.. , and I thought this would be best achieved using enum as the alternative would be using strings to store "Monday","Tuesday",etc instead of integers. I believe I could achieve a similar results by using
#define MONDAY 2
and so on? In an include file?. What do you think?