User-Defined Types in C++
- Surprising no one, user-defined types are types that the user can define.
Enumerations
- The simplest.
- The values that an enumeration can take are restricted to a set of possible values.
- Excellent for modelling categorical concepts.
Classes
- More fully featured types that give you flexibility to pair data and functions.
- Classes that only contain data are called “plain-old-data” classes.
Unions
- Boutique user-defined type.
- All members share the same memory location.
- Unions are dangerous and easy to misuse.
Enumeration Types
- Declare enums using keyword enum class, followed by the type name and a listing og the values it can take.
- The values are arbitrary alphanumeric strings that will represent whatever categories you want to represent.
- Under the hood these values are just integers.
- However, these allow you to write safer, more expressive code by using programmer-defined types rather than integeres that could mean anything.
- To intialize an enumeration variable to a value, use the name of the type, followed by two colons (::) and the desired value.
Plain-old-data Classes (PODs)
- Classes are user-defined types that contain data and functions.
- They are the heart and soul of C++ that allow you to express yourself.
- Plain-old-date classes are simple containers.
- Think of them as a sort of array of elements of potentially DIFFERENT types.
- Each element within a class is called a MEMBER.
- PODs are C compatible, they are efficiently represented in memory, and you can employ machine instructions that are highly efficient to copy/move them.
- C++ also guarantees that members will be sequential in memory.
- In general, you should order members from largest to smallest within POD definitions.
- Every POD begins with the keyword struct followed by the PODs desired name.
- Next, list the members types and names.
- So in usertypes.cpp, you can see we created a POD by using struct Book{}.
- To access or set one of those members, you use dot-operators.
- So if I create a Book object Dune, I can set the page count by using dune.pages
Working examples
These are rendered from the source stored beside this drawer's notes.
user-defined-types.cpp
raw source ↗#include <cstdio>
// The purpose of the below code is to demonstrate the creation of an enum class, example altered from C++ crash course.
enum class Race {
Human,
Turian,
Salarian,
Krogan,
Asari,
Geth,
Quarian,
};
// The purpose of the below code is to demonstrate initializing an enumeration variable to a value.
Race garrus_race = Race::Turian;
//The below function's purpose is to demonstrate the use of a switch statement with an enumeration class.
void ME_party_member_quote(){
Race character_race = Race::Human;
switch(character_race) {
case Race::Human: {
printf("I'm Commander Shepherd, and this is my favorite store on the Citadel.\n");
} break;
case Race::Turian: {
printf("I'm Garrus Vakarian. It's so much easier to see the world in black and white. Gray? What do I do with that?\n");
} break;
case Race::Salarian: {
printf("I'm Mordin. I am the very model of a scientist salarian, I’ve studied species turian, asari, and batarian.\n");
} break;
case Race::Krogan: {
printf("I'm Urdnot Wrex. Why shoot something once when you can shoot it 46 more times?\n");
} break;
case Race::Asari: {
printf("I'm Liara T'Soni. It doesn't have any weapons, Shephard. It's a taxi, it has a fare meter!\n");
} break;
case Race::Geth: {
printf("We are Legion. Geth do not infilitrate. Geth do not..intentionally infiltrate.\n");
} break;
case Race::Quarian: {
printf("I am Tali'Zorah vas Normandy. I am getting drunk very carefully. Turian brandy, triple filtered, then introduced into the suit through an emergency induction port.\n");
} break;
default: {
printf("Error: unknown race!\n");
}
}
}
// The purpose of the below code is to demonstrate the creation of a plain-old-data class using struct and then accessing it with a function.
struct Book {
char name[256];
int year;
int pages;
bool hardcover;
};
void favorite_book(){
Book dune;
dune.pages = 704;
printf("Dune has %d pages in it.\n", dune.pages);
}
int main(){
ME_party_member_quote();
favorite_book();
}