typecast.cpp 788 B

12345678910111213141516171819202122232425
  1. // typecast.cpp -- forcing type changes
  2. #include <iostream>
  3. int main()
  4. {
  5. using namespace std;
  6. int auks, bats, coots;
  7. // the following statement adds the values as double,
  8. // then converts the result to int
  9. auks = 19.99 + 11.99;
  10. // these statements add values as int
  11. bats = (int) 19.99 + (int) 11.99; // old C syntax
  12. coots = int (19.99) + int (11.99); // new C++ syntax
  13. cout << "auks = " << auks << ", bats = " << bats;
  14. cout << ", coots = " << coots << endl;
  15. char ch = 'Z';
  16. cout << "The code for " << ch << " is "; // print as char
  17. cout << int(ch) << endl; // print as int
  18. cout << "Yes, the code is ";
  19. cout << static_cast<int>(ch) << endl; // using static_cast
  20. // cin.get();
  21. return 0;
  22. }