CS 1124 — Object Oriented Programming

Python to C++

What are the biggest issues that hit you when switching to C++ from Python?

[Note, this is not meant as an introduction to C++. For that see: CS 1124 - Introduction. But for a Python programmer, this might help you to appreciate the differences between the languages that you need to pay attention to as you start learning.]

No Interpreter

In Python you are accustomed to being able to type a line into IDLE and have the code execute immediately. In C++, you will have to write a complete program, "build" it and only then can you run it.

Building involves a couple of steps, but generally the separate steps will be managed by your development environment. The steps are compilation and linking. We will discuss the difference between those two steps later in the semester when we start using "separate compilation". Frequently people use the term "compile" to refer to the entire build process and I will typically follow that style here, only making the distinction when it is important.

So, before you can run any code, you have to write a complete program and "compile" it (i.e. build it). The compiler will catch many kinds of errors, which is nice because it is a lot easier to figure out a problem that the compiler points out, than to run your program and only realize there is a problem because the output is wrong. For small programs this advantage isn't usually so important. That's why Python is a reasonable choice for a first programming language. As programs increase in size and complexity, the benefit of having the compiler catch various errors similarly increases. In a course on Object Oriented Programming, our focus is on developing the skills, techniques and style that will enable you to write large programs and have a reasonable hope that they will work.

Variables Have Types

This is likely the biggest issue that you will face. In Python you can just assign a value to a variable at any point in your program. In C++, on the other hand, before you can assign a value to a variable, you have to define the variable, telling C++ what type of value it can hold. An attempt to assign a value with the "wrong" type to a variable will generally result in a compilation error.

Similarly when you define a function, you have to specify the types of the parameters. By doing that, you make it possible for the compiler to catch the kind of error where you call a function and try to pass the wrong sort of argument.

This feature of the language prevents you from assigning a number to a variable at one point in your program and then later assigning a string to the same variable. Most programmers won't see this as a horrible constraint. In fact, good style in programming even in Python would never allow reusing a variable for a different purpose. This goes beyond worrying about using a variable as a number one place and a string in another. It includes the issue of whether we use a variable to hold someone's age, as an integer, in one part of the program or his height in inches, again as an int, in another part. Bad idea, but one that our language / compiler cannot recognize.

Indentation vs. Blocks

As you know, in Python indentation matters. For programmers coming from other languages, that is probably the biggest reason not to use Python. Right or wrong, it just seems backwards to many of us that the amount of whitespace (eg. blanks or tabs) we put at the beginning of a line will effect what the program means.

Since C++ (and many other languages) don't use whitespace to determine which lines are "inside" a loop, for example, the language has to provide another mechanism, typically some pair of markers to indicate the beginning and end of the block. C++ uses "curly braces": { }. The character '{' is used to begin a block, in the same way that the colon is used in Python. The character '}' is used to end one. Python doesn't need that because it can depend on the indentation level to tell when the block ends. (You will find the use of braces in a number of other languages, including C and Java.)

So long as we put our code in a pair of braces, C++ knows that it represents a single block, in the same way as Python knows that a sequence of statements with the same indentation represents a block. Of course, for human readability, it is important in C++ that we still use consistent indentation. And that is the argument in favor of the Python approach. If we are going to use indentation anyway, why not make it count and free us from having to use the braces also? Oh well, different languages, different styles.

Statements End in Semicolon

C++ also allows a statement to take multiple lines without having to write anything special. This also means that we have to tell C++ when a statement is finished. To do so, we have to put a semicolon, i.e. the character ';', at the end of every statement.

Console I/O

C++ does not have a print statement. Nor does it have a function raw_input. Instead, simple I/O to and from the console (i.e. to the screen and from the keyboard) uses what are commonly called the output operator and the input operator.

So, to print the contents of a variable x to the screen, you would type:

cout << x;

What does cout stand for? And how do people usually pronounce it? First, we say it as "sea-out". And it represents the screen, or more precisely standard output. When we get to doing file i/o, we can use other objects that represent files that we have opened, either for reading or writing. Notice that the angle brackets point from the thing we are printing towards the place we are want to write that thing. That piar of angle brackets, without any space between them, is commonly called the output operator in C++. (Later on, you will learn that the same characters serve a different purpose when you are doing bitwise operations. We probably won't get around to discussing that this semester.)

In order to read something into the variable x, we write:

cin >> x;

Here, cin represents the keyboard, or more generally standard input. Note that here the angle brackets, the input operator, point from the place we are getting the information, the stream, towards the place that we want to store that information, the variable.

String vs. Character Literals

Python uses both the single quote and the double quote characters to denote string literals. C++ only uses double quotes for string literals. It does use the single quote, but only to represent single characters. (Does Python have a way to represent a character literal as opposed to a string of one character?)

Variables Are Not Links

First, let me say that this is a subtlety and won't effect your early programs.

In Python, variables link to or point to things in memory. In C++, it is better to think of the variable as actually holding the value, not a pointer to it, i.e. a variable is the address in memory that holds the value.

What Else?

Ok, obviously there's a lot of other things that you will find that are different. However, what I am including here are those things that scream out to me as differences that effect students right away, and sometimes continue to confuse a student for a while. Keep these points in mind as you begin to learn C++ and hopefully the transition will go smoothly.

Let me start a list of things that maybe could have appeared above but I left out:

Anything else that you think I should have mentioned? Please let me know. It might help some future student.

Home


Maintained by John Sterling (jsterling@poly.edu). Last updated January 23, 2011