Wed, 07 Jan 2009

C++ for Python Programmers: a Cheatsheet


A fellow prof here at MSU, Rich Enbody, whipped up the following cheat-sheet for new programmers transitioning from Python (CSE 231) to C++ (CSE 232). He welcomes comments. Here's the link:

http://web.cse.msu.edu/~cse231/python2Cpp.html

Paranthetically, he and his cohort in crime, Bill Punch, will be giving a talk about using Python as the intro CS programming language at PyCon '09. They have some interesting stats on the effects of a mixed Python-C++ curriculum vs a C++-C++ course base for the first year of programming.

And folks... remember, this is for intro programmers who don't know C++ yet!

--titus

posted at: 12:56 | path: /jan-09 | 2 comments

Tags: , ,


Comments:

Posted by Evan at Wed Jan 7 15:59:24 2009:
Pretty nice. I already know C++ pretty well but I'm going to share this with some of my friends who are Python-specific.

Posted by Fabien at Wed Jan 7 16:27:22 2009:
A few problems in that first draft, mostly due to the author's C background:


1/ In contrast to C, in C++, declaring variables at the beginning of the block is a bad habit at best, and quite often impossible.


C code:

int a, b;
a =3;
++a;
b= 4;

C++ equivalent:

int a= 3;
++a;
int b= 4;



2/ "File Input and Output": I'm not 100% sure the code won't work, but this one is far better and more canonical:

int main()
{
  string line; 

  ifstream InStream ("Data.txt");

  while (getline (InStream,line))
  {
  cout << line << endl;  // output the line
  }
}


3/ Arrays.

C-style arrays are only used if you need to put some hard-coded values. And they're not even proper arrays -- they're nearly pointers (which pretty much means, lots of headaches).

In C++, the canonical type for an array is vector<>. It's a real array, whose size can change whenever you wish, and that you can copy.

vector<int> a (10);
vector<int> b= a;
vector<int> c;
c= b;

Post a new comment:

Name:


E-mail:


URL:


Comment:


Note that comments must be manually approved; e-mail titus@idyll.org if your comment doesn't show up quickly.