// Mr. Minich
// CMPSC 101
// Ch. 1 Demo Program #5
// January 30, 2000 
// Purpose - to illustrate type coercion (aka promotion or implicit type conversion)
// & type casting (explicit type conversion)

#include <iostream>
using namespace std;

int main() { int int1 = 1; // int1 is declared and initialized in one statement int int2 = 2;
int int3; // int3 is mistakenly not initialized
double double1 = 0.0; double double2 = 2.0; int3 = int1 + int2; // int3 = 3 (the whole number) cout << "int3 = " << int3 << endl; double1 = int1 + double2; // double1 = 3.0 (the decimal number) // this is an example of type coercion (aka promotion), where C++ automatically // promotes int1 (the whole number) to the double type in order // to be able to perform the addition of two doubles. Technically, // C++ cannot add an int to a double even though both are 4-bytes.
cout << "double1 = " << double1 << endl; double2 = 1999.99; cout << "The price of the computer is $" << double2 << endl; cout << "The price of the computer is $" << int (double2) << endl;

// type casting is used here by the programmer to // force C++ to interpret double2 as an integer // resulting in the truncation of the value 1999.99 to simply 1999 // The newer form of type casting in C++ is to use static_cast <int> as in:

// cout << "The price of the computer is $" << static_cast <int> (double2) << endl; // Summary: Type coercion (promotion) is automatically used by the C++ compiler // to evaluate expressions while typecasting is explicitly // used by the programmer for whatever reason. Typecasting and // type coercion are examples of mixing data types within the same statement.
return 0; }// end of main