Chapter 6. Taking Inventory

Now we have enough of the fundamentals of programming under our belts to look at some of the more powerful features of C++. As I've mentioned before, C++ was intended as the successor to C. What I haven't told you is exactly why it was invented.

To understand this, we'll have to consider the differences between two basic kinds of variables that exist in both C and C++: native (i.e., defined in the language itself) and user-defined (i.e., defined by the programmer). The native types that we've been using are char, short, and unsigned short (and int, but only for the return type of main), all of which have been inherited from C.[1] The user-defined types we've been using are the string, Vec, and the types of cin and cout.

[1] There are actually several other native C++ types that we haven't used: long, float, double, and bool. The long type is useful for storing whole-number values that are larger than will fit into a short (hence the name), while float and double are able to store values that have fractional parts as well as integral values. These are useful in scientific and engineering calculations; we'll use all of them except for float later in the book. The bool type, a relatively recent addition to C++, is useful for keeping track of a true/false condition. We'll see how to use this variable type later in this chapter.

What difference does it make whether a variable is native or user-defined? Quite a bit of difference, in fact. In both C and C++, variables of the native types are fully supported by the language. To be “fully supported” means that variables can be defined, initialized, assigned values, passed as arguments and return values, and compared to other values of the same type. Such a variable can be assigned storage in either the static or auto storage classes: If a variable is auto, the storage is assigned at entry to the function where it is defined, and released automatically at exit from that function; if it is static, it is initialized to some reasonable value either at link time (for a global variable) or upon the first entry to the function where it is defined (for a local variable).

However, most of these facilities aren't available to user-defined data types in C. For example, variables of such types can't be compared; this limitation results from the fact that the C compiler has no idea how to compare two variables of a type that you define. Similarly, what is a reasonable default value for a variable of a user-defined type? Presumably, the user (i.e., the programmer) knows, but the C compiler doesn't.

In this chapter, we'll see how to give a C++ compiler enough information to allow data types that we define to behave almost exactly like the native types. Susan had a few questions on this topic:

Susan: I think I need to find out something here. I am getting the impression that what is "native" is C and what is "user-defined" is C++. Is that right? And if so, why?

Steve: Pretty much so. As to why, the answer is pretty simple: the reason that C++ was invented in the first place was to add good support for user-defined types to the efficiency of C.

Susan: Okay, but why are cin and cout user-defined? We didn't define them. They're from the standard library, right? Then shouldn't they be native?

Steve: That's a good point, but in fact things defined in the standard library aren't native. Here's a quick way to tell whether a variable type is native or user-defined: if you don't need to #include a header file to use it, then it's native. Anything not native is user-defined.

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

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