Other Basic C++ Features

Here are a few other C++ features that are useful to know.

  1. When you define a class Stack, the name Stack becomes usable as a type name as if created with typedef. The same is true for enums.

  2. You can define functions inside of a class definition, whereupon they become inline functions, which are expanded in the body of the function where they are used. The rule of thumb to follow is to only consider inlining one-line functions, and even then do so rarely.

    As an example, we could make the Full routine an inline.

    class Stack {
       ...
       bool Full() { return (top == size); };
       ...
    };
    

    There are two motivations for inlines: convenience and performance. If overused, inlines can make your code more confusing, because the implementation for an object is no longer in one place, but spread between the .h and .c files. Inlines can sometimes speed up your code (by avoiding the overhead of a procedure call), but that shouldn't be your principal concern as a student (rather, at least to begin with, you should be most concerned with writing code that is simple and bug free). Not to mention that inlining sometimes slows down a program, since the object code for the function is duplicated wherever the function is called, potentially hurting cache performance.

  3. Inside a function body, you can declare some variables, execute some statements, and then declare more variables. This can make code a lot more readable. In fact, you can even write things like:

    for (int i = 0; i < 10; i++) ;
    

    Depending on your compiler, however, the variable i may still visible after the end of the for loop, however, which is not what one might expect or desire.

  4. Comments can begin with the characters // and extend to the end of the line. These are usually more handy than the /* */ style of comments.

  5. C++ provides some new opportunities to use the const keyword from ANSI C. The basic idea of const is to provide extra information to the compiler about how a variable or function is used, to allow it to flag an error if it is being used improperly. You should always look for ways to get the compiler to catch bugs for you. After all, which takes less time? Fixing a compiler-flagged error, or chasing down the same bug using gdb?

    For example, you can declare that a member function only reads the member data, and never modifies the object:

    class Stack {
       ...
       bool Full() const;  // Full() never modifies member data
       ...
    };
    

    As in C, you can use const to declare that a variable is never modified:

        const int InitialHashTableSize = 8;
    

    This is much better than using #define for constants, since the above is type-checked.

  6. Input/output in C++ can be done with the >> and << operators and the objects cin and cout. For example, to write to stdout:

        cout << "Hello world!  This is section " << 3 << "!";
    

    This is equivalent to the normal C code

        fprintf(stdout, "Hello world!  This is section %d!\n", 3);
    

    except that the C++ version is type-safe; with printf, the compiler won't complain if you try to print a floating point number as an integer. In fact, you can use traditional printf in a C++ program, but you will get bizarre behavior if you try to use both printf and << on the same stream. Reading from stdin works the same way as writing to stdout, except using the shift right operator instead of shift left. In order to read two integers from stdin:

        int field1, field2;
        cin >> field1 >> field2;
            // equivalent to fscanf(stdin, "%d %d", &field1, &field2);
            // note that field1 and field2 are implicitly modified
    

    In fact, cin and cout are implemented as normal C++ objects, using operator overloading and reference parameters, but (fortunately!) you don't need to understand either of those to be able to do I/O in C++.