//Program based on chapter 2 illustrates implicit type casting (coercion) //integer division and characters treated as integers //illustrates casting a int to a char //Carol Conway 8/27/17 //File: typeconversions.cpp #include using namespace std; int main() { int i = 11, j = 3, ch = 75; float z = 123.65432; char achar = 'A', bchar; cout << "Size of a character is " << sizeof(char) << endl; short int k; cout << "Size of a short is "<< sizeof(short) << endl; bchar = achar + 1; //arithmetic performed on characters uses their ASCII codes cout << "bchar: " << bchar << endl; k = achar; cout << "The ASCII code for 'A' is " << k << endl; //treats the char as an int (uses ASCII code) cout << i << " divided by "<< j << " is " << i/j << endl; cout << i << " mod "<< j << " is " << i % j << endl; cout << "z is " << z << endl; i = z; //implicit type coercion float to int cout << "i is " << i << endl; cout << "The character represented by " << ch << " is " << static_cast(ch) << endl; i = 11; j = 4; z = static_cast(i/j); // notice that integer division still takes place before the type cast cout << i << "\\"<< j << "=" << z << endl; return 0; }