home *** CD-ROM | disk | FTP | other *** search
- // simple C++ program that demonstrates typecasting
-
- #include <iostream.h>
-
- main()
- {
- short shortInt1, shortInt2;
- unsigned short aByte;
- int anInt;
- long aLong;
- char aChar;
- float aReal;
-
- // assign values
- shortInt1 = 10;
- shortInt2 = 6;
- // perform operations without typecasting
- aByte = shortInt1 + shortInt2;
- anInt = shortInt1 - shortInt2;
- aLong = shortInt1 * shortInt2;
- aChar = aLong + 5; // conversion is automatic to character
- aReal = shortInt1 * shortInt2 + 0.5;
-
- cout << "shortInt1 = " << shortInt1 << '\n'
- << "shortInt2 = " << shortInt2 << '\n'
- << "aByte = " << aByte << '\n'
- << "anInt = " << anInt << '\n'
- << "aLong = " << aLong << '\n'
- << "aChar is " << aChar << '\n'
- << "aReal = " << aReal << "\n\n\n";
-
- // perform operations with typecasting
- aByte = (unsigned short) (shortInt1 + shortInt2);
- anInt = (int) (shortInt1 - shortInt2);
- aLong = (long) (shortInt1 * shortInt2);
- aChar = (unsigned char) (aLong + 5);
- aReal = (float) (shortInt1 * shortInt2 + 0.5);
-
- cout << "shortInt1 = " << shortInt1 << '\n'
- << "shortInt2 = " << shortInt2 << '\n'
- << "aByte = " << aByte << '\n'
- << "anInt = " << anInt << '\n'
- << "aLong = " << aLong << '\n'
- << "aChar is " << aChar << '\n'
- << "aReal = " << aReal << "\n\n\n";
- return 0;
- }
-