Boost is mostly made of just header files, as we saw last week, which means you include them in your source, add the correct namespace and you’re good to go. However, there are a handful of compiled libraries too, so let’s take a very quick look at how we can use these. If you haven’t installed [...]
Boost is simply a collection of C++ libraries that provide lots of fun things to do in C++ without you having to write the code yourself. For example, Boost contains libraries to help you with mathematical calculations, regular expressions, smart pointers and even python integration. Parts of Boost have already been integrated into C++11, but [...]
We looked at dynamic memory in C last week, so let’s compare it with dynamic memory in C++. Allocation In C++, if you want to allocate on the heap rather than the stack, you need to use the new operator. Just like the C equivalents, the new operator returns a void* pointer to the block [...]
I wrote some posts a while back on the principles of object oriented programming and have collated them into this 10 minute guide as a PDF file that you can download. It covers the main concepts of OOP and also has a simple example of polymorphism in C++. Get your copy here.
There you are, happily programming away, when suddenly you get a compile error: error: jump to case label error: crosses initialization of ‘int x’ “Huh?” You say, peering at the computer screen. Your code looks fine, so what does it mean? Look closely at your switch statement. A switch statement contains case labels, which provide [...]
Lots of functions in the C standard library will set errno to an error code if something goes wrong, so using errno in your programming can help you pinpoint where problems are occurring and what they might be. errno remains set at the last error code, so bear in mind that: a) if two subsequent [...]
Occasionally I’ve heard these terms used interchangeably – but they actually refer to two completely separate concepts. So what’s what? Overloading describes the creation of a method with the same name as an existing method, but different parameters. For example: int method(); int method(int); int method(int, double); Overriding describes the creation of a method in [...]
28 September 2011 – 08:35
When you first start using C++ you tend to set member variables with default values inside your constructor. For example, you might have a class called Message that has three variables, and your constructor sets the default values like this: Message::Message() { messageLength = 0; messageType = 0; messageBody = “Empty message”; } This is [...]
If you’ve read my 5 minute guide What Is Polymorphism? and want to see it in action, here’s some C++ code that illustrates the example I mentioned in that post. I’ve kept the code to header files only for brevity, but as you well know, in the real world, objects would be split out into [...]
I was once asked in an interview, “Can you tell me what polymorphism is?” A straightforward enough question. Or is it? This can be exactly the kind of question that catches you off guard. You’ve used objects for years and of course you know what polymorphism is. But can you explain it in a concise [...]