Tutorial :Enumerated data types and class APIs in C++



Question:

If you're supposed to encapsulate everything inside a class definition, how is it then possible to use enumerated data types with the class? For example I've just written the following code...

enum PizzaType {DEEP_DISH, HAND_TOSSED, PAN};  enum PizzaSize {SMALL, MEDIUM, LARGE};    class Pizza {      public:          Pizza();          void setPizzaType(PizzaType type);          PizzaType getPizzaType();          void setPizzaSize(PizzaSize size);          PizzaSize getPizzaSize();          void setToppings(int toppings);          int getToppings();          void outputDescription();          double computePrice();      private:          PizzaType pizzaType;          PizzaSize pizzaSize;          int totalToppings;  };  

Is there any way of including the enumerated data types inside the class itself and yet still allow access to the mutator/accessor functions from outside?


Solution:1

Yes, you can do it like this:

class Pizza {      public:          enum PizzaType {DEEP_DISH, HAND_TOSSED, PAN};          enum PizzaSize {SMALL, MEDIUM, LARGE};            Pizza();          void setPizzaType(PizzaType type);          PizzaType getPizzaType();          void setPizzaSize(PizzaSize size);          PizzaSize getPizzaSize();          void setToppings(int toppings);          int getToppings();          void outputDescription();          double computePrice();      private:          PizzaType pizzaType;          PizzaSize pizzaSize;          int totalToppings;  };  

.. and then others have said, you just have to use the Pizza "namespace" in order to get to the enum types:

Pizza::PizzaType tmp = pPizza->getPizzaType();  

etc, etc.

(As a note of style, when you put the enums inside of the class like this, I would personally remove the Pizza in front of it so that you have Pizza::Type and Pizza::Size.)


Solution:2

The preferred method of using enums in C++ is to define them inside a class:

class Foo {  public:       enum Bar {           ENUM_VALUE1,           ENUM_VALUE2       };  };  

Then, you can reference them using:

Foo::Bar var;  var = Foo::ENUM_VALUE1;  

Inside the class, you can drop the Foo:: prefix.

As you have probably noticed, while the type of the enum is Foo::Bar, the values are not referenced through Foo::Bar::ENUM_VALUE1, but rather appear in the Foo's namespace. This may be problematic if different enums share the same value names. To avoid that, you can do the following trick:

class Foo {  public:        struct Bar {               enum ENUM {                    ENUM_VALUE1,                    ENUM_VALUE2               };        struct Baz {               enum ENUM {                    ENUM_VALUE1,                    ENUM_VALUE2               };        };  };  Foo::Bar::ENUM e = Foo::Bar::ENUM_VALUE1;  Foo::Baz::ENUM e2 = Foo::Baz::ENUM_VALUE1;  


Solution:3

class Pizza {      public:          enum PizzaType {DEEP_DISH, HAND_TOSSED, PAN};          void setPizzaType(PizzaType type);          PizzaType getPizzaType();  };  ...  Pizza p;  p.setPizzaType( Pizza::DEEP_DISH );  Pizza::PizzaType pt = p.getPizzaType();  


Solution:4

As long as the enumeration type is public, you are allowed to use it outside the class. You just have to scope it i: Pizza::PizzaType. The same rule exists for static methods and for static constants and variables.


Solution:5

You can define the enum inside the class and use it outside like how you use a namespace i.e. classname::enum_type. Actually it is a good idea to define it inside the class as it avoids the global namespace pollution.


Solution:6

That is not a problem, you can create variables of this enum type by specifying Pizza::PizzaType.

Put the enums inside your class, if you want them to be accessed by the outside make sure they are public too.

Pizza p;  p.setPizzaType(Pizza::PAN);  Pizza::PizzaType pt = p.getPizzaType();  assert(pt == Pizza::PAN);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »