Object oriented or not?

Well I am writing a clone of the Robotron 2084 arcade game using an Arduini Uno and a Gameduino shield and it is pretty much entirely object orientated. All the moving objects are instances of classes derived from a common base class. All the bullets, the score, the stuff for managing the sound, just about everything are all instances of classes. I even have a Game class from which I derived this particular game.

When I first started using object orientation I was a little hesitant and tended to stick to the tried and tested principles of structured programming and encapsulation but nowadays I wouldn't dream of writing anything other than a trivial program without using object orientation. If you're using something like Visual Studio you don't have much choice these days, but that's another story.

There are one or two caveats:

  1. I now have an Arduino 1.0. As I understand it earlier versions did not support the new and delete operators with classes which means it is hard to get polymorphism to work since virtual methods only really work properly when you are using a pointer to a common base class and object pointers are normally created using new. Not sure I explained that very well but you get the idea.

  2. It is good programming practice to put every class you define in a source file which has the same name as the class (I think Java enforces this). So class Wombat would be declared in Wombat.cpp and Wombat.h. This tends to end up with lots and lots of files and the Arduino IDE isn't ideal for this. It works, but its just not that convenient. This isn't a criticism of the language of course.

  3. If you are only interested in learning object orientation then it would probably be easier to start with something like C# or Java on a PC. They are much easier to use than C++ and you are less likely to suffer from the dreaded memory leaks. Visual C# Express is free and perfectly usable (for non-commercial purposes I think).

So yes, Arduino projects can be written in an object orientated manner but it is probably not the first choice if all you want to do is lean object orientation.