home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / typcast1.cpp < prev    next >
C/C++ Source or Header  |  1993-03-18  |  1KB  |  48 lines

  1. // simple C++ program that demonstrates typecasting
  2.  
  3. #include <iostream.h>
  4.  
  5. main()
  6. {
  7.     short shortInt1, shortInt2;
  8.     unsigned short aByte;
  9.     int anInt;
  10.     long aLong;
  11.     char aChar;
  12.     float aReal;
  13.  
  14.     // assign values
  15.     shortInt1 = 10;
  16.     shortInt2 = 6;
  17.     // perform operations without typecasting
  18.     aByte = shortInt1 + shortInt2;
  19.     anInt = shortInt1 - shortInt2;
  20.     aLong = shortInt1 * shortInt2;
  21.     aChar = aLong + 5; // conversion is automatic to character
  22.     aReal = shortInt1 * shortInt2 + 0.5;
  23.  
  24.     cout << "shortInt1 = " << shortInt1 << '\n'
  25.          << "shortInt2 = " << shortInt2 << '\n'
  26.          << "aByte = " << aByte << '\n'
  27.          << "anInt = " << anInt << '\n'
  28.          << "aLong = " << aLong << '\n'
  29.          << "aChar is " << aChar << '\n'
  30.          << "aReal = " << aReal << "\n\n\n";
  31.  
  32.     // perform operations with typecasting
  33.     aByte = (unsigned short) (shortInt1 + shortInt2);
  34.     anInt = (int) (shortInt1 - shortInt2);
  35.     aLong = (long) (shortInt1 * shortInt2);
  36.     aChar = (unsigned char) (aLong + 5);
  37.     aReal = (float) (shortInt1 * shortInt2 + 0.5);
  38.  
  39.     cout << "shortInt1 = " << shortInt1 << '\n'
  40.          << "shortInt2 = " << shortInt2 << '\n'
  41.          << "aByte = " << aByte << '\n'
  42.          << "anInt = " << anInt << '\n'
  43.          << "aLong = " << aLong << '\n'
  44.          << "aChar is " << aChar << '\n'
  45.          << "aReal = " << aReal << "\n\n\n";
  46.     return 0;
  47. }
  48.