Chapter 14. Other Numerical Variable Types
In This Chapter
Reviewing the limitations of integers
Introducing real numbers to C++
Examining the limitations of real numbers
Looking at some variable types in C++
Overloading function names
The programs so far have limited themselves to variables of type
int with just a few
chars thrown in. Integers are great for most calculations — more than 90 percent of all variables in C++ are of type
int. Unfortunately, int variables aren't adapted to every problem. In this chapter, you will see both variations of the basic
int as well as other types of intrinsic variables. An intrinsic type is one that's built into the language. In Chapter 19, you will see how the programmer can define her own variable types.
Some programming languages allow you to store different types of data in the same variable. These are called weakly typed languages. C++, by contrast, is a strongly typed language — it requires you to declare the type of data the variable is to store. A variable, once declared, cannot change its type.
The Limitations of Integers in C++
int variable type is the C++ version of an integer. As such,
int variables suffer the same limitations as their counting integer equivalents in mathematics do.
It isn't that an integer expression can't result in a fractional value. It's just that an
int has no way of storing the fractional piece. The processor lops off the part to the right of the decimal point before storing the result. (This ...