C++ Programming As A Set Of Problems
WE THE WRITERS of this book want to address an area where it seems programming books make a big mistake. From what we've seen, programming tutorials and books for C++ (and other languages, for that matter) are organized in the same old way. They start with "basics" like variables and control flow, each chapter dedicated to a different part of the programming language. They teach the language, they just don't teach things that are absolutely fundamental to programming. Then there are other books who say that they don't just teach the language, they teach "real world" examples. These books tend to compact the language half, and add in chapters for user interface programming or network programming. Once again, this is not teaching exactly what is needed. It's good that they are spreading around knowledge, so that the reader knows a bit of useful skills that they can apply towards their program making. It's bad that they are compacting knowledge of the language, which is itself just as useful.
However, we are still missing something fundamental to programming. What does every programmer do, no matter what programming language, and no matter what task? They solve problems! Problem solving is a skill central to programming. The programmer could know every interface, every library in the world, but it would all come to nothing if they didn't know how to make their knowledge useful. You, as a programmer, not only need to know the language, in its entirety: quirks, oddities, and all, you need to know how to use that knowledge and apply it towards the problem.
So this is where we come in. We're going to take a different approach to teaching programming. It's true that we will teach the programming language C++. It's true that we may also spoil some of those joyful weekends we as programmers spent solving the problems we present in this book a year or two after we have learned to program. For that reason, we will leave the book with a list of "problems" for the reader, pointing to various articles about the problems, so that they to can experience joyful weekends making Tic Tac Toe programs or Sudoku Solvers, as it was these "weekends" that inspired some of us to teach C++ using them, because it's those times when you try new things without worrying about messing something up, and it's those times that new skills truly develop.
Another common thing that I've seen in C++ books is that they teach C before C++, even though they don't say it. C++, as you may have guessed, is derived from another programming language, C, but with many, many extensions, even innovations. Where as C could be taught in a single chapter of a book (and often is), C++ is likely to take up a good chunk of the book. The code in many books early on looks and acts like C code, without using good C++ conventions, yet it is these chapters that become permanently graven in the reader's mind (you'd be amazed how much these have an effect, even if you can't remember the paragraphs 5 minutes after you read them), as they are their first steps into programming. So, in the spirit of Accelerated C++ (a highly recommended commercial book), we will start out with C++ code, and finish with C++ code.
- Dev-C++ (uses the GCC compiler neatly)
- MinGW (uses the GCC compiler)
- Cygwin (uses the GCC compiler directly, you'll have to generate compile codes yourself (note: this also goes for MingW), you'll be helped with that ;-) )
These are free and open source, but you might still like to use a commercial compiler instead. As long as it is compatible with ANSI-C and/or ANSI/ISO C++, you won't have to worry much (although there is one compiler, which rumors say, is the least standards conforming compiler made by man). Otherwise just edit this book and add what worked for you. Suggested non-open source compilers are Microsoft Visual C++ or Borland C++.
Table Of Contents[edit | edit source]
Your first problem: The Tower Of Hanoi. It might not sound exciting, but it makes a great introduction to programming. In this chapter we will make a program that solves the Tower Of Hanoi, showing us the individual steps it took to arrive at a solution. We will first decipher the problem, figure out a solution, then create the code. The exercise will teach you C++ basics such as functions, variables, conditional statements, formatted output, and containers. Your first program is truly one to be proud of.
A program that outputs a simple HTML page onto the console screen.