The next topic on the class menu is type conversion. We’ll look into how C++ handles conversions to and from user-defined types. To set the stage, let’s first review how C++ handles conversions for its built-in types. When you make a statement that assigns a value of one standard type to a variable of another standard type, C++ automatically converts the value to the same type as the receiving variable, provided that the two types are compatible. For example, the following statements all generate numeric type conversions:

long count = 8;      // int value 8 converted to type long

double time = 11;    // int value 11 converted to type double

int side = 3.33;     // double value 3.33 converted to type int 3

These assignments work because C++ recognizes that the diverse numeric types all represent the same basic thing—a number—and because C++ incorporates built-in rules for making the conversions. Recall from Chapter 3, “Dealing with Data,” however, that you can lose some precision in these conversions. For example, assigning 3.33 to the int variable side results in side getting the value 3, losing the 0.33 part.

The C++ language does not automatically convert types that are not compatible. For example, the following statement fails because the left side is a pointer type, whereas the right side is a number:

int * p = 10;  // type clash

And even though a computer may represent an address internally with an integer, integers and pointers are conceptually quite different. For example, you wouldn’t square a pointer. However, when automatic conversions fail, you may use a type cast:

int * p = (int *) 10;  // ok, p and (int *) 10 both pointers

This sets a pointer to the address 10 by type casting 10 to type pointer-to-int (that is, type int *). Whether this assignment makes sense is another matter.

You may define a class sufficiently related to a basic type or to another class that it makes sense to convert from one form to another. In such a case, you can tell C++ how to make such conversions automatically or, perhaps, via a type cast. To see how that works, you can recast the pounds-to-stone program from Chapter 3 into class form. First, you need to design an appropriate type. Fundamentally, you’re representing one thing (a weight) two ways (pounds and stone). A class provides an excellent way to incorporate two representations of one concept into a single entity. Therefore, it makes sense to place both representations of weight into the same class and then provide class methods for expressing the weight in different forms. Listing 11.16 provides the class header.

Listing 11.16. stonewt.h

// stonewt.h -- definition for the Stonewt class

#ifndef STONEWT_H_

#define STONEWT_H_

class Stonewt

{

private:

    enum {Lbs_per_stn = 14};      // pounds per stone

    int stone;                    // whole stones

    double pds_left;              // fractional pounds

    double pounds;                // entire weight in pounds

public:

    Stonewt(double lbs);          // constructor for double pounds

    Stonewt(int stn, double lbs); // constructor for stone, lbs

    Stonewt();                    // default constructor

    ~Stonewt();

    void show_lbs() const;        // show weight in pounds format

    void show_stn() const;        // show weight in stone format

};

#endif

As mentioned in Chapter 10, enum provides a convenient way to define class-specific constants, provided that they are integers. Or you could use the following alternative:

static const int Lbs_per_stn = 14;

Note that the Stonewt class has three constructors. They allow you to initialize a Stonewt object to a floating-point number of pounds or to a combination of stone and pounds. Or you can create a Stonewt object without initializing it:

Stonewt blossem(132.5);   // weight = 132.5 pounds

Stonewt buttercup(10, 2); // weight = 10 stone, 2 pounds

Stonewt bubbles;          // weight = default value

The class doesn’t really need to declare a destructor because the automatic default constructor is fine for this case. On the other hand, providing an explicit declaration prepares you for the future, when you will have to define constructors.

Also the Stonewt class provides two display functions. One displays the weight in pounds, and the other displays the weight in stone and pounds. Listing 11.17 shows the class methods implementation. Note that each constructor assigns values to all three private members. Thus, creating a Stonewt object automatically sets both representations of weight.

Listing 11.17. stonewt.cpp

// stonewt.cpp -- Stonewt methods

#include

using std::cout;

#include "stonewt.h"

// construct Stonewt object from double value

Stonewt::Stonewt(double lbs)

{

    stone = int (lbs) / Lbs_per_stn;    // integer division

    pds_left = int (lbs) % Lbs_per_stn + lbs - int(lbs);

    pounds = lbs;

}

// construct Stonewt object from stone, double values

Stonewt::Stonewt(int stn, double lbs)

{

    stone = stn;

    pds_left = lbs;

    pounds =  stn * Lbs_per_stn +lbs;

}

Stonewt::Stonewt()          // default constructor, wt = 0

{

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги