C++ is the successor to C. They come from the same place – Dennis Ritchie made C at Bell Labs in 1972 and Bjarne Stroustrup made C++ at Bell Labs in 1983. Stroustrup was going for an extension to the C language, to support all the extra features that were missing or clumsy to implement in C. What kind of features? Objects.
You couldn’t possibly have investigated programming this far without already encountering the concept of Object Oriented Programming (OOP) being whacked into your noggin with a mallet. What is OOP, and why is it such a huge matter? Let’s try to deal with that issue clearly and quickly:
Say you’re writing a series of card games. For your poker game, you first had to design a programmatic representation of a deck of playing cards. You have to simulate shuffling, dealing, making sure the computer keeps track of every card in play and doesn’t duplicate them, and you also have to build in the concept of a suit and a rank, for computing flushes and straights. Each card is both a piece of data and a link to the image of the card on the screen. This is a minor challenge for the novice programmer; solving all of these nitty little problems one at a time, and coding and testing each function. But in the end, you have your poker game.
Suppose you then decide to write a blackjack game. Now, if you’re a beginner, you probably made the mistake of weaving the card deck code into the poker game code. Oh, no! Now, even though you’ve already solved the problem of the card deck once, you have to turn around and do it all over again as part of your new blackjack game!
After getting bitten with these kinds of problems a few times, you’ll start wanting… it’s hard to describe when you don’t know what it is, isn’t it?… you want some way that the code for a card deck could be self-contained. Then you could simply copy the card deck from your poker game to your blackjack game, and then all you have to do is write different rules for the new game! And so on while you go on to tackle gin rummy, Texas hold-em, and Caribbean stud. In other words, you need a card deck object.
Here is where you might stop and draw up the plan for your card deck idea, maybe on a scratch pad:
array[4,13] // four suits, thirteen ranks
initialize the card deck, link to graphics, load array.
randomly shuffle the deck.
return one card each time you're called.
All of this is behavior that won’t change for any game involving a standard 52-card deck. Not to keep you guessing a moment longer, here is just such an example of the object code for a deck of cards, in a listing provided by a university.
The concept we’re striving for is “code re-use” by “encapsulation”. We want to solve each problem exactly once, without having to back-track. Of course, in the real world, there will still be special cases. In straight draw poker, we need only be concerned with the ranks and suits, but in blackjack we have to add the values of cards together – and the Ace gets counted as one or eleven! And then what if you want to do a wild-card poker game?
And so we stumble upon the concepts of “inheritance” and “polymorphism”. For a joker’s wild game, we can make a new deck object which inherits the behavior of the standard deck but adds a joker or two and replaces our formerly 4×13 square array with a single-dimension array of 53 or 54 cards. The other behaviors can be modified to fit the new requirements. Object inheritance lets our new object copy the parts of the old deck that don’t change, while polymorphism would allow us to only change the parts that have to be adapted to our new purpose.
Most, if not all, of the C compilers we linked to in the last article can also compile C++. The GNU compiler collection includes g++, Bloodshed Software Dev-C++ works for both, and so on.
We’ll skip the code listing that we did with our last installment about C, because much of C++ is backwards-compatible with C. The listing we linked to is a good illustration of the extended powers of C++. Also, by the time you want sophisticated tools of this power, you need some formal education and probably have goals towards programming for a career, in which case a mere blog post isn’t going to do you much good.
We will also dodge the comparison of C, C++, and the other C-like object-oriented language (that would be Java!), because whole forums have been filled with arguments about it every which way. Whichever of these three you pick, you might have the idea that sometimes the compiled C-based languages are a lot of trouble to go to for a simple one-shot project. Do we really have to tell the computer every little thing, every time? There are languages out there where you just tell the computer the basics and it knows how to fill in even more detail – tell it MyString=”hello” without needing to specify the variable as a string containing five bytes, for instance.
In short, we have higher-level languages to explore. And some of them are going to show you startling new ways to program that you never would have thought possible after seeing Assembly, C, and C++…
If you’ve enjoyed this post, consider subscribing to my blog feed for free updates
- Six Exercises to Learn Programming
- The Python Programming Language
- The C Language
- Playing with the Google calculator
- Sunday’s Spam