Archive for the ‘C++’ Category

New Programming Note

Tuesday, September 4th, 2007

I just added a new programming note about a project I have been tinkering with lately. The project is a Buddhabrot image generator. Right now I am still working on generating some images so I will add those tomorrow, but tonight I wrote up a page about some of the interesting things I learned from this little project. So, check it out. Hopefully you will find it interesting too.

Functional Dependencies

Wednesday, March 14th, 2007

I wrote a fun little program for computing the closure of a set of functional dependencies this week. Functional dependency theory is the foundation for Third Normal Form and Boyce-Codd Normal Form, and a variety of other neat things you can do with relation schemas. An interesting and powerful set of ideas, but I think that most database designers don’t really use functional dependency theory so much. But it is fun to play with!

Anyway, a lot of the problems are very simple in that they work with abstracted schemas, such as R(A, B, C, D, E), and if you take a set F of functional dependencies against the relation R, you would like to compute things like “What is the closure of F?” or “What is a canonical cover of F?” Then you can answer questions like “What are the candidate keys of R, given F?”

(more…)

Stupid Reference Tricks, Part 2

Friday, January 19th, 2007

The last post I wrote about C++ was about an unfortunately too-common programming practice of writing a class so that it provides a direct reference to an internal data-member. Here is the offending code, yet again:

class Widget {
private:
  double m_distance;

public:
  double & distance() { return m_distance; }
  const double & distance() const { return m_distance; }
};

I showed this kind of approach in my Advanced C++ track last night, and here is something that one of my students thought of: Can you grab a reference to Widget’s m_distance data-member, and hang on to it?

In other words, what does this code print:

  Widget w;
  w.distance() = 5;

  double &evil = w.distance();
  evil = -2;

  cout << w.distance();

(more…)

Stupid Reference Tricks

Monday, October 23rd, 2006

Today’s interesting C++ question comes from one of my students:

class Widget {
private:
  double m_distance;
  ...
};

Widget w;
w.distance() = 62;

Question 1: How does this work?
Question 2: Is this even a good idea?

(more…)

Details, Details.

Tuesday, July 11th, 2006

C++ templates are tricky.

In C++ you can declare typedefs inside of classes or class templates, and then you can use them to make your life a little easier. Well, mainly it’s easier with classes; templates make you jump through some extra hoops.

(more…)

Improvements

Monday, January 23rd, 2006

Today I gave what will hopefully be one of the last CS11 lectures I have to create from scratch. It was for the Advanced C++ track of CS11, which is going through some minor changes.

The main issue was that I didn’t feel like it had a unified focus, or a contiguous flow from the Intro C++ track. My predecessor Ben probably had it all well in hand, but my first term teaching that track sure didn’t feel right!

So, I have been tweaking it around a bit. This week I created a brand new lecture, all about std::string and the C++ Stream-IO classes. I think it was a nice broad lecture, and probably won’t need major tweaking from now on. It also has the benefit of covering exactly what the homework assignment needs, which I think the students will appreciate, too.

As one point of interest, I explained the dangers of this code:

  char* getUserName() {
    string name;
    cout << "Enter username:  ";
    cin >> name;
    return name.c_str();
  }

People who are used to Java usually get snapped by this one!

Ugh, now if I could only force myself to grade homeworks! (Yes, this blog entry is me flicking…) Just gotta get some grading knocked out by 10pm so that I can watch TV guilt-free…