【正文】
C A History of C, C++, and C The C programming language was created in the spirit of the C and C++ programming languages. This accounts for its powerful features and easy learning curve. The same can39。t be said for C and C++, but because C was created from the ground up, Microsoft took the liberty of removing some of the more burdensome features — such as pointers. This section takes a look at the C and C++ languages, tracing their evolution into C. The C programming language was originally designed for use on the UNIX operating system. C was used to create many UNIX applications, including a C piler, and was eventually used to write UNIX itself. Its widespread acceptance in the academic arena expanded to include the mercial world, and software vendors such as Microsoft and Borland released C pilers for personal puters. The original Windows API was designed to work with Windows code written in C, and the latest set of the core Windows operating system APIs remain patible with C to this day. From a design standpoint, C lacked a detail that other languages such as Smalltalk had already embraced: the concept of an object. You39。ll learn more about objects in Chapter 8, Writing ObjectOriented Code. For now, think of an object as a collection of data and a set of operations that can be performed on that data. Objectstyle coding could be acplished using C, but the notion of an object was not enforced by the language. If you wanted to structure your code to resemble an object, fine. If you didn39。t, fine. C really didn39。t care. Objects weren39。t an inherent part of the language, so many people didn39。t pay much attention to this programming paradigm. After the notion of objectoriented development began to gain acceptance, it became clear that C needed to be refined to embrace this new way of thinking about code. C++ was created to embody this refinement. It was designed to be backwardly patible with C (such that all C programs would also be C++ programs and could be piled with a C++ piler). The major addition to the C++ language was support for this new object concept. The C++ language added support for classes (which are templates of objects), and enabled an entire generation of C programmers to think in terms of objects and their behavior. The C++ language is an improvement over C, but it still has some disadvantages. C and C++ can be hard to get a handle on. Unlike easytouse languages like Visual Basic, C and C++ are very low level and require you to do a lot of coding to make your application run well. You have to write your own code to handle issues such as memory management and error checking. C and C++ can result in very powerful applications, but you need to ensure that your code works well. One bug can make the entire application crash or behave unexpectedly. Because of the C++ design goal of retaining backward patibility with C, C++ was unable to break away from the low level nature of C. Microsoft designed C to retain much of the syntax of C and C++. Developers who are familiar with those languages can pick up C code and begin coding relatively quickly. The big advantage to C, however, is that its designers chose not to make it backwardly patible with C and C++. While this may seem like a bad deal, it39。s actually good news. C eliminates the things that makes C and C++ difficult to work with. Because all C code is also C++ code, C++ had to retain all of the original quirks and deficiencies found in C. C is starting with a clean slate and without any patibility requirements, so it can retain the strengths of its predecessors and discard the weaknesses that made life hard for C and C++ programmers. Introducing C C, the new language introduced in the .NET Framework, is derived from C++. However, C is a modern, objectedoriented (from the ground up) typesafe language. Language features The following sections take a quick look at some of the features of the C language. If some of these concepts don39。t sound familiar to you, don39。t worry. All of them are covered in detail in later chapters. Classes All code and data in C must be enclosed in a class. You can39。t define a variable outside of a class, and you can39。t write any code that39。s not in a class. Classes can have constructors, which execute when an object of the class is created, and a destructor, which executes when an object of the class is destroyed. Classes support single inheritance, and all classes ultimately derive from a base class called object. C supports versioning techniques to help your classes evolve over time while maintaining patibility with code that uses earlier versions of your classes. As an example, take a look at a class called Family. This class contains the two static fields that hold the first and last name of a family member as well as a method that returns the full name of the family member. class Class1 { public string FirstName。 public string LastName。 public string FullName() { } return FirstName + LastName。 } Note Single inheritance means that a C class can inherit from only one base class. C enables you to group your classes into a collection of classes called a namespace. Namespaces have names, and can help organize collections of classes into logical groupings. As you begin to learn C, it bees apparent that all namespaces relevant to the .NET Framework begin with System. Microsoft has also chosen to include some classes that aid in backwards patibility and API access. These classes are contained within the Microsoft namespace. Data types C lets you work with two types of data: value types and reference types. Value types hold actual values. Reference types hold references to