May 9 2010

Introduction to Programming with C++

This set of articles serves to act as a introduction to not just programming in C++, but programming in general. They assume no previous knowledge; my aim is to write the articles such that anyone with a computer, internet access and an interest in computing should be able to read and understand them.

These articles are targeted mainly towards those who want to achieve a deeper understanding of programming, rather than just learning by the basic monkey-see, monkey-do ‘Hello World’ school of teaching.

“Why start learning programming with C++?”, you might ask. There are a few good reasons for this:

  • C++ contains some of the best features of all the programming languages.
  • C++ contains some of the worst features of all the programming languages. Why is this important? Because one of the most important things to learn when becoming a programmer is how to adjust your programming style to the language you are programming in, and this includes avoiding bad practices and/or features that the language may support.
  • C++ allows us to interact at a ‘low level’ with the computer. That is, when we write code in C++ we are very close to what is actually happening in the hardware (as opposed to other languages that may simplify or dumb-down some aspects of the programming). Although this makes programming in C++ difficult (some may say frustrating) at times, ultimately it gives the programmer more control over what is happening and gives us a better understanding of how computers actually work.
  • C++ supports object oriented programming. Object oriented programming is a style of programming that has been used as ‘best practice’ for decades now. It allows us to write bigger, more complex programs using less code that is more reliable.
  • The syntax and control structures of C++ form the basis for a number of popular programming languages. Learn C++ and you’ll quite easily understand code in Java, C#, PHP and many other languages with little or no further education.

Before learning how to program in C++, it would be helpful to think about how a computer actually works. Computers have been constructed using the same basic architecture for decades. The architecture, known as the Von Neumann Architecture, looks something like the following:

Von Neumann Architecture

Von Neumann Architecture

It includes:

  • Input devices; used to provide input to the computer. This includes things such as a mouse and keyboard.
  • Output devices; used to display output from the computer to the user. These devices are things such as printers and monitors
  • External storage; used to provide “long-term” storage. This may include a hard disk, optical drive (CD burner) or USB flash drive.
  • Memory; used for “short-term” storage. Memory is used to hold data the computer needs at the moment. This may include the operating system routines (for example, Windows or Mac OS X code) as well as the currently executing program (for example, the browser program code you’re using at the moment to surf pages on the internet).
  • Control Unit; controls what the computer is doing at the moment and what it will do next. Basically a big list of commands the computer is going to execute, in the order they are going to be executed.
  • Arithmetic Logic Unit; basically a really fast calculator. The Arithmetic Logic Unit, or ALU, executes the commands fed to it from the Control Unit.

This is (from a high-level) how most computers are constructed – and have been constructed since the invention of the computer. When we want to instruct the computer to do something, we need to provide ‘instructions’ which are fed by the control unit to the ALU. These instructions are written in something called Assembly Language, or ‘Machine Code’.

This language consists of numerically coded operations which represent basic arithmetic procedures like addition, subtraction and multiplication. They also provide basic data manipulation operations for moving information around the system. It is these basic principles upon which every computer program ever written is based. From these humble origins we derive CGI movies, MP3 audio technology and social media web applications.

As you can probably imagine, it takes A LOT of number crunching to create something like the latest 3D rendered movie. While very basic applications can be written in machine code, as the complexity of our applications increases, it quickly becomes quite infeasible to operate entirely in machine code. This is where our high-level languages come into the picture.

High level languages, like Java, Python, C and C++ allow us to write code using easily understood symbols. This human-readable code is then converted into machine code by something called a compiler. The human readable code typically consists of:

  • Keywords; words that instruct the compiler what you want to do
  • Variables; word used to represent a unit of data

Don’t worry if these definitions aren’t clear at the moment. They will become obvious in lessons to come. Read over the content in this article, and when you think you have a rough understanding of it all, you can advance onto the next step: installing the compiler and writing your first C++ application. To be continued…