Professional C__ - Marc Gregoire [432]
Graphical user interfaces: Most commercial programs today run on an operating system that has a graphical user interface, containing such elements as clickable buttons, movable windows, and hierarchical menus. C++, like the C language, has no notion of these elements. To write a graphical application in C++, you need to use platform-specific libraries that allow you to draw windows, accept input through the mouse, and perform other graphical tasks.
Networking: The Internet has changed the way we write applications. These days, most applications check for updates through the web, and games provide a networked multiplayer mode. C++ does not provide a mechanism for networking, though several standard libraries exist. The most common means of writing networking software is through an abstraction called sockets. A socket library implementation can be found on most platforms and it provides a simple procedure-oriented way to transfer data over a network. Some platforms support a streams-based networking system that operates like I/O streams in C++. Since IPv4 is running out of IP addresses, its successor, IPv6, will soon take over. Therefore, choosing a networking library that is IPv-independent would be a better choice than choosing one that only supports IPv4.
OS Events and application interaction: In pure C++ code, there is little interaction with the surrounding operating system and other applications. The command-line arguments are about all you get in a standard C++ program without platform extensions. For example, operations such as copy and paste are not directly supported in C++ and require platform-provided libraries.
Low-level files: Chapter 15 explains standard I/O in C++, including reading and writing files. Many operating systems provide their own file APIs, which are sometimes incompatible with the standard file classes in C++. These libraries often provide OS-specific file tools, such as a mechanism to get the home directory of the current user.
Threads: Concurrent threads of execution within a single program are not directly supported in C++03 or earlier. C++11 does include a threading library, explained in Chapter 22. If your compiler does not yet support the C++11 threading library, you need to use a third-party library. The most commonly used third-party thread library is called pthreads. Many operating systems and object-oriented frameworks also provide their own threading models.
CROSS-LANGUAGE DEVELOPMENT
For certain types of programs, C++ may not be the best tool for the job. For example, if your Unix program needs to interact closely with the shell environment, you may be better off writing a shell script than a C++ program. If your program performs heavy text processing, you may decide that the Perl language is the way to go. Sometimes what you want is a language that blends the general features of C++ with the specialized features of another language. Fortunately, there are some techniques you can use to get the best of both worlds — the flexibility of C++ combined with the unique specialty of another language.
Mixing C and C++
As you already know, the C++ language is a superset of the C language. All C programs will compile and run in C++ with a few minor exceptions. These exceptions usually have to do with reserved words. In C, for example, the term class has no particular meaning. Thus, it could be used as a variable name, as in the following C code:
int class = 1; // Compiles in C, not C++
printf("class is %d\n", class);
Code snippet from MixingC\MixingC.cpp
This program will compile and run in C, but will yield an error when compiled as C++ code. When you translate, or port, a program from C to C++, these are the types of errors you will face. Fortunately, the fixes are usually quite simple. In this case, rename the class variable to classID and the code will compile.
The ease of incorporating C code in a C++ program comes in handy when you encounter a useful