C++ for Python ProgrammersEnbody & Punch |
This document is a brief introduction to C++ for Python programmers -- something to help start a transition to C++. It is not meant to be a complete introduction to C++. Brevity took precedence over details.
We'd like to make this document a useful reference for a wide audience. Please send suggestions or additions to enbody@cse.msu.edu. We welcome coauthors of significant expansions.
This document is a supplement to our CS1 Python text which is currently in its first draft.
Why should Python programmers care about C++?
C++ and its ancestor language C allow one to operate closer to the operating system and
hardware. That closeness can result in better performance, but it is not guaranteed.
Also, C++ is a common and powerful language so it is worth learning.
Look "under the hood" of many applications and operating systems, and you find C++ or C.
One thing to keep in mind when learning C++ is to keep an eye out for the STL: the C++ Standard Template Library. The STL is frequently overlooked in textbooks, but it is a powerful set of tools and will be your friend.
There are other languages worth learning, but this page is devoted solely to the transition from Python to C++. Note that C++ preceeded Java and C# so one finds many similarities (as well as significant differences). Once one learns C++ it is relatively easy to transition to the other two.
Finally, language choice need not be an "either/or" situation. You can code in Python and then incorporate C++ where you need speed or low-level access to system components. It is relatively easy to wrap C++ libraries for calling from Python (many useful ones are already wrapped for you). Python plays well with other languages. Tools such as Bgen, PySTL, and Sip help.
Here at Michigan State University our first course for Computer Science and Computer Engineering majors is in Python, and the second course uses C++ in a Linux environment.
The tradition in C programming is to write one's first program to simply print "Hello World!" That is how the first C programming book The C Programming Language by Kernighan and Ritchie began. Since C++ is derived from C, we'll start the traditional way.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
}
The first thing a Python programmer notices is significant overhead to simply print a string. The #include
works like import in Python (not really, but the analogy works for now) so in this case
the input/output stream library is brought in. In this case, that is where cout is defined.
The using namespace specifies the namespace so we can use cout instead of
std::cout.
The int main() begins the declaration of the main function which in C++ indicates
the first function to executed, i.e. "begin here." The curly braces { and }
delimit the beginning and end of a block of code, in this case the beginning and end of the function
main.
The statement cout << "Hello World!"; directs the string to cout, the standard
output stream, which displays it. Indentation is optional in C++, but code will be unreadable, if you
do not indent. If you indent like you were forced to do in Python, your code will be readable.
Gotcha #1: Statements in C++ end with a semicolon ";" (that isn't precisely correct, but
will do for now).
C++ is a compiled language so you must compile it before executing it. Compilation takes the C++ code and translates it into machine language that a processor understands. Compiling and executing code is system dependent. For simplicity, I will describe how to do it in Linux.
The program must be written using some editor such as VI or EMACS on Linux. A C++ program file
extension is .cpp so save the program as hello.cpp.
The GNU compiler, g++, is widely available so compiling is simply the line:
g++ hello.cpp
If compilation is successful, the executable code will by default be written to a file named
a.out which can be executed by simply typing a.out at the
command prompt. If the compiler finds syntactical errors in the program, error messages
will be printed.
To download the recommended SSH client (free or licensed through the CSE department) for connecting remotely to the Linux server, click here.
If none of this makes sense, don't worry. Your first laboratory exercise in CSE232 will be a Linux tutorial.
On a Windows computer, the Visual C++ development envrionment is an excellent, but sophiscated, way to create and run programs (named VisualStudio). You compile and edit within the same application. Compilation is done by "building" the program. Running is simply a command in the application.
C++ is a strongly typed language -- not precisely stongly typed, but close enough for now. That is, all objects (things you name) must be declared before use. Python figures out types based on the objects you assign names to, but C++ wants to know up front. Python allows names to refer to different types of objects while the program is running; in C++ the type remains fixed.
For example, the type of a variable must be stated before use.
int x;
x = 2;
Gotcha #2: Forgetting to declare a variable before use.
(Don't worry the compiler will yell at you, if you forget.)
Note how each statement ends with a semicolon (Gotcha #1).
Some C++ types are familiar:
Some are unfamiliar:
Some differences:
int doesn't have extended precision. An int is limited by machine word size (usually 32 bits, but 64-bit use is growing); roughly, a max of 2 billion.int: short, long, unsigned, unsigned long, etc., but you can go far with only int.float, double is often used.
It is still a floating point number but has greater precision in calculations. Assignment is familiar:
x = 4;
but, multi-assignment is not allowed:
x,y = y,x
In C++, variables are memory locations with names. Declaring a variable puts a name to that memory location and associates a type with that memory location.
int x, y;
x = 7;
y = x;
In Python the expression y = x gives two names (x and y) to the same object, but in C++ that
same expression copies the value of x into the location named by y.
This difference can be illustrated as follows. In this example, we can see how two names for the same object allow changing the value using one name to be observed when displaying the other.
In Python we can name a list A and then also name it B. The 'is' operator shows that A and B name the same object. When you modify B by changing the first element in the list, that change is reflected when you display A:
>>> A = [2,3]
>>> B = A
>>> B is A
True
>>> B[0]=100
>>> A
[100, 3]
Illustrating something similar in C++ takes a bit more. In this case, we will use the C++ array which is has some similarity to a Python list except that an array's length is fixed -- it cannot change. In C++ it is not possible to assign one array to another. Instead, you have to loop through the arrays assigning one element at a time. The same is true for output.
int main()
{
int A[2] = {2,3}; // declare array A of length 2 and assign its elements to have values 2 and 3
int B[2]; // declare array B of length 2, but don't assign its elements (yet)
for (int i = 0; i < 2; i++) // loop through both arrays A and B assigning to each element
B[i] = A[i]; // of B the corresponding value of A
B[0]= 100; // change the first element of B
for (int i = 0; i < 2; i++) // loop through A and B printing out each element
cout << "A,B: " << A[i] << ", " << B[i]; // output would look like: A,B: 2,100 A,B: 3,3
}
In the case of C++, the values of A are not changed when B is changed.
Arithmetic expressions in C++ will be familiar:
However, division in C++ is indicated by "/" only, no"//". The operator performs "true division" on any floating point types and "floor division" on any integer types. "/=" follows the same rules.
Increment is new:
Decrement is similar: --
but
Gotcha #3:In C++ this expression does not work like Python or mathematics:
Assume that x = 3, what is the value of the expression: 5 < x < 10?
In C++ the subexpression 5 < x is evaluated first to be false with value "0". The "0"
replaces the subexpression so next 0 < 10 is evaluated to be true. So the value of the
whole expression 5 < x < 10 when x = 3 is evaluated to be TRUE!
To get the correct answer in C++ the expression must be written as:
(5 < x) && (x < 10)
For example:
if (x > 4)
y = 7;
Like Python, C++ allows an "else" clause:
if (x <= 5)
y = 8;
else
z = 10;
However, C++ does not have the "elif" keyword. Instead, you must use "else if":
if (y == 7)
x = 2;
else if (x != 2)
y = 5;
else
z = 10;
Python uses indentation to indicate suites.
C++ uses curly braces, { and } to delimit blocks.
Indentation is strongly recommended.
For example:
if ((x <= 5) && (y != 7))
{
z = 12;
w = 13;
}
However, if the block only includes one line of code the curly braces are not required. (See the if examples above.)
Comments and their usage will be familiar, but the delimiters are different:
// this is a comment at the end of a line
/* this is
a multi-line comment */
C++ has no notion of a docstring.
The C++ while loop will be familiar, and shares syntax rules with the C++ "if" statement:
For example:
while (x < 5)
{
y = z + x;
x = x + 1;
}
The C++ while also has the familar:
but there is no "else" for a C++ while statement.
The "for" in C++ is quite different than in Python. There is good news and bad news.
The bad news is that Python's powerful "for ... in ..." loop does not have an equivalent in C++.
However, the good news is that the C++ "for" loop is similar to Python's "for x in range(...)" loop.
For example, in Python:
for x in range(5):
print(x)
the equivalent in C++ is:
for (int x = 0; x < 5; x++)
cout << x;
The C++ "for" header has three parts:
for (int x = 0; x < 5, x++)
Note that if the conditional expression is false at the beginning, the loop body will not be executed at all.
In C++, "for" is a special case of "while." Understanding that relationship is important for understanding how the loops work. Note that this equivalence is not true in Python.
To illustrate with an example:
for (int x = 0; x < 5, x++)
cout << x;
is equivalent to
int x = 0;
while (x < 5)
{
cout << x;
x++;
}
Like "while", the "for" in C++ has
but also no "else" in C++ "for" loop.
Output in C++ looks familiar, but with differences. In particular, Python uses syntax that is similar to C output expressions which are allowable in C++.
For example, in Python 3 we can use:
print( "int {} and float {:5.3f} ".format( 45, 3.1416 ))
In earlier versions of Python print formatting was based off C and looked like:
print "int %d and float %5.3f" % (45, 3.1416)
In C (which is allowed in C++) the output looks similar:
printf("int %d and float %5.3f \n", 45, 3.1416);
Note the "\n" -- an explicit carriage return which is required in C to get the same output in this example.
However, the C++ standard is quite a bit different than the allowable C syntax.
First, C++ needs #include <iomanip> at the top of the program
(along with #include <iostream>).
cout<< setprecision(4); // significant digits is 4
cout<< "int "<< 45 <<" and float "<< setw(5)<< 3.1416;
cout << endl; // explicit carriage return
Here we see a huge difference in necessary syntax. In particular, Python's shortcut for file input is considerably shorter than the C++ syntax.
For example, in Python we could simply do:
for line in file("Data.txt"): print(line)
Alternatively, in Python we could have explictly opened the file first -- a sequence which is more similar to what is done in C++:
inStream = open("Data.txt", "r")
for line in inStream:
print(line)
In C++, we must include the file stream libraries, declare objects, explicitly open the file, and explictly check for when we reach the end of the file:
#include <iostream>
#include <fstream> // file streams
#include <string>
using namespace std;
int main()
{
ifstream InStream; // declare the input stream
string line; // a string
InStream.open("Data.txt"); // open file
getline(InStream,line); // read a line
while (!InStream.eof()) // check if not at end-of-file (eof)
{
cout << line << endl; // output the line
getline(InStream,line); // read next line
}
}
Gotcha #4: Note how the "while" loop functions: we get some input (using getline) before we check for the end-of-file at the top of the loop and then we get the next input at the bottom of the loop. A common error with "while" loops is to put the "getline" at the beginning of the block in the "while" loop resulting in one too many iterations. (This gotcha also applies to Python, but in Python you likely used "while" less frequently than you will in C++.)
Functions have similarities, but also have some very significant differences. You will need to be careful!!
Let's begin with what is similar.
In Python:
def fun(x):
return x + 2
In C++, it looks quite similar. Most notable is the need to declare the type of the parameter x.
int fun(int x)
{
return x + 2;
}
In both C++ and Python functions return exactly one object. However, in Python you have built-in objects that contain multiple values such as tuples. While it is possible in C++ to build similar objects, the tendency is to use parameters to "return" multiple values -- similar to how mutable parameters can be changed in Python.
Parameters: value vs. reference
The default parameter passing in C++ is "pass by value" -- similar to Python passing of immutable objects.
That is, changing the associated object in the function does not change the original.
Stated another way: changing the parameter does not change the argument.
Pass by value
Let's examine a C++ pass-by-value example. The output can be seen in the comments. In particular, note that
changing the x in the function did not change the x in the main:
int fun(int x)
{
x = x + 2;
cout << x; // prints 5
}
int main()
{
int x = 3;
fun(x);
cout << x; // prints 3, i.e. unchanged as expected
}
Pass by reference
In C++ pass-by-reference is flagged by appending an ampersand, &, to each parameter name you want passed by reference.
In this example, the x in the function refers to the same object as the x in main so changing the value of x in the function changes the value of x in main (because they refer to the same object). It is worth noting that if the parameter in the function was named y instead of x, the behavior of the example would be unchanged.
For example, notice how in this example the value of x in main is changed by the function.
int fun(int& x)
{
x = x + 2;
cout << x; // prints 5
}
int main()
{
int x = 3;
fun(x);
cout << x; // prints 5, DIFFERENT than above!
}
Classes have similarities, but are quite different. Classes in C++
usually involve two files, an interface fale and an implementation file. The
first includes the declarations of all the members of the class. Classes have
public and private members like Pyhton. They also have "friends" and
"associates" but those are not important right now. The members are declared
under the headings "private:" and "public:". Unlike Python the private members
are truly private and cannot be accessed except by other class members. An
example of an interface file is:
#indef SQUARE_
#define SQUARE_
Using namespace std;
class Square
{
private:
double length, width;
public:
Square(double Length = 0.0, double Width = 0.0); // Constructor
~Square(); // Destructor
double area();
}
The C++ equivalent of "self' is "this" however it does
not have to be included as a function parameter.
The implementation file includes all the function
definitions, each must include the return type, class name,
function name, and any function parameters. So the funcion
"area()" above would be defined as:
double Square::area()
{
return length * width; // Can access private data members
}
Functions that do not return anything have the return type "void"
such as:
void Sequence::display() // Prints class object but returns nothing
It is possible to override operators. The constructor above is the equivalent of
__init__ in Python. The destructor deletes memory after you are finished with it. There
are defaults for these funcions or you can write your own if the defaults are not
sufficient. Copy constructors and assignment operations can also be overridden. Other
operators include mathematic operations like:
Addition---Class Class::operator+( Class& Item ) // Python's __add__
Subtration-Class Class::operator-( Class& Item ) // Python's __sub__
// Note these operations return type "Class"
Boolean operations which compare class objects:
Equal--------bool Class::operator==(const Class& Item1, const Class& Item2)
Greater than-bool Class::operator>(const Class& Item1, const Class& Item2)
// Note the parameters are all constant so the items cannot be changed while
// you are comparing them
And input and output operations which allow you to choose how input is read
and output is formatted:
Input-----istream& Class::operator>>( istream& In, const Class& Item )
Output----ostream& Class::operator<<( ostream& Out, const Class& Item )
Pointers will be new to you so we'll briefly mention them -- they require considerably more space than we can devote to the topic here. In many respects pointers are simple, but their simplicity allows one to get into trouble. A pointer is simply a variable that holds a memory address. In a variable declaration an asterisk, *, indicates that a variable is a pointer, and the type indicates the type of the object found at the address in the pointer. An ampersand, &, indicates the address of an object. For example:
int A, *C = &A;
The pointer C (indicated by the *) holds the address (&) of integer A. A simple example can be found here. A longer explanation of pointers in general including references to C and C++ can be found here in Wikipedia.
Try/except are quite similar. A "try' block of code is written followed by "catch" blocks. Built in exceptions include: bac_alloc, bad_cast, bad_exception, bad_typeid, and ios_base::failure. It is also possible to throw your own exceptions. However, using exceptions in C++ is more complicated than Python and for now you won't need it.