Professional C__ - Marc Gregoire [7]
Strongly Typed Enumerations
Enumerations as explained above are not strongly typed, meaning they are not type-safe. Basically, they are always interpreted as integers and thus you can compare enumeration values from completely different enumeration types. C++11 introduces the enum class to solve these problems. For example:
enum class MyEnum
{
EnumValue1,
EnumValue2 = 10,
EnumValue3
};
Code snippet from StronglyTypedEnums\StronglyTypedEnums.cpp
This is a type-safe enumeration called MyEnum. The enumeration values are not automatically converted to integers and the enumeration names are not automatically exported to the enclosing scope. Because of this, the following is not legal in C++11:
if (MyEnum::EnumValue3 == 11) {...}
By default, the underlying type of an enumeration value is an integer, but this can be changed as follows:
enum class MyEnumLong : unsigned long
{
EnumValueLong1,
EnumValueLong2 = 10,
EnumValueLong3
};
Code snippet from StronglyTypedEnums\StronglyTypedEnums.cpp
Structs
Structs let you encapsulate one or more existing types into a new type. The classic example of a struct is a database record. If you are building a personnel system to keep track of employee information, you will need to store the first initial, last initial, middle initial, employee number, and salary for each employee. A struct that contains all of this information is shown in the header file that follows.
typedef struct {
char firstInitial;
char middleInitial;
char lastInitial;
int employeeNumber;
int salary;
} EmployeeT;
Code snippet from structtest\employeestruct.h
A variable declared with type EmployeeT will have all of these fields built-in. The individual fields of a struct can be accessed by using the “.” character. The example that follows creates and then outputs the record for an employee.
#include #include "employeestruct.h" using namespace std; int main() { // Create and populate an employee. EmployeeT anEmployee; anEmployee.firstInitial = 'M'; anEmployee.middleInitial = 'R'; anEmployee.lastInitial = 'G'; anEmployee.employeeNumber = 42; anEmployee.salary = 80000; // Output the values of an employee. cout << "Employee: " << anEmployee.firstInitial << anEmployee.middleInitial << anEmployee.lastInitial << endl; cout << "Number: " << anEmployee.employeeNumber << endl; cout << "Salary: $" << anEmployee.salary << endl; return 0; } Code snippet from structtest\structtest.cpp Conditionals Conditionals let you execute code based on whether or not something is true. As shown in the following sections, there are three main types of conditionals in C++. if/else Statements The most common conditional is the if statement, which can be accompanied by else. If the condition given inside the if statement is true, the line or block of code is executed. If not, execution continues to the else case if present, or to the code following the conditional. The following pseudocode shows a cascading if statement, a fancy way of saying that the if statement has an else statement that in turn has another if statement, and so on. if (i > 4) { // Do something. } else if (i > 2) { // Do something else. } else { // Do something else. } The expression between the parentheses of an if statement must be a Boolean value or evaluate to a Boolean value. Conditional operators, described later, provide ways of evaluating expressions to result in a true or false Boolean value. switch Statements The switch statement is an alternate syntax for performing actions based on the value of a variable. In switch statements, the variable must be compared to a constant, so the greater-than if statements above cannot be converted to switch statements. Each constant value represents a “case.” If the variable matches the case, the subsequent lines of code are executed until the break statement is reached. You can also provide a default case, which is matched if none of the other cases match. switch statements are generally used when you want to do something based