11.07.2015 Views

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

88 classclassA class is a data type that combines both a data structure<strong>and</strong> methods for manipulating the data. For example, astring class might consist <strong>of</strong> an array to hold the charactersin the string <strong>and</strong> methods to compare strings, combinestrings, or extract portions <strong>of</strong> a string (see characters<strong>and</strong> strings).As with other data types, once a class is declared,objects (sometimes called instances) <strong>of</strong> the class can becreated <strong>and</strong> used. This way <strong>of</strong> structuring programs iscalled object-oriented programming because the classobject is the basic building block (see object-orientedprogramming).Object-oriented programming <strong>and</strong> classes provide severaladvantages over traditional block-structured languages.In a traditional BASIC or even Pascal program, there isno particular connection between the data structure <strong>and</strong>the procedures or functions that manipulate it. In a largeprogram one programmer might change the data structurewithout alerting other programmers whose code assumesthe original structure. On the other h<strong>and</strong>, someone mightwrite a procedure that directly manipulates the internaldata rather than using the methods already provided. Eithertransgression can lead to hard-to-find bugs.With a class, however, data <strong>and</strong> procedures are boundtogether, or encapsulated. This means that the data in aclass object can be manipulated only by using one <strong>of</strong> themethods provided by the class. If the person in charge<strong>of</strong> maintaining the class decides to provide an improvedimplementation <strong>of</strong> the data structure, as long as the dataparameters expected by the class methods do not change,code that uses the class objects will continue to functionproperly.A class encapsulates (or hides) its internal information from therest <strong>of</strong> the program. When the program calls MyCircle.GetPosition,the GetPosition member function <strong>of</strong> the MyCircle Circle class objectretrieves the private Position data <strong>and</strong> sends it back to the callingstatement, where it is assigned to the variable P. Private data cannotbe directly accessed or changed by an outside caller.Most languages that use classes also allow for inheritance,or the ability to create a new class that derives data<strong>and</strong> methods from a “parent” class <strong>and</strong> then modifies orextends them. For example, a class that provides supportfor 3D graphics could be derived from an existing class for2D graphics by adding data items such as a third (Z) coordinate<strong>and</strong> replacing a method such as “line” with a versionthat works with three coordinates instead <strong>of</strong> two.In designing classes, it is important to identify theessential features <strong>of</strong> the physical situation you are trying tomodel. The most general characteristics can be put in the“base class” <strong>and</strong> the more specialized characteristics wouldbe added in the inherited (derived) classes.Classes <strong>and</strong> C++Classes first appeared in the Simula 67 language, whichintroduced the terms class <strong>and</strong> object (see Simula). As thename suggests, the language was used mainly for simulation<strong>and</strong> modeling, but its object-oriented ideas wouldprove influential. The Smalltalk language developed atXerox PARC in the 1970s ran on the Alto computer, whichpioneered the graphic user interface that would becomepopular with the Macintosh in the 1980s. Smalltalk usedclasses to build a seamless <strong>and</strong> extensible operating system<strong>and</strong> environment (see Smalltalk).However it was Bjarne Stroustrup’s C++ language thatbrought classes into the programming mainstream (seec++). C++ essentially builds its classes by extending theC struct so that it contains both methods (class functions)<strong>and</strong> data. An access mechanism allows class variables to bedesignated as completely accessible (public), which is rare,accessible only by derived classes (protected), or accessibleonly within the class itself (private). The creation <strong>of</strong> a newobject <strong>of</strong> the class is specified by a constructor function,which typically allocates memory for the object <strong>and</strong> setsinitial default values. The corresponding destructor functionfrees up the memory when the object no longer exists.C++ allows for multiple inheritance, meaning that a classcan be derived from more than one parent or base class.The language also provides two powerful mechanisms forextending functionality. The first, called virtual functions,allows a base class <strong>and</strong> its derived classes to have functionsbased on the same interface. For example, a base graphicsclass might have virtual line, circle, setcolor, <strong>and</strong> otherfunctions that would be implemented in derived classes for3D objects, 3D solid objects, <strong>and</strong> so on. When the programcalls a method in a virtual class, the compiler automaticallysearches the class’s “family tree” until it finds the class thatcorresponds to the actual data type <strong>of</strong> the object.A template specifies how to create a class definitionbased on the type <strong>of</strong> data to be used by the class. In otherwords, where a regular procedure takes <strong>and</strong> manipulatesdata parameters <strong>and</strong> returns data, a template takes dataparameters <strong>and</strong> returns a definition <strong>of</strong> a class for workingwith that data (see template).Other languages <strong>of</strong> the 1980s <strong>and</strong> later have embracedclasses. Examples include descendants <strong>of</strong> the Algol family<strong>of</strong> languages (see Pascal, Ada, c++’s close cousin—Java),<strong>and</strong> Micros<strong>of</strong>t’s Visual Basic. (There is even a version <strong>of</strong>COBOL with classes.)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!