Classes in C++

If you’ve been following this blog, then you know I just wrote an article about passing objects as arguments in C++.

This article was originally intended to be part of that one. But while writing it, I realized that you need to know about objects in general, and how they work in C++. This is not a trivial subject, and what started out as a mere introduction ballooned into the article you see before you.

This article will cover object life cycles, inheritance, polymorphism, and composition. These are deep subjects in their own right, so I’ll only cover how C++ handles them. Even so, it is not an article for people who are completely new to object-oriented programming. It will require lot of thought, but in my opinion, it’s worth it. Aside from being a better C++ programmer, you will also gain a deeper understanding of how other languages operate “behind the scenes.”

Continue reading

Posted in c++, Programming | Leave a comment

Like Bigger Stones: Argument Passing in C++, Part 2

In my previous article, I talked about how C++ handles parameter passing when the arguments are primitive types or arrays. This article will talk about passing objects in C++. Objects are more complicated than primitives or arrays, so as you can imagine, passing them is also more complicated.

Before you read this article, you should understand the three basic C++ mechanisms for parameter passing: by value, by reference, or by pointer (or “simulated reference”). You should also understand how C++ hanldes automatic local variables. If you don’t understand this already, you should read my previous article on the subject. (If you don’t understand that, then I’m not a good writer, and you shouldn’t read this article anyway.)

Continue reading

Posted in c++, Programming | Leave a comment

Like Stones: Argument Passing in C++, Part 1

For people new to C++, argument passing can be very hard to understand. Most programming languages handle argument passing “behind the scenes” somehow. Either there is really one form of argument passing (e.g. C), or your choices are determined for you (e.g. Java). Not so with C++. Like almost everything else, C++ requires that you handle argument passing yourself.

So, for those people, I thought I’d write up a couple of posts about the various types of argument passing. But be aware: despite what you may think, this is not really an article for beginners. I wrote this mostly for people who already have some programming experience in another language, but are new to C++. It doesn’t require a lot of knowledge, and I’m not saying that beginners won’t get anything out of it. But if you are an absolute beginner, you may be overloaded with information.

With that out of the way, let’s talk about C++. The short version is that C++ has three mechanisms for argument passing: passing by value, passing by reference, or passing by pointer (or “simulated reference”). I’ll explain how each one works in turn.

Continue reading

Posted in c++, Programming | Tagged , | Leave a comment

An Evil Scheme

In the winter of 2013, I took a course at UMass Boston called CS 450: The Structure of Higher Level Languages. I had no choice; it is a required course for getting a CS degree. I hated, hated, hated that course. And I am hardly alone; of all my classmates that I talked to, not one liked it. The best one could say is that others hated it less than me.

The main reason for my hatred is because it is a Scheme course. I know that there are many programmers who love Scheme, but I have no idea why. I also know that most programmers have never written a single line of Scheme code in their entire lives. But quite a few of them will be forced to eventually; UMass is not the only university that has a Scheme requirement.

If you’re one of those programmers, then this article is for you. By reading this article, you will hopefully be a little more prepared for your inevitable pain. On the flip side, if you read this article and say, “Hey, that actually sounds kinda cool” – well, then, Scheme is the language for you. Good for you; you’ll save lots of money on therapy.

Continue reading

Posted in Programming, Scheme | Tagged , | Leave a comment

Efficient prime number testing

In nearly every lower-level CS class, students are invariably asked to create a function that can recognize a prime number. There are very good reasons for this. For one thing, the study of prime numbers is one of the longest-running fields in mathematics; the ancient Greeks were the first to study them in depth, but even the ancient Egyptians distinguished between prime numbers and composites. For another, the study of prime numbers has applications in a wide variety of fields. The one that will probably interest CS students the most is cryptography.

But the reason I’m going to talk about them is because recognizing prime numbers is a good starting point for talking about algorithms. By starting with the most basic, inefficient algorithm, we can learn some techniques to improve efficiency and eliminate redundancy. These same techniques can be applied to almost any algorithm, and will be useful (I hope) throughout our entire careers.
Continue reading

Posted in Programming | Tagged , , | Leave a comment

The Double Baggins

(This post originally appeared on my Digication site.)

As a (non-gradable) challenge for BHCC’s Data Structures course, Prof. Richmond challenged us to come up with a new sorting algorithm. This is the one I came up with.
Continue reading

Posted in Programming | Tagged , , | Leave a comment

Breaking cin for fun and profit

This post was originally written for students in Bunker Hill’s CIT 120 course.

Breaking the cin object for validation

As you should know, cin is an object. Specifically, it is an object of type istream (or, an instance of the istream class). For those who know anything about C (as opposed to C++), it is very roughly equivalent to stdin from the cstdio (stdio.h) library.

In C++, objects can overload operators. This means that whoever programmed the object, is able to determine the behavior of that operator when it works with that object. An example of this is the addition (+) operator and a string object; the operator is overloaded to concatenate strings (whereas by default, it simply adds numeric values).

In fact, this is how the stream extraction operator (>>) and stream insertion operator (<<) work. By default, in C++, they are not stream operators at all, but the “bitshift right” and “bitshift left” operators; they are overloaded when used with stream objects.

What does this have to do with “breaking” cin? Well, behind the scenes, the cin object has a series of flags. Flags are simple data types that can be either “1″ or “0″ – they are similar to the boolean type, but only take up one bit, rather than a whole byte. Flags usually represent the “status” of the object. The cin object has a bunch of flags; the ones that concern us here are badbit and failbit. If either of those bits are true, the cin object is considered “broken.” Any attempt to use the cin object’s methods (such as cin.ignore() or cin.getline()) or the stream extraction operator (>>) won’t work at all.

Continue reading

Posted in c++, Programming | Tagged , | Leave a comment