I started learning C++ no more than 4 days ago so please use the simplest vocabulary that you can manage while still being clear.
I started learning C++ no more than 4 days ago so please use the simplest vocabulary that you can manage while still being clear.
It's a method of representing numbers with words.
Instead of manually maintaining that index 0 of your array represents a panda and index 1 represents a brown bear, you can use an enumeration and call elements in your array with array[panda_bear] = ...
Here is the specific example of what I'm talking about. I am supposed to be making a tic tac toe gmae using enums. Do I use enums for the spaces of the tic tac toe board or the x's, o's, and blank spaces. If you could explain why that would be awesome. On top of that, Im not sure how to use enums. When using them as input, do I type in "red" or "0". Also, what do enums represent? Functions, variables? I'm really confused about this.
What are the values each space is supposed to be able to hold in tac-tac-toe? It's X, O or Blank, right? We want each square to only hold these allowable values, and nothing else. That's where enums come in handy. They allow us to create a new type, and in that type, only specific allowable named constants are allowed to be assigned. Nothing else.
Ex:
To give you something to get started with.Code:enum class MyEnum { Value1, Value2, Value3 }; MyEnum MyVar; // Create a variable of our enum type MyVar = MyEnum::Value1; // OK MyVar = 0; // Not OK: 0 is an int, not of type MyEnum MyVar = '0'; // Not OK: '0' is a char, not of type MyEnum
And to remind you, MyVar can only hold the values Value1, Value2, Value3 from MyEnum and nothing else. Trying to assign anything else will give you a compile error.
Of course, Value1, ..., can be named anything you want. So can the enum itself.
You can also compare to enum values:
Code:if (MyVar == MyEnum::Value2) // Do something if (MyVar == 0.0) // Error: can't compare a double to MyEnum.
This has definitely helped, but I still have a few questions. Just to make sure I am not mistaken, MyVar is only a variable made to represent Value1, then when MyEnum MyVar is entered, it takes Value1 and uses that as input. If thats the case, couldn't you set Value1=MyVar within in the enum? Also, does this mean that enum is technically just a variable with a bunch of constants? Furthermore, aren't constants within the enum set to 0,1,2,... respectively by default? If so, could I just write "MyEnum 0;" instead? What does the "::" in your sample mean?
Here's the example given for enums in the book.
Doesn't this only allow for "RainbowColor chosen_color" to equal "RC_RED"? Wouldn't that mean that I could just set "RC_RED" into "switch(chosen_color)"?Code:enum RainbowColor { RC_RED, RC_ORANGE, RC_YELLOW, RC_GREEN, RC_BLUE, RC_INDIGO, RC_VIOLET }; RainbowColor chosen_color = RC_RED; switch (chosen_color) { case RC_RED: /* paint screen red */ case RC_ORANGE: /* paint screen orange */ case RC_YELLOW: /* paint screen yellow */ case RC_GREEN: /* paint screen green */ case RC_BLUE: /* paint screen blue */ case RC_INDIGO: /* paint screen indigo */ case RC_VIOLET: /* paint screen violet */ default: /* handle unexpected types */ }
Last request:
This is a lot to ask especially since I'm already asking so much, but could you show me an example of how this can be implemented into a tic tac toe program? It still doesn't make too much sense to me in that regard. If it's too much to ask I understand. And thank you very very much in advance, I can't even explain how much of a help you are.
No. MyEnum is a type and MyVar is an variable of that type.
This is no different from using an int. Consider:
int MyVar1; // Myvar1 is a variable of type int
MyEnum Myvar2; // Myvar2 is a variable of type MyEnum
The only difference is that MyVar1 can take any value from min(int) to max(int), while MyVar2 can only takes the values listed in the enum, i.e. MyEnum::Value1, MyEnum::Value2, MyEnum::Value3.
An enum is a type, not a variable, so that makes no sense. An enum does not take up memory and thus cannot hold any data.If thats the case, couldn't you set Value1=MyVar within in the enum?
No, it's a type with a bunch of named constants.Also, does this mean that enum is technically just a variable with a bunch of constants?
They do have a numeric value so that they can be represented by computers. But normally we don't really mind that. That's an implementation detail. We're after the name of the constants instead of using 0, 1, 2, etc directly.Furthermore, aren't constants within the enum set to 0,1,2,... respectively by default?
No. MyEnum is not a variable. It makes no sense to type "Type Value". Only a variable can store a value; a type cannot.If so, could I just write "MyEnum 0;" instead?
It's a namespace, e.g. std::string, where "std::" refers to the std namespace. When creating an (strongly typed) enum, it creates a namespace wherein the named constants lie, as to not pollute the global namespace.What does the "::" in your sample mean?
The book is stupid. I recommend you ignore it.Here's the example given for enums in the book.
Don't know what this means exactly.Doesn't this only allow for "RainbowColor chosen_color" to equal "RC_RED"? Wouldn't that mean that I could just set "RC_RED" into "switch(chosen_color)"?
No. I'm leaving that as an exercise on purpose. Start by getting down a simpler example first, then move on to tic-tac-toe.This is a lot to ask especially since I'm already asking so much, but could you show me an example of how this can be implemented into a tic tac toe program?