C++ Programming As A Set Of Problems

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

I wanted to address something that it seems like books make a big mistake on. From what I've seen, all programming tutorials or books for C++ (or any other language for that matter) are organized in the same old way. They start with "basics" like variables and control flow, each chapter is dedicated to a different part of the programming language. They teach the language, they 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. What does every programmer do, no matter what programming language, no matter what program? They solve problems! Problem solving is a very important skill in programming. You could know every interface, every library in the world, but it wouldn't matter if you didn't know how to apply it towards *your* problem. You, as a programmer, need not only to know the language, the entire language in all its quirks and oddities, you need to know how to use that language and apply it towards your problem.

So this is where I come in. I'm going to take a different approach to teaching programming. Indeed, I will teach the programming language C++. Indeed, I may also ruin some of those joyful weekends we as programmers spend solving the problems I present in this book a year or two after we have learned to program. For that reason, I 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 a joyful weekend making a Tic Tac Toe program or Sudoku Solver, as it was these "weekends" that inspired me to teach C++ using them, because its those times that you try new things without worrying about destroying something, and its 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 state it. C++, as you may have guessed, is derived from another programming language, C, but with many, many improvements. 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, it doesn't use good C++ conventions, yet it is these chapters that someone new will remember what is taught forever (you'd be amazed how much these have an affect, even if you can't remember the paragraphs 5 minutes after you read them), as it is their first steps into programming. So, in the spirit of Accelerated C++ (a very highly recommended commercial book), I will start out with C++ code, and finish with C++ code.

Compilers

  • 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 still might want to use a commercial compiler instead. As long as it is compatible with ANSI-C and/or ANSI/ISO C++, you don't have to worry so much (although there is one compiler where rumors say, it's the least standards conforming compiler made by man). Else just edit this book and add what worked for you. Suggestions can be Microsoft Visual C++ or Borland C++.

[edit] Table Of Contents

[edit] Chapter 1, Covering The Basics

Your first problem: The Tower Of Hanoi. Exciting as it doesn't sound, 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 make the code. It 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.