Types in C++

Enumerated Types


An integer really represents a value within a sequence — the sequence of numbers. Enumerated types
let you define your own sequences so that you can declare variables with values in that sequence. For
example, in a chess program, you could represent each piece as an int, with constants for the piece
types, as shown in the following code. The integers representing the types are marked const to indicate
that they can never change.


const int kPieceTypeKing = 0;
const int kPieceTypeQueen = 1;
const int kPieceTypeRook = 2;
const int kPieceTypePawn = 3;
//etc.
int myPiece = kPieceTypeKing;


This representation is fine, but it can become dangerous. Since the piece is just an int, what would
happen if another programmer added code to increment the value of the piece? By adding one, a king
becomes a queen, which really makes no sense. Worse still, someone could come in and give a piece a
value of -1, which has no corresponding constant.


Enumerated types resolve these problems by tightly defining the range of values for a variable. The fol-
lowing code declares a new type, PieceT, that has four possible values, representing four of the chess
pieces.
typedef enum {

         kPieceTypeKing,

         kPieceTypeQueen,

         kPieceTypeRook,
         kPieceTypePawn
} PieceT;

Behind the scenes, an enumerated type is just an integer value. The real value of kPieceTypeKing is
zero. However, by defining the possible values for variables of type PieceT, your compiler can give you
a warning or error if you attempt to perform arithmetic on PieceT variables or treat them as integers.
The following code, which declares a PieceT variable then attempts to use it as an integer, results in a
warning on most compilers.


PieceT myPiece;
myPiece = 0;

 

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.

// employeestruct.h
typedef struct {
char    firstInitial;
char    middleInitial;
char    lastInitial;
int     employeeNumber;
int     salary;
} EmployeeT;


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.


// structtest.cpp
#include
#include “employeestruct.h”


using namespace std;


int main(int argc, char** argv)
{

    // 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;
}

你可能感兴趣的:(The,basics,of,C++)