< previous page page_990 next page >

Page 990
erence is the one that we have been using all along and continue to use throughout the book.
By now, you have probably noticed that the ampersand (&) has several meanings in the C++ language. To avoid errors, it pays to keep these meanings distinct from each other. Below is a table that summarizes the different uses of the ampersand. Note that a prefix operator is one that precedes its operand(s), an infix operator lies between its operands, and a postfix operator comes after its operand(s).
PositionUsageMeaning
Prefix& VariablesAddress-of operation
InfixExpression & ExpressionBitwise AND operation (mentioned, but not explored, in Chapter 10)
InfixExpression && ExpressionLogical AND operation
PostfixDataType&Data type (specifically, a reference type)
Exception: To declare two variables of reference type, the
& is attached to the variable name: int &var1, &var2;

Classes and Dynamic Data
When programmers use C++ classes, it is often useful for class objects to create dynamic data on the free store. Let's consider writing a variation of the DateType class of Chapter 15. In addition to a month, day, and year, we want each class object to store a message string such as My birthday or Meet Al. When a client program prints a date, this message string will be printed next to the date. To keep the example simple, we'll supply only a bare minimum of public member functions. We begin with the class declaration for a Date class, abbreviated by leaving out the function preconditions and postconditions.
class Date
{
public:
    void Print() const;                     // Output operation
    Date( /* in */ int         initMo,      // Constructor
          /* in */ int         initDay,
          /* in */ int         initYr,
          /* in */ const char* msgStr );
private:
       int   mo;
       int   day;

 
< previous page page_990 next page >