开发者

How to use enumeration types in C++?

开发者 https://www.devze.com 2022-12-25 22:33 出处:网络
I do not understand how to use enumeration types. I understand what they are, but I don\'t quite get their purpose.

I do not understand how to use enumeration types. I understand what they are, but I don't quite get their purpose.

I have made a program that inputs three sides of a triangle and outputs whether or not they are isosceles, scalene, or equilateral. I'm suppose to incorporate the enumeration type somewhere, but don't get where and how to use them. Any help would be appreciated.

#include <iostream>

using namespace std;

enum triangleType {scalene, isosceles, equilateral, noTriangle};

triangleType triangleShape(double x, double y, double z);
void printTriangleShape(triangleType shape);

int main()
{
    double x, y, z;
    triangleType scalene, isosceles, equilateral, noTriangle;

    cout << "Please enter the three sides of a triangle:" << endl;
    cout << "Enter side 1: ";
    cin >> x;
    cout << endl;
    cout << "Ente开发者_如何学编程r side 2: ";
    cin >> y;
    cout << endl;
    cout << "Enter side 3: ";
    cin >> z;
    cout << endl;

    triangleType t = triangleShape(x, y, z); 
printTriangleShape(t);

    return 0;
}

triangleType triangleShape(double x, double y, double z)
{
   triangleType scalene, isoceles, equilateral, noTriangle;
    if (((x+y) > z) && ((x+z) > y) && ((y+z) > x))
    {
        cout << "You have a triangle!" << endl;
            if (x == y && y == z)
               return equilateral;
            else if (x == y || x == z || y == z)
                 return isosceles;
            else
               return scalene;
    }
    else if ((x+y) <= z || ((x+z) <= y) || ((y+z) <= x))
        return noTriangle;  
} 
void printTriangleShape(triangleType shape)
{
    switch (shape)
    {
    case scalene: cout << "Your triangle is Scalene!" << endl;
        break;
    case isosceles: cout << "Your triangle is an isosceles!" << endl;
        break;
    case equilateral: cout << "Your triangle is an equilateral!" << endl;
        break;

    }
}


It's a value, and you probably want to return it from your function.

Try:

triangleType triangleShape(double x, double y, double z) {
  if (...) {
    return scalene;
  } else if (...) {
    return isosceles.
  } else if (...) {
    return equilateral
  } else {
    return noTriangle;
  } 
}

Note, you can print the result, but it will print as an integer: scalene = 0, isosceles = 1, ...

Edit, for printing you may want to do this:

void printTriangleShape(triangleType shape) {
   switch (shape) {
     case scalene:
       cout << "Your triangle is Scalene!" << endl;
       break;
     case isosceles:
       cout << "Your triangle is isosceles!" << endl;
       break;
     ...;
   }
}


In C enums make debugging easier because often debuggers print the name value rather than a numeric value. They also allow the compiler to enforce places where it can determine that an invalid value is being stored into an enum variable.

In C++ there is also another benifit, which is that you can use enum types in overloads. For instance, you could:

ostream & operator<<(ostream & ostr, triangleType t) {
     string s;
     switch (t) {
          case scalene:
             s = "scalene";
             break;
          case isosceles:
             s = "isosclese";
             break;
          case equilateral:
             s = "equilateral";
             break;
          case noTriangle:
             s = "noTriangle";
             break;
          default:
             s = "error bad triangle type";
             break;
       }
       return cout << s;
}

and then in main do

cout << "Your triangle is" << t << endl;


An enumeration can be used to identify 'types' of objects, as you are in your case.

For example, your triangle shape method could return a triangleType and that way you could do all of the cout << "..." in your main method and separate the display logic from the triangle object.


The idea is to replace using numbers (1,2,3...) that don't explain their meaning with tags that do have meaning (red, green, blue...). Numbers used in code that only you understand the meaning of are called "magic numbers" and should be avoided since it keeps others from understanding your code.


An enum is a new type in c++. Using this type creates additional type safety as you are only allowed to use the values that are defined for that enum. Enum values will get numbered automatically unless you specify a value yourself, which should be rarely needed. An example:

enum Color { Red, Green, Blue }; // Red = 0, Green = 1, Blue = 2
enum Shape { Circle, Square };   // Circle = 0, Square = 1

int printColor(Color c)
{
    // do something with the color here, for example print it.
    switch(c)
    {
        case Red:
            cout << "Red";
            break;
        case Green:
            cout << "Green";
            break;
        case Blue:
            cout << "Blue";
            break;
    }
}

int main(int argc, char* argv[])
{
    printColor(Red); // works
    printColor(0);   // will give an error or warning in C++.
                     // However, C does less type checking and allows this.

    printColor(Circle);
                     // error, the type is wrong even if the values are identical.
}

You get added type safety in the printColor(0) call -- c++ does additional type checking here, so you can't mistakenly put an invalid number in the call. You can of course achieve the same result with using #define for the values or even put them directly, but in that case the compiler won't be able to warn you if you put in invalid values.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号