Using Coconuts - a Pythonic Blog

Username:

Password:


Don't have an account? Get one!

A First Impression of C++

http://blog.opensourcenerd.com/upload/pg-13

So NYU Poly, after taking into account my AP Computer Science score (5), placed me into CS 1124: Introduction to Object Oriented Programming in C++. Sadly, this means I skipped CS 1114, which was taught in Python...

http://blog.opensourcenerd.com/upload/sad-cat

But I want my Pythons...

On the plus (plus-plus?) side, this involves me diving into a poolful of sharks: C++. Now, this isn't any regular person's first encounter with this language (that's too terrible an image), but rather that of someone who already knows Java, C, and Python. With that in mind, to me, C++ looks like a Java-fied C which doesn't allow you to shoot your own foot off anymore just for fun. Now, since it's supposedly an improvement to C, here are some things I noticed that were different from C:

It knows strings. However, it's still just another external library -- you'd think such an essential feature would be in there by default...

#include <string>

int main(){
  string a = "Hello";
  string b = "World";

  string c = a + " " + b;    // c = "Hello World"

  bool d = ( a == "Hello" ); // d = True

  char* e = a.c_str();       // Convert back to a C string
                             // AKA a char array
}

Its I/O is... still awkwardly unintuitive, but a little better. Remember that printf() nonsense? Yeah, no. Now there's pipes to the input and output. Check this out.

#include <string>
#include <iostream>
#include <stdlib.h>

using namespace std;   // Eek! C++ has STDs!

int main(){

  cout << "Hello, World!\n";  // Pipe the string "Hello, World\n" to stdout.

  cout << "Hello, World!" << endl; // Same net output, endl for newline.

  cerr << "Oh, no! An error!" << endl;

  String s;
  cin >> s;   // Input a word from stdin into s

  int x;
  cin >> x;   // Input an int from stdin into x (wow overload!)
}

All this made possible by ye olde operator overloading: being able to configure what an operator does for a certain class. For example, instead of having a special multiply() method to a Person class, you can just have person * person return a Person object for the resulting baby.

http://blog.opensourcenerd.com/upload/xkcd-people-multiply

The different ways (person * person) can compile. xkcd.com

Similarly, streams apparently have operator overloading for the bitshift operators, to allow them to do the magic above. I'm not sure if that's a new C++ feature, or if C had it too, but to me, that's pretty slick.

Variable definitions. Ugh, back to this stuff. For a Python developer, it's like chains on my feet. And then there's the lack of duck typing and easy reference changes! In my homework, I wanted to do something like this:

struct Warrior{ string name; int strength; };

// ...
  String warrior;
  cin >> warrior;

  warrior = getWarrior(warrior); // Ack! Result is not a string!

It made me sad...

And, last but not least, the mess of pointers is transferred from C, with references and objects added in just for kicks. What ticked me off most is that assigning a value to a new variable doesn't make a reference from the variable to the value (object or otherwise). It took me a while to debug this:

// ...
  Warrior w = warriors[0];
  w.strength = 0;

  cout << w.strength << endl;
  cout << warriors[0].strength << endl;

// Output:
// 0
// 10

Apparently, that created a copy of warriors[0] in the memory allocated to w. In retrospect, and keeping in mind how C/C++ do their memory allocation, this makes sense, but coming from Python and Java, it didn't. Here are two solutions to my dilemma, the former of which I actually used:

// ...
  Warrior* wPointer = &(warriors[0]);
  wPointer -> strength = 0;

  Warrior& wReference = warriors[0];
  wReference.strength = 0;

... and that's only because I didn't know before hand what the heck a reference was, or that C++ treats it specially.

One last note of difference from C: structs no longer need the horribly inconvenient typedef syntax. The struct prefix is obsolete, and defining a struct automatically defines it as a type:

// C

struct Warrior { char* name; int strength; };
struct Warrior w;
// or
typedef struct Warrior_struct { char* name; int strength; } Warrior;
Warrior w;

// C++

struct Warrior { string name; int strength; };
Warrior w;

Anyway, that's enough of my shenanigans. Now this piece of homework is done, I have two weeks of classes to teach myself how to write a classes... with class. And classy puns.

wooo! new post!

on 2009-09-11 02:35:40
craig just ltg says... source permalink

nice. at least you're learning something. mason's equivalent class is in C. so nothing new =(. Also, the xkcd pic is broken.

on 2009-09-11 03:54:36

How is it broken? To me it looks just fine. XKCD is having some server problems, it seems. Perhaps I should just host it on my blog.

on 2009-09-11 03:55:48

Fixed by uploading the picture itself to my server. No more depending on XKCD to be up, I guess...

on 2009-09-11 04:24:23
cheshire13 says... source permalink

The kitty looks so sad! What have you done to it?!

mmm....explicit pictures

on 2009-09-11 23:50:38

There, I added a PG-13 warning.

on 2009-09-12 03:10:00
jelkner says... source permalink

Look dude, you already know way more Python than you would've learned in that intro class. Face it, you didn't need it!

on 2009-09-12 13:36:57
thatone says... source permalink

C++ may seem horrible. But it still has it uses...

on 2009-09-12 18:25:31

Well, I definitely didn't need it, but it would have been an easy class. Not that this isn't... I'm already done with the homework for the week after the next, and in class I learned dealing with streams not too bad... Especially considering I didn't have the Internet at my disposal

on 2009-09-12 21:58:07
New Comment
You're not logged in! Log in to be awesome!
Format: BBCode ReStructured Text

Author (max. 20 characters):