C++ can convert values between related types when code asks for it.

static_cast `static_cast<double>(value)` asks C++ to convert a value to a different type.

Type Conversion

cents
type_conversion.cpp
Replay: real traced execution (multi-file project)
#include <iostream>

int main() {
    int cents = 375;
    double dollars = static_cast<double>(cents) / 100.0;
    int wholeDollars = cents / 100;

    std::cout << "cents=" << cents << std::endl;
    std::cout << "dollars=" << dollars << std::endl;
    std::cout << "wholeDollars=" << wholeDollars << std::endl;
    return 0;
}
#include <iostream>

int main() {
    int cents = 99;
    double dollars = static_cast<double>(cents) / 100.0;
    int wholeDollars = cents / 100;

    std::cout << "cents=" << cents << std::endl;
    std::cout << "dollars=" << dollars << std::endl;
    std::cout << "wholeDollars=" << wholeDollars << std::endl;
    return 0;
}
#include <iostream>

int main() {
    int cents = 1234;
    double dollars = static_cast<double>(cents) / 100.0;
    int wholeDollars = cents / 100;

    std::cout << "cents=" << cents << std::endl;
    std::cout << "dollars=" << dollars << std::endl;
    std::cout << "wholeDollars=" << wholeDollars << std::endl;
    return 0;
}
  1. cents ← 375, dollars ← 3.75, wholeDollars ← 3

    3int main() {4    int cents→ 375 = 375; //@cents=99, 12345    double dollars→ 3.75 = static_cast<double>(cents375) / 100.0;6    int wholeDollars→ 3 = cents375 / 100;78    std::cout << "cents=" << cents375 << std::endl;9    std::cout << "dollars=" << dollars3.75 << std::endl;10    std::cout << "wholeDollars=" << wholeDollars3 << std::endl;11    return 0;12}
    outputcents=375
    dollars=3.75
    wholeDollars=3
  1. cents ← 99, dollars ← 0.99, wholeDollars ← 0

    3int main() {4    int cents→ 99 = 99;5    double dollars→ 0.99 = static_cast<double>(cents99) / 100.0;6    int wholeDollars→ 0 = cents99 / 100;78    std::cout << "cents=" << cents99 << std::endl;9    std::cout << "dollars=" << dollars0.99 << std::endl;10    std::cout << "wholeDollars=" << wholeDollars0 << std::endl;11    return 0;12}
    outputcents=99
    dollars=0.99
    wholeDollars=0
  1. cents ← 1234, dollars ← 12.34, wholeDollars ← 12

    3int main() {4    int cents→ 1234 = 1234;5    double dollars→ 12.34 = static_cast<double>(cents1234) / 100.0;6    int wholeDollars→ 12 = cents1234 / 100;78    std::cout << "cents=" << cents1234 << std::endl;9    std::cout << "dollars=" << dollars12.34 << std::endl;10    std::cout << "wholeDollars=" << wholeDollars12 << std::endl;11    return 0;12}
    outputcents=1234
    dollars=12.34
    wholeDollars=12

Follow the Conversion

  1. cents starts at 375.
  2. Converting to dollars gives 3.75.
  3. wholeDollars keeps only the whole dollar part.
  4. wholeDollars becomes 3.
  5. The program prints cents=375, dollars=3.75, and wholeDollars=3. | cents | dollars | wholeDollars | | --- | --- | --- | | 99 | 0.99 | 0 | | 375 | 3.75 | 3 | | 1234 | 12.34 | 12 |

Exercise: type_conversion.cpp

Reproduce dollars=3.75 and wholeDollars=3, then use cents 99 and 1234 to predict both values.