Creating User Types
The great thing about C++ is that you can create your own types using struct, class, enum, or union and the compiler will treat it as a fundamental type throughout the code. In this section, we will explore creating our own type and the methods that we need to write to manipulate it, as well as some methods that the compiler will create for us.
Enumerations
The simplest user-defined type is the enumeration. Enumerations got an overhaul in C++11 to make them even more type-safe, so we have to consider two different declaration syntaxes. Before we look at how to declare them, let's figure out why we need them. Consider the following code:
int check_file(const char* name)
{
FILE* fptr{fopen(name,"r")};
if ( fptr == nullptr)
return -1;
char buffer[120];
auto numberRead = fread(buffer, 1, 30, fptr);
fclose(fptr);
if (numberRead != 30)
return -2;
...