Section 9.1
C++ Programming Fundamentals

WHEN I DECIDED TO TEACH COMPUTER SCIENCE 124 in Java rather than C++, it was with the knowledge that students would probably be moving on to C++ eventually. This is because C++ is still the major programming language for professional programmers. In addition, it has many language features that Java lacks and that computer science students should be familiar with. However, it was my feeling that Java would be a superior language for an introductory course, and that in any case much of what could be covered in a first course in programming would be the same for both languages. So, I hope that a transition to C++ will not be too difficult.

But before discussing C++, let me mention some of the things that it does not have. First of all, there is no platform-independent GUI support in C++. A typical first course in C++ might not even mention windows, graphics, or events. In addition, standard C++ does not have built-in support for threads or asynchronous programming. Nor does it have a standard interface for network programming. Of course, classes can be written to provide these features in C++. But since they are not standardized, such classes tend to be platform-dependent so that what you learn about one type of computer doesn't automatically apply to other computers. They also tend to be more complicated than the versions available in Java. By using Java, I was able to include these important features of modern computing environments in a natural way.

But enough propaganda about Java. The rest of this chapter is a very brief overview of C++.

Program Structure

The first thing to know about C++ is that you can write entire, complicated programs without ever using classes or objects. Whereas in Java everything, even a program's main() routine, must be contained inside classes, C++ allows free-standing subroutine and variable declarations. The main() routine, in particular, cannot be a member of a class.

C++ variables and subroutines that are defined outside classes are similar to static variables and subroutines in Java. That is, they exist for the whole time that the program is running. In C++, such variables and routines are said to be global or to have global scope. They can be use "globally" inside any subroutine and even in methods that belong to classes.

However, no variable, class, or subroutine can be used in a C++ program unless it has been declared earlier in the same file. (This is different from Java, which allows you to used a variable or subroutine that is not declared until later in the program; of course, Java even allows you to use classes that are defined in other files, and it will go off and search for them when necessary.) This might seem to require that the entire program be in one big file, with the main() routine at the end, but that is not the case. To avoid this, C++ uses header files. A header file contains declarations of variables, subroutines, and classes that are actually defined in other files. C++ distinguishes carefully between declarations and definitions. The declaration of a subroutine, for example, gives only its name, its return type, and its parameter list. The definition of a subroutine also includes the code that defines the routine. A header file would contain only the declaration, which gives enough information to the compiler for it to check whether the subroutine is being used legally.

There are many standard header files that declare useful subroutines and classes. For example, the header file math.h defines mathematical functions such as sin(x) and sqrt(x). (By convention, the name of a header ends with ".h".) Another standard header file, iostream.h, defines stream classes that can be used for doing input and output. To get access to the declarations in a header file, a program must include the header file. This is done with a #include statement at the beginning of the program. For example, a program that starts with the statements

              #include "math.h"
              #include "iostream.h"

will be able to use the mathematical functions and the stream classes that are defined in the header files. The #include statement is very similar to the import statement in Java. In particular, saying #include "iostream.h" in C++ is similar to saying import* in Java. (But in C++, you'll also have to include ifstream.h, if you want to do input from files, and ofstream.h, if you want to do file output.) It's often a chore just determining which header files your program needs to use.

When you write a large program in C++ and you want to break it up into several pieces, you will generally have to write two files for each piece: one file that actually defines some subroutines and classes and a header file that declares the same subroutines and classes. Other files that need access to those subroutines and classes can then #include the header file to get access to them.

Types and Variables

C++ has standard types named short, int, long, float, double, and char that are similar to Java's primitive types with the same name. In C++, however, the exact meaning of these types is not completely standardized. I can't tell you the range of legal values for ints, for example, because the legal range is different on different systems. There is no standard byte data type, but char really plays the same role, since a char in C++ is considered to be simply an 8-bit integer.

C++ does not really have a standard String type that is equivalent to the String type in Java. C++ does have some support for strings, but they are more difficult to use and they require a knowledge of pointers and arrays. I'll discuss them in the next section. It is possible to write a String class in C++ that is very similar to Java's class, and most C++ programmers would probably have access to such a class by #including a String.h header file. However, you can't depend on this since it's not a standard part of the language.

There is no standard boolean type in C++. Instead, integers are used to represent boolean values. The value false is represented by zero, while any non-zero integer is interpreted as representing the value true. Thus, the value of a boolean expression such as "(x < 0) && (y > 0)" is really an integer, and an integer-valued expression can be used as the condition in an if statement or while loop.

I should note that there is a lot more to say about types in C++, since C++ has several different ways to define new types. Like Java, C++ has class types and array types. But it also has things called pointer types, structs, enumerated types, and function types. The later three types of types, I won't even mention again.

Variables are declared and initialized in C++ in much the same way as in Java. C++ has global variables (declared outside subroutines and classes), instance variables (both static and non-static), and local variables (declared inside subroutines or methods). Local variables can be declared at any point in a routine, and are valid only for the block in which they are declared. One big difference is that in C++, a variable can actually contain an object, whereas in Java, a variable can only contain a reference to an object. I'll discuss this further in Section 3.

Control Structures, Assignment Statements, and Expressions

Fortunately, there is very little to say about control structures in C++. Except for a few minor details that you will probably never encounter in practice, they are the same as in Java. That is, C++ has if statements, for statements, while loops, do loops, and switch statements that work the same way as their counterparts in Java. Also, you can use break statements and continue statements in C++, just as in Java.

The same goes for assignment statements and expressions. All the operators are the same: +, -, *, /, %, ++, &&, ||, =, +=, and so on. One difference, already noted above, is that the mathematical functions are not contained in a Math class. Thus, instead of saying y = Math.sqrt(x), you would say simply y = sqrt(x) (On the other hand, if you want to use the sqrt() function in C++, you have to #include the header file math.h at the beginning of your program. Whereas the Math class is automatically imported into every Java program, the math.h header file is not automatically included in a C++ program.)

Input and Output

Like Java, C++ has a standard input stream and a standard output stream for reading and writing user input. The C++ versions are actually much more well-developed than the Java versions, but the syntax for using them is a bit strange. The standard input stream is called cin, and the standard output stream is called cout. (The "c" in these names stands for "console.") To use these streams in a program, you must #include the header file iostream.h.

The syntax for reading a value from the standard input stream into a variable named x is:

            cin >> x;

(The double-arrow operation is meant to indicate the direction of flow of information.) Output is similar, except that the direction of the arrows is reversed. For example,

           cout << x;

will output the value of the variable x to the console, and

          cout << "Hello World!\n";

will output the message "Hello World!". The new line character, '\n', outputs a carriage return at the end of the message. You can string several output items into a single statement, like this:

          cout << "The square root of " << x << " is " << sqrt(x);

You can do the same thing with cin.

The standard I/O streams are instances of general stream classes called ostream and istream, which are analogous to Java's PrintStream and to my AsciiInputStream, respectively.


Subroutine definitions look pretty much the same in C++ as in Java, except of course that they are not necessarily found inside classes. Furthermore, many of Java's subroutine modifiers, such as private and static are either not used or have a different meaning in C++. For the most part, subroutines in C++ are declared without such modifiers.

Subroutine call statements and function invocations are also just about identical in Java and in C++.

Every program must have exactly one main() subroutine, and just as in Java, the system runs the program by calling its main() routine. The exact form of the main routine is not entirely standardized, and often several forms are acceptable. In some cases, for example, the main routine has return type void, while in other cases, it has return type int. (The int that is returned by the main routine is sent back to the system -- which called that routine -- as a status code to indicate whether the program completed successfully or ended in error.) On a given system, either or both of these formats might be acceptable.

Exception Handling

Although exception handling is not really a " programming fundamental," I mention it in this section because it is another area in which Java and C++ are very similar. C++ uses try, catch, and throw in the same way as C++. However, C++ does not allow a finally clause in a try statement, and it does not have the large number of predefined exception classes that Java has. Since exception handling is a recent addition to C++, it is still not completely well-integrated into the language. Many of the standard subroutines, for example, were written before exception handing was introduced, and they use older, alternative ways to deal with errors.

[ Next Section | Previous Chapter | Chapter Index | Main Index ]