SUMMARY

This chapter covered the basics of computation in C++. You have learned about all the elementary types of data provided for in the language, and all the operators that manipulate these types directly.

Although I have discussed all the fundamental types, don’t be misled into thinking that’s all there is. There are more complex types based on the basic set, as you’ll see, and eventually, you will be creating original types of your own.

EXERCISES
1. Write a program that asks the user to enter a number and then prints it out, using an integer as a local variable.
2. Write a program that reads an integer value from the keyboard into a variable of type int, and uses one of the bitwise operators (i.e. not the % operator!) to determine the positive remainder when divided by 8. For example, 29 = (3×8)+5 and −14 = (−2×8)+2 have positive remainder 5 and 2, respectively, when divided by 8.
3. Fully parenthesize the following expressions, in order to show the precedence and associativity:
   1 + 2 + 3 + 4
        
   16 * 4 / 2 * 3
        
   a > b? a: c > d? e: f
        
   a & b && c & d
4. Create a program that will calculate the aspect ratio of your computer screen, given the width and height in pixels, using the following statements:
   int width = 1280;
   int height = 1024;
        
   double aspect = width / height;
When you output the result, what answer will you get? Is it satisfactory — and if not, how could you modify the code, without adding any more variables?
5. (Advanced) Without running it, can you work out ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.