//This is not very dynamic. How do I get different values into myCar? i.e. CT_MUSTANG
Code:
#include <iostream>
int main()
{
enum CarType {CT_FORD, CT_MUSTANG, CT_SKODA}; //not sure about using class yet
CarType myCar = CT_FORD; //This is not very dynamic. How do I get different values into myCar? i.e. CT_MUSTANG
if (myCar == CT_FORD) //how do you get to the point where myCar actually equals CT_FORD (or 0)?
std::cout << "You got a Ford"; //of course, because 'CarType myCar = CT_FORD' is hard-coded in!
if (myCar == CT_MUSTANG)
std::cout << "You got a Mustang";
myCar=CT_SKODA;
if (myCar == CT_SKODA)
std::cout << "You got a Skoda";
}
the main advantage of enums comes when you have multiple diferent actions that may or may not be used. traditionaly (meaning: when done by stuborn programmers like myself who use ints for everything even when we shouldnt) you would need to remember the correspondence between the value of your int and the option to use, so i could do something like this:
Code:
int main()
{
int coordenatesystem; //controls the coordenate system used: 0 means Cartesian; 1 logaritmic; 2 polar; 3 cylindrican; 3 spheric
if (coordenatesystem==1)
/*do something*/;
if (coordenatesystem==3)
/*do something else*/;
//etc
return 0;
}
notice that ill need to keep remembering what 0,1,2,3&4 mean, its not particulary reader friendly, I could easily switch a 3 for a 4. one thing to keep in mind is that in a large project things get edited halfway through so readbility becomes more and more important the larger the work...and another thing is the fact that nothing stops me (compiler-wise) from assigning 6 to the int.
on the other hand using enums looks mutch clearer:
Code:
int main()
{
enum coordenatesystem{cartesian,polar,spheric};
coordenatesystem coords;
if (coords==cartesian)
/*do something*/;
if (coords==spheric)
/*do something else*/;
//etc
return 0;
}