ShadowViper
ShadowViper

Reputation: 365

Implementing Enumeration types in C++

So i am trying to create a class that is of enum type except that i have never used enum types before and im in the process of learning. I understand that enumeration type is basically enum a(red, blue green); where red would =0, blue =1 and green =2. But from there i dont quite understand how to further implement this enums class. If i have a header class such as

#ifndef COLOR_H
#define COLOR_H


class Color
{
    public:
        enum Color {red, blue, green};
};

#endif // COLOR_H

and then a cpp file which goes like this:

#include "Color.h"

enum Color::Color {red, blue, green}{
 //dont know how to implement in here
}

How would i implement the member function so that if the Color is passed 1 from main.cpp, then the member function will lets say print out blue?

Upvotes: 1

Views: 3078

Answers (4)

Shoe
Shoe

Reputation: 76240

There's no need for an "enum implementation" (whatever that means to you). You can simply have:

struct Color {
    enum ColorInternal {red, blue, green};
};

and access the enum values as Color::red, Color::blue, Color::green and the enum as Color::ColorInternal.

Alternatively you can simply use an enum class:

enum class Color {red, blue, green};

and access the values as Color::red and the class as Color.

Upvotes: 2

Christophe
Christophe

Reputation: 73366

Aternative 1: simple enum type

You could define in your header a simple enum type without a class:

enum ColorType { red, blue, green };  // Simple enum

You could overload common stream operations for example for printing the value, tailor made:

ostream& operator<< (ostream& os, ColorType color) {
    switch (color) {
    case red:
        std::cout << "red" << endl;  break;
    case blue:
        std::cout << "blue" << endl;  break;
    case green:
        std::cout << "green" << endl;  break;
    }
    return os;
}

In main(), you could then use it easily:

    ColorType val = red; 
    if (val==blue) 
        cout << "blue";
    cout << val; 

I'd recommend this in most cases.

Alternative 2: Complete color class

However, you cannot enrich an enum of alternative one with a different semantic, for example to allow for adding colors (or even washing away color intensity at each copy) or other member functions.

For this enriched approach you'd need a class. Here you'd need to change slightly your definition. First, a member cannot have the same name as the class itself (otherwhise type definitions and constructors would be extreamly ambiguous !):

class Color
{
public:
    enum MyColor { red, blue, green };  // That's just a nested type
    Color(MyColor c = red);             // That's a constructor for the class, with a default value
    void print();                       
private: 
    MyColor color;                      // That's a value
};

You could then start to use this class:

Color cr = Color::MyColor::red;  // access to nested color type needs some extra typing
Color choice; 
choice = cr; 
choice.print();         // of course you'd need to define print() first

Then it's up to you to enrich this class.

Upvotes: 0

Daniel Samson
Daniel Samson

Reputation: 718

Here's an other way way:

#include "stdafx.h"

using namespace System;

enum ECOLOR { RED, BLUE, GREEN };

class Color
{
public:
    Color();
    ~Color();
    Color(ECOLOR);
};

Color::Color(ECOLOR color)
{
    switch (color)
    {
    case RED:
        Console::WriteLine(L"Hello Red\n");
        break;
    case BLUE:
        Console::WriteLine(L"Hello Blue\n");
        break;
    case GREEN:
        Console::WriteLine(L"Hello Green\n");
        break;
    default:
        break;
    }
}

int main(array<System::String ^> ^args)
{
    Color *blue = new Color(BLUE);

    return 0;
}

Upvotes: 0

user2008934
user2008934

Reputation: 319

There is no need for implementation in a .cpp file - What is in the header is enough. Incidentally, you don't actually need a class to hold an enum. simply writing enum Color {Red, Blue, Green}; is enough.

You can think of it as a cleaner way to write:

#define Red 0
#define Blue 1
#define Green 2
#define Color int

There are several fundamental differences but the point is that this too can be written in a .h file. Of course, you should never use the above example, it is horrible programming practice.

Upvotes: 0

Related Questions