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.

340 object-oriented programmingPARC laboratory, home <strong>of</strong> innovative research in graphicaluser interfaces. Smalltalk, like Windows today, treats eachwindow, menu, <strong>and</strong> other control on the screen as an object(see Smalltalk). Finally, during the 1980s C++ came intoprominence, adding the essential features <strong>of</strong> object-orientedprogramming to the already very popular C language.Today most popular mainstream languages, including C++,Java, <strong>and</strong> Visual Basic, are object-oriented (see C++ <strong>and</strong>Java). Many specialized database languages are also objectoriented.Elements <strong>of</strong> Object-Oriented ProgrammingThe various object-oriented languages differ somewhat incapabilities, <strong>and</strong> <strong>of</strong> course in syntax. However, being objectorientedgenerally implies that the language has the followingfeatures.Classes <strong>and</strong> ObjectsAn object is defined using a template called a class. A classcontains both the data needed to characterize the object<strong>and</strong> the procedures (sometimes called methods or memberfunctions) needed to work with the object (see class).Thus, there could be a class for circles to be drawn on agraphics display. The class might include as its data the ×<strong>and</strong> y coordinates for the center <strong>of</strong> the circle, the size <strong>of</strong> theradius, whether the circle is filled, the color to be used forfilling, <strong>and</strong> so on. (See C++ for more examples.)When the program needs to use an object <strong>of</strong> the class,it declares it in the same way it would an ordinary built-indata type such as an integer. Languages such as C++ providefor a special function called a constructor that can beused to define the processing needed when a new object iscreated—for example, memory allocation <strong>and</strong> setting initialvalues for variables.To access data or functions within a class, the name<strong>of</strong> an object <strong>of</strong> that class is used, followed by a variable orfunction. Thus, if there’s a class called circle, a programmight specify the following:MyCircle Circle; // Declare an object <strong>of</strong> theCircle classMyCircle.X = 100; // X coordinate on screenMyCircle.Y = 50; // Y coordinate on screenMyCircle.Radius = 25; // Radius in pixelsMyCircle.Filled = True; // A Boolean constantequal to 1MyCircle.FillColor = Blue; // a previouslydefined color constantOnce these specifications have been made, the circlecan be drawn by calling upon its “draw” method or memberfunction:MyCircle.Draw;The designer <strong>of</strong> a class can choose to restrict access tocertain data items or functions, using a keyword such asprivate or protected. For example, instead <strong>of</strong> having thepart <strong>of</strong> the program that uses the class directly set the x<strong>and</strong> y coordinates, it could keep those variables private <strong>and</strong>In the object-oriented C++ programming language data within aclass can be restricted in several ways. Private data can be accessedonly from within the class itself, or from another class declared tobe a “friend” <strong>of</strong> the containing class. Protected data has these forms<strong>of</strong> access, plus it can also be accessed from any class derived fromthe containing class. Finally, Public data or functions (methods)can be accessed from anywhere in the program, <strong>and</strong> provides theinterface by which the class is used.instead provide a method called SetPos. The class mightthen take the coordinates specified by the user <strong>and</strong> adjustthem to fit the screen dimensions. The Draw method wouldthen use the adjusted internal coordinates rather than thosesupplied originally by the user.InheritanceMany objects are more elaborate or specialized variations <strong>of</strong>more basic objects. For example, in Micros<strong>of</strong>t Windows thevarious kinds <strong>of</strong> dialog boxes are specialized versions <strong>of</strong> thegeneral Window class. Therefore, the specialized versionis created by declaring it to be derived from a “base class.”Put another way, the specialized class inherits the basicdata <strong>and</strong> functions available in the base (parent) class. Theprogrammer can then add new data or functions or modifythe inherited ones to create the necessary behavior for thespecialized class.Languages such as C++ allow for a class to be derivedfrom more than one base class. This is called multipleinheritance. For example, a Message Window class mightinherit its overall structure from the Window class <strong>and</strong>its text-display capabilities from the Message class. However,it can sometimes be difficult to keep the relationshipsbetween multiple classes clear. The Java language takes thealternative approach <strong>of</strong> being limited to only single inheritance<strong>of</strong> classes, but allowing interfaces (specifications <strong>of</strong>how a class interacts with the program) to be multiplyinherited.Polymorphism <strong>and</strong> OverloadingDifferent kinds <strong>of</strong> objects <strong>of</strong>ten have analogous methods.For example, suppose there is a series <strong>of</strong> classes that representvarious polygons: square, triangle, hexagon, <strong>and</strong>

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

Saved successfully!

Ooh no, something went wrong!