#52WeeksOfCode Week 14 – C++

Week: 14

Language: C++

IDE(s): XCode

Background:

It’s been a few years since I’ve used C++. I started out with Fortran in 1977 (with punch cards…pardon me while I adjust the onion in my belt), then Basic, moving on through Pascal, shell scripting,Lisp, Assembler, awk and C. I generally picked up programming languages as I needed them for work or school. (Frankly once you get past the first two or three, it becomes easier to get up to speed on new ones. The trick to learning a programming language is to have a project that holds your interest.)

C++ was basically the C language with some extras. (The name is a bit of an inside joke, meaning literally “increment C by 1”.) So this means that you can mix C code with C++ code and it will still compile and run. It’s not a good idea, for the sake of manageability, but it will work.

I like C++. I was comfortable with C and C++ added enough good features that the transition wasn’t too tough. (C# on the other hand…I’m not a fan. Just my opinion.)

C++ was my first introduction to Object-Oriented Programming (OOP). Previously, I felt like I had to micromanage every activity of my software. With OOP, however, I could create software objects with properties (things they know) and methods (things they know how to do) and just set them loose with instructions. It seemed pretty natural to me and there was a good library of pre-written objects that I could use so I didn’t have to re-invent the wheel.

At the time, C++ and Java were the New Hotness so these were the languages I taught in my school’s Software Engineering program. C# did come up later and we incorporated it into the curriculum, displacing both C++ and Java.  But before that happened, I discovered two of what are still my favorite C++ textbooks:

Fundamentals of C++ and Data Structures (Lambert) – Despite the title, this is a surprisingly friendly book. You will need to have some programming background but the book opens with a quick review of the essentials of C++. The writing is friendly, with plenty of diagrams and code examples and it even walks you through the math of analyzing algorithms, a topic that can be a bit intimidating to the newbie coder.

Beginning C++ Game Programming (Dawson) – This is a surprisingly subversive book. Your typical programming textbook is pretty dry and full of dull, mostly theoretical assignments. They might try to liven it up a bit by having you ‘create an inventory management system for a video rental store.’ Get it? Because renting videos and managing store inventory are what all the cool kids are doing these days!

But Dawson takes a different tack and I applaud him for it. He spends a lot of time talking about computer games and how they work with plenty of examples. The fact that these examples just happen to use the programming technique in the current chapter is just a happy coincidence. So the student spends the entire time messing around with games and by the end of the book is dealing with topics like inheritance and polymorphism. I think this is brilliant especially since my edition is copyright 2004, several years prior to the gamification of learning to code. I’m not sure why more computer textbooks for beginners don’t use this technique. I think you could even teach math like this. (Note to self: idea for a math textbook….)

Both of these books still have pride of place on my bookshelf.

Discussion:

C++, Java and C# were all designed to solve the same problem – those darn programmers. All of these languages impose structure and new rules on programmers in an attempt to keep them from stomping all over system resources either accidentally or on purpose. C++ and C# did this by building on C (C# added some features like garbage collection from Java) and Java just wrote the rules from scratch.

As a result, C++ and C# still let you write misbehaving programs and count on you wanting the new features enough to code in a safer, more managed fashion. Java doesn’t let you do that, which is why some programmers think Java is too structured. Which, to Java, is sort of the point.

Once again, this week’s program is pretty simple. Just a little hangman program with some sample code from Dawson:

macpro15:week_14_cplus tsinclair$ ./week_14_cplus
Welcome to Hangman.  Good luck!
You have 8 incorrect guesses left.
You've used the following letters:
So far, the word is:
-----
Enter your guess: e
That's right! E is in the word.
You have 8 incorrect guesses left.
You've used the following letters:
E
So far, the word is:
-E---

Enter your guess: l
That's right! L is in the word.

You have 8 incorrect guesses left.
You've used the following letters:
EL
So far, the word is:
-ELL-

Enter your guess: h
That's right! H is in the word.

You have 8 incorrect guesses left.
You've used the following letters:
ELH
So far, the word is:
HELL-

Enter your guess: o
That's right! O is in the word.
You guessed it!
The word was HELLO

 

In case you’re interested, the source code is available here.

 

References:

Lambert, K., & Naps, T. L. (1998).Fundamentals of program design and data structures with C. Cincinnati: South-Western Educational Pub.

Dawson, Michael. Beginning C through Game Programming. Australia: Course Technology, 2011. Print.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s