A class is a data type that combines both a data structure and methods for manipulating the data. For example, a string class might consist of an array to hold the charac-ters in the string and methods to compare strings, combine strings, or extract portions of a string (see characters and strings).
As with other data types, once a class is declared, objects (sometimes called instances) of the class can be created and used. This way of structuring programs is called object-oriented programming because the class object is the basic building block (see object-oriented programming).
Object-oriented programming and classes provide sev-eral advantages over traditional block-structured languages. In a traditional BASIC or even Pascal program, there is no particular connection between the data structure and the procedures or functions that manipulate it. In a large program one programmer might change the data structure without alerting other programmers whose code assumes the original structure. On the other hand, someone might write a procedure that directly manipulates the internal data rather than using the methods already provided. Either transgression can lead to hard-to-find bugs.
With a class, however, data and procedures are bound together, or encapsulated. This means that the data in a class object can be manipulated only by using one of the methods provided by the class. If the person in charge of maintaining the class decides to provide an improved implementation of the data structure, as long as the data parameters expected by the class methods do not change, code that uses the class objects will continue to function properly.
Most languages that use classes also allow for inheri-tance, or the ability to create a new class that derives data and methods from a “parent” class and then modifies or extends them. For example, a class that provides support for 3D graphics could be derived from an existing class for 2D graphics by adding data items such as a third (Z) coor-dinate and replacing a method such as “line” with a version that works with three coordinates instead of two.
In designing classes, it is important to identify the essential features of the physical situation you are trying to model. The most general characteristics can be put in the “base class” and the more specialized characteristics would be added in the inherited (derived) classes.
Classes and C++
Classes first appeared in the Simula 67 language, which introduced the terms class and object (see Simula). As the name suggests, the language was used mainly for simu-lation and modeling, but its object-oriented ideas would prove influential. The Smalltalk language developed at Xerox PARC in the 1970s ran on the Alto computer, which pioneered the graphic user interface that would become popular with the Macintosh in the 1980s. Smalltalk used classes to build a seamless and extensible operating system and environment (see Smalltalk).
However it was Bjarne Stroustrup’s C++ language that brought classes into the programming mainstream (see c++). C++ essentially builds its classes by extending the C struct so that it contains both methods (class functions) and data. An access mechanism allows class variables to be designated as completely accessible (public), which is rare, accessible only by derived classes (protected), or accessible only within the class itself (private). The creation of a new object of the class is specified by a constructor function, which typically allocates memory for the object and sets initial default values. The corresponding destructor func-tion frees up the memory when the object no longer exists.
C++ allows for multiple inheritance, meaning that a class can be derived from more than one parent or base class. The language also provides two powerful mechanisms for extending functionality. The first, called virtual functions, allows a base class and its derived classes to have functions based on the same interface. For example, a base graph-ics class might have virtual line, circle, setcolor, and other functions that would be implemented in derived classes for 3D objects, 3D solid objects, and so on. When the program calls a method in a virtual class, the compiler automatically searches the class’s “family tree” until it finds the class that corresponds to the actual data type of the object.
A template specifies how to create a class definition based on the type of data to be used by the class. In other words, where a regular procedure takes and manipulates data parameters and returns data, a template takes data parameters and returns a definition of a class for working with that data (see template).
Other languages of the 1980s and later have embraced classes. Examples include descendants of the Algol family of languages (see Pascal, Ada, c++’s close cousin—Java), and Microsoft’s Visual Basic. (There is even a version of COBOL with classes.)
The use of class frameworks, such as the Microsoft Foundation Classes (MFC), the C++ STL (Standard Tem-plate Library) and various Java implementations, has pro-vided a superior way to organize the complexities of data access and operating system functions.
No comments:
Post a Comment