How Access the the enum fields...

I make a program in of inheritance in which I declared an ENUM Type enum BREED {GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB}; I am inheriting some functions from the base class for my Dog Class, so when I made the the object of the class and wanted to access the BREED function
Dog Fido;
Fido.SetBreed(CAIRN);
cout<<"Fido,s Breed is " << Fido.GetBreed()<< endl;

so it shows me the output that "Fido,s Breed is 1" which is although right because in ENUM it counts the values from 0 and so on, so CAIRN is on position 1 so its quite right.
Now my Question is that how to print the names in the Enum type on the screen. I mean it doesn,t show me numbers like 0,1,2,3 it directly shows me GOLDEN, CAIRN or whatever value i pass to its function accordingly.

Below is the complete Source Code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  #include <iostream>

using namespace std;

enum BREED {GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB};

class Mammal{
public:
      Mammal():itsAge(2),itsWeight(5){}  //Constructor//
      ~Mammal(){} //Destructor//

      int GetAge() const {return itsAge;} //Accessors//
      void SetAge(int age){itsAge=age;}
      int GetWeight() const {return itsWeight;}
      void SetWeight(int Weight){itsWeight=Weight;} //Accessors//

      void Speak() const {cout<<"Mammal Sound\n";} //Other functions//
      void Sleep() const {cout<<"Shhh! I am Sleeping\n";} //Other Functions//

protected: //protected Variables can be accessed in the derived classess , but private otherwise//
    int itsAge;
    int itsWeight;
} ;

class Dog:public Mammal
{
public:
    Dog():itsBreed(GOLDEN){}  //Constructor//
      ~Dog(){} //Destructor//

      BREED GetBreed() const {return itsBreed;} //Accessors//
      void SetBreed(BREED breed){itsBreed=breed;}

      void WagTail() const {cout<<"Wagging Tail\n";} //Other functions//
      void BegForFood() const {cout<<"Begging For Food\n";} //Other Functions//

private:
    BREED itsBreed;
};

int main()
{
    Dog Fido;
    Fido.SetBreed(CAIRN);
    cout<<"Fido,s Breed is " << Fido.GetBreed()<< endl;
    Fido.Speak();
    Fido.Sleep();
    cout << "Fido is "<< Fido.GetAge() << " Years Old!!!"<< endl;
    cout << "Fido is ";
    Fido.BegForFood();
    cout << "Fido is ";
    Fido.WagTail();
    return 0;
}
Last edited on
You'll have to manually make a conversion method using a map or just a switch/case or something similar. There is no built-in way to retrieve the names of the enumeration's values.
Simply, you can't.

A straight forward way is to have an array of c-strings ( better a std::vector of std::strings):

1
2
3
4
5
6
7
8
9
10
enum Breed { LAB, COLIE };

const char* breed_names[] = {
  "Labrador", "Colie" };
size_t breed_names_length = sizeof( breed_names ) / sizeof(breed_names[0];

class dog
{
  const char* get_name(void) const { return breed_names[ itsBreed ]; }
};
Last edited on
Topic archived. No new replies allowed.