Chapter 34

Ten Features Not Covered in This Book

In This Chapter

arrow Binary logic

arrow Pure virtual functions

arrow The string class

arrow Templates and the Standard Template Library

The C++ language contains so many features that covering every one in a single book — especially a book intended for beginning programmers — is impossible. Fortunately, you don’t need to master all the features of the language in order to write big, real-world programs.

Nevertheless, you may want to look ahead at some of the features that didn’t make the cut for this beginner’s book, just in case you see them in other people’s programs.

The goto Command

This command goes all the way back to C, the progenitor of C++. In principle, using this command is easy. You can place goto label; anywhere you want. When C++ comes across this command, control passes immediately to the label, as demonstrated in this code snippet:

    for(;;)
  {
      if (conditional expression)
      {
          goto outahere;
      }
      // ...whatever you want...
  }
outahere:
  // ...program continues here...

In practice, however, goto introduces a lot of ways to screw up — many more than I can go into here. In any case, it didn’t take long before programmers noticed that the two most common uses of the goto were to exit loops and to go to the next case within a loop. The C Standards Committee introduced break and continue and almost completely removed the need for the goto command. I can say that I’ve been programming in C and C++ for almost 20 years, and I’ve never had an application for a goto that I couldn’t handle in some other way more clearly.

The Ternary Operator

The ternary operator is an operator unique to C and C++. It works as follows:

  int n = (conditional) ? expression1 : expression2;

The ? operator first evaluates the conditional. If the condition is true, then the value of the expression is equal to the value of expression1; otherwise, it’s equal to the value of expression2.

For example, you could implement a maximum() function as follows:

  int max(int n1, int n2)
{
    return (n1 > n2) ? n1 : n2;
}

The ternary operator can be applied to any type of numeric but cannot be overloaded. The ternary operator is truly an expression — not a control statement like an if.

Binary Logic

I chose to skip entirely the topic of bitwise operators. Some readers will consider this scandalous. After all, how can you talk about programming without getting down to ones and zeros? It’s not that I don’t consider the topic worthwhile — it’s just that I find explaining the topic properly takes many pages of text and leaves readers somewhat confused, when in practice it’s rarely used. Google the topic once you feel comfortable with the basics of C++ programming.

Enumerated Types

This topic was almost included in the book but it just isn’t used often enough. A quick overview here should suffice. The simple idea is that you can define constants and let C++ assign them values, as shown here:

  enum Colors {BLACK, BLUE, GREEN, YELLOW, RED};
Colors myColor = BLACK;

The problem with enumerated types lies in the implementation: Rather than create a true type, C++ uses integers. In this case, BLACK is assigned the value 0, BLUE is assigned 1, GREEN 2, and so on.

The 2011 Standard Library for C++ “fixed” this problem by creating an enumerated class type as shown in the following snippet:

  enum class Colors {BLACK, BLUE, GREEN, YELLOW, RED};
Colors myColor = Colors::BLACK;

In this version, Colors is a new type. Each of the constants, BLACK, BLUE, and so on, are members of type Colors. You can still cast an object of class Colors into an int, but an implicit cast is not allowed.

Namespaces

It’s possible to give different entities in two different libraries the same name. For example, the grade() function within the Student library probably assigns a grade, whereas the grade() function within the CivilEngineering library might set the slope on the side of a hill. To avoid this problem, C++ allows the programmer to place her code in a separate namespace. Thus the grade within the Student namespace is different from the grade within CivilEngineering.

The namespace is above and beyond the class name. The grade() member function of the class BullDozer in the CivilEngineering namespace has the extended name CivilEngineering::BullDozer::grade().

remember.eps All library objects and functions are in the namespace std. The statement at the beginning of the program template using namespace std; says that if you don’t see the specified object in the default namespace, then go look in std. Without this feature, I would have to include the namespace explicitly, as in the following snippet:

  std::cout << "Hello, world!" << std::endl;

Pure Virtual Functions

You get a handle on how to declare functions virtual in Chapter 29. What I don’t mention there is that you don’t have to define a function declared virtual. Such an undefined function is known as a pure virtual member function. At that point, however, things get complicated. For example, a class with one or more pure virtual functions is said to be abstract and cannot be used to create an object (see what I mean?). Tackle this subject after you feel comfortable with virtual functions and late binding.

The string Class

This is another topic that barely missed the cut. Most languages include a string class as an intrinsic type for handling strings of characters easily. In theory, the string class should do the same for C++. In practice, however, it’s not that simple. Because string is not an intrinsic type, the error messages that the compiler generates when something goes wrong are more like those associated with user-defined classes. For a beginner, these messages can be very difficult to interpret.

technicalstuff.eps It’s actually worse than I’m describing here — string isn’t even a class. It’s an instance of a template class. The error messages can be breathtaking.

Multiple Inheritance

In Chapter 28, I describe how to base one class on another by using inheritance. What I don’t mention there is that one class can actually extend more than one base class. This sounds simple but can get quite complicated when the two base classes contain member functions with the same name. Even worse is when both base classes are themselves subclasses of some common class. In fact, so many problems arise that C++ is the only C-like language that supports multiple inheritance. Java and C#, both languages derived from C++, decided to drop support for multiple inheritance. I recommend that beginning programmers avoid the subject.

Templates and the Standard Template Library

The makers of C++ noticed how similar functions like the following are:

  int max(int n1, int n2)
{
    if (n1 > n2)
    {
        return n1;
    }
    return n2;
}
double max(double n1, double n2)
{
    if (n1 > n2)
    {
        return n1;
    }
    return n2;
}
char max(char n1, char n2)
{
    if (n1 > n2)
    {
        return n1;
    }
    return n2;
}

I can almost imagine the scene: “Wouldn’t it be cool,” one says to another, “if we could replace the type with a pseudo-type T that we could define at compile time?” Before you know it, presto — templates become part of C++:

  template <class T> T max(T t1, T t2)
{
    if (t1 > t2)
    {
        return t1;
    }
    return t2;
}

Now the programmer can create a max(int, int) by replacing T with int and compiling the result, create a max(double, double) by replacing T with double, and so forth. The Standards Committee even released an entire library of classes, known as the Standard Template Library (STL for short), based upon template classes.

For a beginner, however, the subject of template classes starts to get syntactically very complicated. In addition, the errors that the compiler generates when you get a template instantiation wrong are bewildering to an expert, never mind a beginner. This is definitely a topic that needs to wait until you feel comfortable with the basic language.

Lambda Functions

The older C++ standard was adopted in 2003. A newer standard was finally adopted in 2011, introducing a number of features to the language. Although these new features certainly add more capability, they’ve also changed C++ from a nice (if somewhat quirky) smallish language to a very large language. For example, lambda functions are a new — and remarkably different — way of declaring and invoking functions. Lambda functions support a different style of programming known as functional programming. The problem is that many of the new 2011 features don’t address a burning need for what they provide. So it’s okay to save them for later. You can attack many of these new features, such as lambda functions, after you feel comfortable with the basic features of C++ presented in this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.78.102