This document contains a summary of most of the C++ features you are expected to learn quickly, given that you are familiar with Java. It may be that you are already reasonably comfortable with C++, but even if this is the case there are likely some things you have forgotten or never knew, so it might be dangerous to be too complacent. For brevity, the material here is mostly descriptive, but you should also study the additionally available separate documents containing many examples of C++ code illustrating the topics and ideas described here, including proper formatting. If something is mentioned here that you don't understand, and for which you cannot find an illustrative example in the supplied sample code, ask your instructor about it.

For the sake of completeness, all current C++ standard libraries are listed here, as well as (nearly) all C++ reserved words, but you are not expected to be familiar with the complete list of either.

Notes on C++ and its direct antecedent, the C programming language

  1. C++ was "invented" by Bjarne Stroustrup at Bell Laboratories in the early 1980s, and he remains very much involved in its development. C++ is a superset of C, which in theory means that any valid C program will compile as a C++ program, with some provisos. (You could not use a C++ reserved word in a valid C program and expect to have it compile as a C++ program, for example.) In practice, of course the ideal espoused by the theory may not always be achieved.
  2. The current C++ Standard was agreed upon in late 1998. Standard C++ consists of a core language and an extensive set of libraries, which may be classified into the following three groups:
    1. The Standard C Library, which contains the following 18 header files:
      cassert   cctype   cerrno   cfloat   ciso646   climits
      clocale   cmath    csetjmp  csignal  cstdarg   cstddef
      cstdio    cstdlib  cstring  ctime    cwchar    cwctype
      
    2. The "traditional" new C++ libraries, which contain the following 20 header files:
      // 10 header files related to "streams"
      fstream    iomanip   ios       iosfwd      iostream
      istream    ostream   sstream   streambuf   strstream
      
      // 4 "language support" header files
      exception  new   stdexcept   typeinfo
      
      // 6 "odds and ends" header files
      bitset   complex   limits   locale   string   valarray
      
    3. The STL (Standard Template Library), which is the most recent addition, and which contains the following 13 header files:
      algorithm  deque    functional  iterator  list     map    memory
      numeric    queue    set         stack     utility  vector
      
    Note that in the new Standard the .h file extension is no longer used to specify the name of a header file in a #include compiler directive, not even for the legacy header files from the Standard C Library. You should try to avoid mixing use of the old-style and new-style header files. Where possible, use the new-style header files, and since the contents of these header files are in the "standard namespace", you must insert, following your #include directives, the statement:
    using namespace std;
    
  3. Many versions of C++ exist: Visual C++, gnu C++, DEC C++, Borland C++, and so on, and many of these versions add capabilities to Standard C++, like the ability to do Windows programming (Microsoft Windows, X-Windows, or other Windows systems).

Edit | Compile | Link | Run | Test | Debug | Edit ...

You are expected to be thoroughly familiar with this "cycle". The actual steps involved in this process depend, of course, on the nature of your programming environment, and one or more of the steps might even be "hidden". You are expected to be reasonably familiar with how it goes in the Microsoft Visual C++ world.

Remember, as an aside, that the compile step also involves a preliminary preprocessor step that is generally "invisible" to the programmer.

Comments

There are two kinds of comments in C++, and you should have some idea of when to use each kind:

// C++ comments like this for single-line or end-of-line remarks.

/*
C-style comments like this are more suitable for comments that extend
over multiple lines, or that must be inserted in the middle of code.
Remember that C-style comments cannot be nested.
*/

Simple Data Types, Values, and Variable Declarations/Definitions/Initializations

The most commonly used of these simple data types are shown in a monospace, bold font like this. Remember that the number of bytes occupied by a value of each of these data types in the computer's memory is a system-dependent feature of each C++ implementation.

  1. char
    Example values: 'A', 'B', 'c', 'd', '?', '/', '\n', '\t', ...
  2. short, int, long
    Any type on the preceding line may be preceded by the keyword unsigned.
    Example values: ... –3, -2, -1, 0, 1, 2, 3 ...
  3. float, double, long double
    Example values: 23.789, -2.1E+3, 4e-1
  4. bool
    The only two values of the bool data type are true and false.
  5. enum
    A programmer-defined simple type whose values are programmer-chosen.
    Example: enum ColorType {RED, WHITE, BLUE};

Some notes on simple data types:

  1. The data types char, short, int and long are all referred to as integral types in C++. The only odd thing about this is that char is usually (in other programming languages) regarded as a type separate and apart from any integral type. This close association in C++ can be either convenient or troublesome, depending on how careful you are (when converting between types, for example).
  2. You must understand the notion of conversion of a value of one data type to a value of another data type and the difference between implicit conversion (coercion) and explicit conversion (casting).
  3. You must understand the distinctions between a variable declaration, a variable definition, and a variable initialization.
  4. You must know how to define a named constant and know why you must use such constants whenever appropriate.

Structured Data Types, Values and Variable Declarations/Definitions/Initializations

You must also know how to declare, define, and/or initialize, variables of the following structured data types:

Arrays

Strings

For both kinds of strings, and the corresponding string variables, you must know how they are input and output, how to find their length, how to concatenate two of them, how to copy (or assign) one to another, and how to compare two of them. In the case of C++ strings there are many additional useful operations, and you should know some of them as well.

Structs

There are two schools of thought on the use of structs. One school thinks they shouldn't be used at all any more, and that classes should always be used instead. Another school thinks that a struct should be used whenever you have the choice of a struct or a class and the structure has data members but no function members. We shall subscribe to the latter school of thought.

Classes

Here is a list of the class-related concepts with which you are expected to be familiar:

Input/Output

Keyboard Input (Standard Input)

Assuming appropriate declarations, you must know how the following statement would read first an integer, then a double, then a character, and finally a string:
cin >> n >> x >> c >> name;
You must also know how each of the following works (and differs from cin >>):
cin.get(c);                 // Read a character
cin.get(cString, n+1);      // Read a string
cin.getline(cString, n+1);  // Read a line of text
cin.ignore(80, '\n');       // Ignore remaining input on the line
getline(cin, cppString);    // Read a line of text into cppString

Screen Output (Standard Output)

Assuming appropriate declarations, and that all variables have values, you must know how the statement shown below would display the following: first an integer, then a double, then a character, and finally two strings, after which the manipulator endl would terminate the output line, in the sense that the next value displayed will appear on a new line of output:
cout << n << x << c << name << "and some text" << endl;

Manipulators

The default appearance of C++ output values may be altered by the use of manipulators, and you should know how and when to use these manipulators:

File Input/Output

You must know how to open and close a textfile, and how to read from a file when it is open for input, or write to a file when it is open for output. In particular, you must be aware of, and appreciate, the strong syntactic and behavioral parallel between keyboard input/output and file input/output, a consequence of the notion of C++ "streams".

Operators

In the following table the operators are grouped by conceptual category:

The same operators shown above are repeated below, but this time grouped by precedence, highest to lowest, with those operators on the same line, or on immediately adjacent lines, having the same precedence:

::

.   ->  []  ()(function call)

++  --   unary + and -
* (i.e., pointer dereference)
(typecast)
!   &
new  delete  new[]  delete[]
sizeof

*  /  %

+  -

<  <=  >   >=

== !=

&&

||

? :

=  +=  -=  *=  /=  %=

Some operator notes:

  1. It's at best often confusing, and at worst sometimes possibly dangerous, to use either ++ or -- anywhere but in a stand-alone-statement.
  2. Know how short-circuit evaluation works with && and ||.
  3. Don't forget how / works when both operands are integers.
  4. Don't use = when you mean ==, and watch out especially for this in if-statements.
  5. Most C++ operators (but not all) can be overloaded. However, you cannot invent new operators, and you can't change either the arity (required number of operands), or the precedence (the order in which it will be evaluated), of an operator when you overload it.

Expressions

You need to understand the difference between an expression and a statement.

You also need to understand how the data type of the result is determined when an expression containing values of different data types is being evaluated.

Control Structures

Boolean values, simple and compound conditions, and short-circuit evaluation

You need to understand the bool data type and how boolean values (constants, variables or expressions) are used to test conditions in both decision-making constructs and looping constructs. You must understand the use of the six relational operators and the three boolean operators to form both simple and compound boolean expressions and know how to evaluate those expressions. You must also understand how short-circuit evaluation of certain boolean expressions works.

You need to understand that in the C programming language the values 0 and 1 (or, more generally, 0 and anything not 0) were used as "true" and "false" and that for reasons of backward compatibility this still holds in C++.

For choosing actions (selection, or branching, or making decisions):

You need to understand how to use the various forms of the if-statement, including the if..else-statement and nested if-statements, as well as the switch-statement. You should know when a nested if-statement can be replaced by a switch-statement and when it cannot. You must be able to choose the most appropriate of the various possibilities for performing any given test. In particular, you must (for example) be able to write code to

Even more important, you must be able to recognize which of the above situations (or other similar ones) you are in at any given time, before you begin to write the corresponding code.

For repeating actions (looping or iteration):

You need to understand how to use the three C++ looping constructs

as well as the reasons for choosing one rather than another to perform a particular loop. This implies an understanding of the following terms and concepts:

You must also understand that if a loop is controlled by a compound boolean expression, then it is often the case that several tests need to be made after the loop has terminated to see why it ended (so that appropriate action may then be taken). In particular, you must be able to write loops to

Even more important, you must be able to recognize which of the above situations (or other similar ones) is most relevant to the problem you are trying to solve, before you begin to write the corresponding code.

For altering the flow of control within a loop:

You need to understand how each of the following statements alters the flow of control within a loop:

break;    // Control transfers to the statement immediately following
          // the loop within which the break statement occurs
continue; // Control transfers immediately to the beginning of the
          // next iteration of the loop within which the continue
          // statement occurs

Nested decision-making and looping structures

You must be able to recognize and code properly those situations which call for either a nested decision-making structure or a nested loop structure or, for that matter, some combination of two or more of any of the structures for decision-making and looping.

Functions and Parameters

You need to understand and appreciate that functions help make your programs modular, readable, and maintainable. A function encapsulates a "logical chunk" of related code that accomplishes a particular goal for the program. Each function should perform one simple task, such as computing and returning a value, or displaying a menu.

A function is declared to say that it exists somewhere and it is going to be used. A function is defined to show how it does what it does, and to reserve memory for the code that actually does it. A function is called (or invoked) to actually do what it does. A function declaration may be a full function definition or just a function prototype. Function prototypes are placed before the main function, and function definitions after main, at least in a single-file program.

Functions may be classified into the following two categories:

Functions may also be classified into the following two categories:

A void function is generally the best choice for "performing a task" that does not require any value to be "returned", though void functions may well return values to their caller via reference parameters. Use a value-returning function if the function's main or only job is to obtain or compute a single value of some kind and return that value to the caller.

A function definition always has a header and a body, and may or may not have a parameter list. The corresponding function prototype may be obtained by taking the function header and adding a semicolon at the end. You can even leave out the parameters in the function prototype (but not their types).

If present, the parameter list of a function consists of one or more type_name parameter_name pairs separated by commas within the parentheses following the function name. The parameter names are optional in the prototype but necessary in the header of the definition. The parameters in a function's parameter list form its interface with the rest of the program, i.e., its way of receiving information from, and sending information back to, the rest of the program when that function is called. Parameters thus fall into three conceptual categories, according to purpose:

Parameters may also be classified into the following two categories:

C++ offers the following two parameter-passing mechanisms:

You should understand that value parameters are used for "safety", reference parameters are used for "efficiency", and constant reference parameters are used for both when the thing being passed is large data structure.

Library Functions

You should be familiar with the following functions from the C++ Standard Library. In each case the name of the library is given first in angle brackets, followed by the names of the functions from that library.

<cctype>   isalnum  isalpha  isdigit
           islower  isupper
           tolower  toupper
<cmath>    exp      log       pow      sqrt     fabs
<cstdlib>  abs      atoi      exit     rand     srand    system
<cstring>  strcat   strcmp    strcpy   strlen

Also, although you are not expected to know offhand any of the values of the system- dependent constants defined in the <cfloat> and <climits> libraries, you should at least be aware of the existence of these libraries and their contents (in a general way).

Overloaded Functions and Default Parameters

You should know when and how to overload a function, as well as how to use default parameters with a function, and be able to decide whether one or the other should be used in a given situation.

Template Functions

You should have a basic understanding of what a template function is, as well as when and how to define and use one.

Pointers and Dynamic Storage

You should be familiar with and understand the following pointer concepts:

Scope, Lifetime and Storage Class of Variables

  1. You must understand that the scope of a program entity (such as a variable, but the idea applies to all program names), refers to the region of a program where the name of the entity can be accessed (i.e., used, or "seen"). C++ has
  2. You must understand the difference between local scope and global scope, which are often conveniently thought of as relative terms. The best advice we can offer in this context is this: Avoid using global variables!
  3. You must understand that the lifetime of a variable (or object) is the time during which memory is allocated for the storage of that variable (or object), whether this memory allocation occurs automatically or under programmer control via new and delete.
  4. You must understand the difference between the block lifetime of local (or automatic) variables) and the program lifetime of global variables, local variables in the main function, or variables declared to be static.
  5. You should understand the difference between a regular (or auto) variable, an extern variable, and a static variable. These represent three different storage classes in C++.

Miscellaneous

  1. A block of code is any code (a group of declarations and statements, for example) enclosed in braces like these: { } For example, the body of a main function (or any other function) is a block, and loop bodies are often blocks of code.
  2. The preprocessor deals with preprocessor directives like #include or #define before compilation starts.
  3. Understand the use of the reserved word const in each of the following contexts:
    1. defining named constants
    2. constant reference parameters
    3. constant member functions of a class
  4. Understand the use of the reserved word static applied to a local variable inside a function.
  5. Understand the use of the reserved word typedef, especially as used with arrays and C- strings, as in:
    typedef char LineType[81];
    
  6. Understand the difference between a named data type and an anonymous data type.
  7. Understand the dangers of overflow and underflow in numerical computations.
  8. Understand binary, octal and hexadecimal numbers and the C++ way of representing octal and hexadecimal numbers. (Example: 62 == 076 == 0x3E)

Keywords (Reserved Words)

Legacy keywords from C

auto      break     case       char     const     continue    default
do        double    else       enum     extern    float       for
goto      if        int        long     register  return      short
signed    sizeof    static     struct   switch    typedef     union
unsigned  void      volatile   while

New keywords in C++ (most, but not all of them)

asm          bool            catch       class             const_cast
delete       dynamic_cast    explicit    false             friend
inline       mutable         namespace   new               operator
private      public          protected   reinterpret_cast  static_cast
template     this            throw       true              try
typeid       typename        using       virtual           wchar_t

Common C++ Programming Errors

The following is a far-from-exhaustive list of typical C++ programming errors. You should be aware of all of them, as well as how each error is reported or tends to show up on your system, and what you should try to do to avoid that error in the first place.

  1. Making up your own syntax rules instead of following those of C++
  2. Making up your own style conventions instead of following those of your working group (in this case, your class)
  3. Style errors: Using poor indentation and/or alignment (leading to confusing or unreadable code)
  4. Capitalizing names inconsistently
  5. Believing that a compiler error pointer always points directly at the place in your program where there is a problem, or that the error message always describes the actual problem with your code
  6. Forgetting to #include one or more required header files
  7. Forgetting to include appropriate using statements for the namespaces used
  8. Forgetting to terminate each statement with a semicolon (;)
  9. Using = when you need == in a condition
  10. Trying to assign to an array or C-string variable
  11. Forgetting that arrays are always indexed starting at 0
  12. Trying to access non-existent array elements ("running off the end of an array")
  13. Using a value parameter when it must be a reference parameter, or vice-versa
  14. Failing to flush an input stream when you're finished reading from it
  15. Comparing float or double quantities for equality
  16. Forming relational or logical expressions that are meaningful to C++ but do not have the meaning you intended
  17. Failing to eliminate a dangling else
  18. Using & or | when you need && or ||
  19. Omitting one or more break statements in a switch
  20. Misinterpreting the scope of the loop control variable in a for-loop
  21. Omitting the semicolon that must follow the definition of a struct or a class
  22. Forgetting to initialize a variable, especially a loop control variable
  23. Failing to modify appropriately the loop control variable in a conditional loop
  24. Forgetting to provide appropriate constructors or a destructor (when necessary) for a class
  25. Failing to make things const that should be constant
  26. Attempting to dereference a NULL pointer
  27. Confusing the initialization of a pointer to a class object with the initialization of the object itself