I'm new to learning C++. I'm trying to convert a Java application that I wrote to C++. What I can't understand is how to recreate the Enumeration classes from Java to C++. I would like to be able to create Enumeration objects which have a constructor and a toString()
method.
Below is an example of my Java Enumeration:
public enum InteractionType {
// Enumeration Values
PRINTACTIONS("Print Available Actions"),
PRINTBANKNAME("Display Bank Name"),
PRINTBANKACTIONS("Display Bank Actions"),
CREATEBRANCH("Create a Branch"),
CREATECUSTOMER("Create a Customer"),
READBRANCH("Query Bank Branches"),
READCUSTOMER("Query Customers and/or Customer Transactions"),
UPDATEBRANCH("Update Branch Name"),
UPDATECUSTOMER("Update Customer Information"),
DELETEBRANCH("Delete Branch"),
DELETECUSTOMER("Delete Customer"),
QUITAPP("Quit Application");
// Private Variables
private final String description;
// Constructor
InteractionType(String description) {
this.description = description;
}
// Methods
public String toString() {
return description;
}
}
I would then create a method in the main file to get a specific InteractionType
from an array of values. An example of that method would be:
private static InteractionType getSelectedAction() {
// Creates an array with all the values of the Enumeration Class
InteractionType[] interactionTypes = InteractionType.values();
System.out.print("Select an action: ");
int id = scanner.nextInt();
scanner.nextLine();
// Returns a specific InteractionType
return interactionTypes[id];
}
This is the class I have attempted to write to recreate a Java Enumeration in C++:
InteractionType.h
#include <iostream>
#include <string>
using namespace std;
class InteractionType {
public:
static const InteractionType PRINTACTIONS(string = "Print Actions");
static const InteractionType PRINTBANKNAME(string = "Print Bank Name");
static const InteractionType PRINTBANKACTIONS(string = "Print Bank Actions");
static const InteractionType CREATEBRANCH(string = "Create a Branch");
static const InteractionType CREATECUSTOMER(string = "Create a Customer");
static const InteractionType READBRANCH(string = "Read Branches");
static const InteractionType READCUSTOMER(string = "Read Customers");
static const InteractionType UPDATEBRANCH(string = "Update a Branch");
static const InteractionType UPDATECUSTOMER(string = "Update a Customer");
static const InteractionType DELETEBRANCH(string = "Delete a Branch");
static const InteractionType DELETECUSTOMER(string = "Delete a Customer");
static const InteractionType QUITAPP(string = "Quit Application");
private:
string description;
InteractionType(string description);
};
InteractionType.cpp
#include "InteractionType.h"
const InteractionType InteractionType::PRINTACTIONS(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::PRINTBANKNAME(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::PRINTBANKACTIONS(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::CREATEBRANCH(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::CREATECUSTOMER(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::READBRANCH(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::READCUSTOMER(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::UPDATEBRANCH(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::UPDATECUSTOMER(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::DELETEBRANCH(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::DELETECUSTOMER(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::QUITAPP(string description)
{
return InteractionType(description);
}
InteractionType::InteractionType(string description) {
this->description = description;
}