Skip to main content

Section 4.5 Type Casting

As mentioned earlier, C++ converts int’s to double’s automatically if necessary, because no information is lost in the translation. On the other hand, going from a double to an int requires truncating the value and produces a warning.
To remove this warning, you can do a typecast. Typecasting is so called because it allows you to take a value that belongs to one type and β€œcast” it into another type (in the sense of molding or reforming, not throwing).
In C++, the way ask the compiler to do a typecast is to write static_cast<TYPE>(VALUE). When you write that, you are essentially saying β€œI know this looks potentially dangerous, but please go ahead and convert VALUE to type TYPE and do not warn me about it.” This is known as an explicit conversion because you are explicitly (visibly) doing the cast. Here is an example of an explicit conversion of a double value to the int type using static_cast<int>(VALUE):
Note that the static_cast does not change the value of the variable pi. it makes a copy of the value and converts it to the new type (int). The result is exactly the same as if we wrote int x = pi; on line 7 except there is no warning!

Insight 4.5.1.

A static_cast does a conversion the compiler already knows how to do but would normally either not do or issue a warning for.
Using static_cast clearly communicates to both the compiler and other programmers that you are intentionally doing a potentially dangerous conversion (and thus hopefully are aware of the consequences!)
It is possible to type cast an integer value into a double using static_cast<double>(VALUE). Normally, it is not necessary to do so as the compiler will implicitly do the conversion without complaining. But sometimes it is useful to cast an integer to a double in order to force floating-point math. The following example does this to get a decimal answer for 1/3 in the second line of output:
As the line is evaluated, the static_cast happens first. It converts 1 to 1.0. Then the division takes place and it now says 1.0 / 3. Since there is at least one floating-point value involved, C++ does floating-point math.

Note 4.5.2.

In C programming, you cast by writing something like int x = (int)pi;. Although C-style casts can also be written in C++, you should avoid using them.

Checkpoint 4.5.1.

Build code to turn the value in double x to an int using static_cast and store the result into int y.

Checkpoint 4.5.2.

Multiple Response Select all variables that have a non-zero value after the decimal place. (3.1 has a non-zero value, while 3.0 and 3 do not)
#include <iostream>
using namespace std;

int main() {
  int a = 1.5;
  double b = a + 1.5;
  double c = 2.4;
  double d = 1/5;
  int e = c * c;
  double f = static_cast<int>(c);
}
  • C++ performs automatic type conversion to round 1.5 down to the nearest integer.
  • Since a = 1, we know that b = 2.5, which is a non-zero decimal.
  • c is a double and has a non-zero decimal.
  • C++ performs integer division to round 1/5 down to the nearest integer. The value will be stored as 0, not 0.2.
  • c squared may have a non-zero decimal, but automatic type conversion will round it down to the nearest integer before storing the value in e.
  • static_cast truncates c the value to 2. When stored into f it is 2.0.

Checkpoint 4.5.3.

Your final grade consists of your average performance on three exams. Your professor is using C++ to grade the exams and allows you to choose which method you’d like your exam to be graded.
double exam1 = 88.8;
double exam2 = 72.7;
double exam3 = 94.6;
Method 1:
int final = (static_cast<int>(exam1) + static_cast<int>(exam2) + static_cast<int>(exam3)) / 3;
Method 2:
int final = static_cast<int>((exam1 + exam2 + exam3) / 3);
Which method would you choose and why?
  • Method 1: both test scores will be rounded to the nearest int, which in my case, will round all of them up.
  • Converting to an int does not round. It truncates (drops the decimal).
  • It does not matter, both produce the same result.
  • Method 1 throws away the .8, .7, and .6 before averaging, while Method 2 averages first and then throws away the decimal part of the final result. The first method produces a grade of 82 while the second produces a grade of 83.
  • Method 2: the rounding happens at the very end, so my grade will be higher!
  • Always save your rounding until the end!
You have attempted of activities on this page.