Data Conversion

As you design classes, one of the "operator" types to consider is data conversion. (Technically this is not an operator since the keyword operator does not appear in the definition.) When working with the system data types, it was sometimes necessary to convert from one data type to another.

For example:

image from book

 anInt = int(aFloat); anInt = int(aChar); aFloat = float(anInt); 

image from book

This type of data conversion is referred to as explicit data conversion because it requires an explicit function to be defined. If an int is converted to a float, then no special conversion function is needed. This type of conversion is referred to as implicitly data conversion.

The class Date again is not a good candidate for an explicit data conversion. However if the class of rational numbers (i.e. numbers of the form a/b where a & b are integers) was defined, it would be desirable to convert back and forth between the rationales and the system data types. See ratoreal.cpp

Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
Year: 2007
Pages: 142 © 2008-2017.
If you may any questions please contact us: