代码一:
#include <iostream>
using namespace std;
enum fruit {
apple, oringe, banana, watermelonl
} myfruit;
int main()
{
myfruit = banana;
switch (myfruit)
{
case apple:
cout << "apple is here.\n";
break;
case banana:
cout << "banana is here.\n";
break;
case oringe:
cout << "oringe is here.\n";
break;
}
getchar();
return 0;
}
结果
代码二:
#include <iostream>
using namespace std;
enum fruit {
apple, oringe, banana, watermelonl
};
void GetApple(fruit myfruit)
{
switch (myfruit)
{
case apple:
cout << "apple is here.\n";
break;
case banana:
cout << "banana is here.\n";
break;
case oringe:
cout << "oringe is here.\n";
break;
}
}
int main()
{
fruit myfruit = banana;
GetApple(myfruit);
getchar();
return 0;
}
结果:
代码三:
#include <iostream>
using namespace std;
class fruitclass{
public: enum fruit {
apple, oringe, banana, watermelonl
}myfruit;
};
void GetApple(fruitclass fruit,fruitclass::fruit myfruit)
{
switch (myfruit)
{
case fruit.apple:
cout << "apple is here.\n";
break;
case fruit.banana:
cout << "banana is here.\n";
break;
case fruit.oringe:
cout << "oringe is here.\n";
break;
}
}
int main()
{
fruitclass fruit;
fruit.myfruit = fruit.banana;
GetApple(fruit, fruit.myfruit);
getchar();
return 0;
}
结果与上面一样;
代码四:
#include <iostream>
#include <functional>
using namespace std;
class fruitclass{
public: enum fruit {
apple, oringe, banana, watermelonl
}myfruit;
};
void GetApple(fruitclass fruit,fruitclass::fruit myfruit)
{
switch (myfruit)
{
case fruit.apple:
cout << "apple is here.\n";
break;
case fruit.banana:
cout << "banana is here.\n";
break;
case fruit.oringe:
cout << "oringe is here.\n";
break;
}
}
int main()
{
std::function<void(fruitclass fruit, fruitclass::fruit)> fun;
fun = GetApple;
fruitclass fruit;
fruit.myfruit = fruit.banana;
GetApple(fruit, fruit.myfruit);
getchar();
return 0;
}
结果与上述一样