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