Since I am leaving academics, I have decided to open source all of the materials that I have created over my 3 1/2 years as a professor.
This contains material from Miami University (2005 – 2007) and the University of Cincinnati (2013 – 2014) and contains material from these courses:
- Computer Science 1
- Computer Science 2
- Data Structures
- Enterprise Application Architecture
- Large Scale Software Engineering
You can find it all on github.com.
Yet another reason I believe that C++ is the wrong language for teaching introductory programming. Because of the way classes are laid out in memory, it is trivial to show (a good thing), but it is also trivial to access those private member variables directly.
TLDR? – View the gist on github
Let’s break this down. We have a class called Point
The point class in this instance represents a 2D and has the private member variables of x and y.
If we have, say, a local variable (Point p;) we can do some basic calculations. This may be compiler / OS / architecture dependent, but this works in 32-bit linux w/ gcc on i386.
When we run this code, we see that the sizeof(point) is identical to the sizeof(int) + sizeof(int). Since classes in C++ are based on structs in C. This is just a tight packing of the member variables.
And if the base pointer for &p is 0xa43c then we see that the address of x is the same, and y is off by 4 bytes (the size of an int).
Now – if we just try to access the private member variable directly, i.e. p.x = 27; the compiler will detect that we’re trying to do something bad and give us an error (good!).
However, through the magic of memory addresses and pointers we can change that. It is a simple cast. First we get a pointer to our instance of the Point class, and then we re-interpret that as a pointer to an int. From there we can start derferencing and using pointer math.
That’s it. A simple cast and some pointer math and private doesn’t mean anything. Who needs encapsulation anyway?
It has been a little while since I’ve pulled out this article by the pragmatic programmers (Andy Hunt and Dave Thomas), Keep it DRY, Shy, and Tell the Other Guy.
I’m going to use this in the first week of teaching CS 1 students classes (in C++) for the first time. To me, the critical point is the “tell the other guy” part. The way they present it is the more useful of the OO metaphors. It isn’t calling a function on a object to achieve some goal. It is sending a message to some object, with apathy towards the details of how it is done.
I think this is the key to breaking from procedural programming and moving successfully towards object-oriented programming.
The students in my Computer Science 2 class have recently been coding a bowling scoring program. I decided to see what this looked like in Earlang. I spent over two years writing Earlang code (for work, yes I got paid), and really like the language. Now that I have this done, it makes the c++ look long winded and over-complicated with all the conditional checks.