Appendix B

Differences between Karel++ and C++


The robot programming was purposely made very similar to C++ since you might want to use Karel to help learn C++. There are some differences, however.

task versus main

In Karel++ we name the main task block task. In C++ it is a function named main. This function is not a part of any class.

Visibility of Class features

In Karel++ local robots are assumed to be private to that robot and new instructions and predicates are assumed to be public. C++ permits much more control over what is public and what is not.

Robot Initializations

When a robot is local to a class we have defined its initialization in the same way that we define initialization in the main task block. For example:

Mile_Walker Lisa(3, 2, East, 0);

In C++ such robots are initialized in special functions called constructors instead.

Structured statements: if, while, and loop

We required all such statements to include compound statements enclosed in { and }. C++ has no such instruction. It is possible in C++ to write

if (nextToABeeper())

where we require

if (nextToABeeper())
{	move();

This leads to a problem in C++ called the "dangling else problem" which will be discussed in any good C++ book.


In C++, our Boolean is spelled bool. Older C++ systems use int.

In C++ our loop instruction is replace by a much richer instruction called for.

In C++, what we have called aliases are called pointers. Pointers have many additional capabilities.

In C++, what we have called myself is called this.

We have assumed that all Karel++ instructions and predicates are what C++ refers to as virtual. This is what enables polymorphism. C++ has additional options in this regard.

C++ would permit a class to inherit from several classes called base classes, and all classes in C++ need not derive from a common point like ur_Robot.