C++ Class Libraries vs. C Function Libraries

C function libraries have been available for years. Some, such as the standard run-time libraries, are bundled with the compiler; others, such as database and user interface libraries, are sold by independent software vendors.

This section compares C function libraries with C++ class libraries. The advantages of the C++ language, such as inheritance and polymorphism, are discussed in the C++ Tutorial.

Advantages of C++ Class Libraries

Class libraries have several advantages when compared with function libraries:

Classes encapsulate code and data.

Ordinary C libraries consist of one or more discrete program modules, which manage data in nonsystematic ways. It is generally difficult to isolate and control a particular library's data. By contrast, in C++ the data is made an integral part of the class through “encapsulation.” The class designer can control access to the data through the class's member functions.

New classes appear to be language extensions.

You can create objects of a library class the same way you create instances of the C++ built-in types. Thus classes, with their special constructors and overloaded operators, provide a natural programming interface.

Inheritance eliminates “code cloning.”

If you need a new C function that is “similar to but different from” a library C function, you must copy the original (assuming you have the source) and then change its name. A C++ class allows you to add functionality through derivation without disturbing the original code. Indeed, class derivation does not require you to have the base class source code.

Variable and function name collisions are minimized.

If two classes have identical data member names or member function names, there is no conflict. Classes must not have the same name, however, unless they are nested.

Tools such as class browsers enhance source code control.

C++ adds a level of structure not possible with C function libraries. The Source Browser in the Microsoft Programmer's WorkBench is a tool that allows you to view your source code in the order of class hierarchy.