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 <iostream>
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值