C++ In Action

Language

Go to chapter  

  • Objects and Scopes
  • What's the most important thing in the Universe? Is it matter? It seems like everything is built from matter-galaxies, stars, planets, houses, cars and even us, programmers. But what's matter without energy? The Universe would be dead without it. Energy is the source of change, movement, life. But what is matter and energy without space and time? We need space into which to put matter, and we need time to see matter change.

    Programming is like creating universes. We need matter: data structures, objects, variables. We need energy--the executable code--the lifeforce of the program. Objects would be dead without code that operates on them. Objects need space to be put into and to relate to each other. Lines of code need time to be executed. The space-time of the program is described by scopes. An object lives and dies by its scope. Lines of executable code operate within scopes. Scopes provide the structure to program's space and time. And ultimately programming is about structure.

    Go to chapter  

  • Arrays and References
  • In a program, an object is identified by its name. But if we had to call the object by its name everywhere, we would end up with one global name space. Our program would execute in a structureless "object soup." The power to give an object different names in different scopes provides an additional level of indirection, so important in programming. There is an old saying in Computer Science--every problem can be solved by adding a level of indirection. This indirection can be accomplished by using a reference, an alias, an alternative name, that can be attached to a different object every time it enters a scope.

    Computers are great at menial tasks. They have a lot more patience that we humans do. It is a punishment for a human to have to write "I will not challange my teacher's authority" a hundred times. Tell the computer to do it a hundred times, and it won't even blink. That's the power of iteration (and conformity).

    Go to chapter  

  • Pointers
  • Using references, we can give multiple names to the same object. Using pointers, we can have the same name refer to different objects--a pointer is a mutable reference.

    Pointers give us power to create complex data structures. They also increase our ability to shoot ourselves in the foot. Pointer is like a plug that can be plugged into a jack. If you have too many plugs and too many jacks, you may end up with a mess of tangled cables. A programmer has to strike a balance between creating a program that looks like a breadboard or like a printed circuit.

    Go to chapter  

  • Polymorphism
  • Polymorphic means multi-shaped. A tuner, a tape deck, a CD player--they come in different shapes but they all have the same audio-out jack. You can plug your earphones into it and listen to music no matter whether it came as a modulation of a carrier wave, a set of magnetic domains on a tape or a series of pits in the aluminum substrate on a plastic disk.

    Go to chapter  

  • Small Software Project
  • When you write a program, you don't ask yourself the question, "How can I use a particular language feature?" You ask, "What language feature will help me solve my problem?"