
TOPICS 1 13.1 Procedural and Object-Orien ted 13.12 Arrays of Objects Programming 13.13 Focus on Problem Solving 13.2 Introduction to Classes and Program Desig n: An OOP 13.3 Defining an Instance of a Class Case Study 13.4 Why Have Private Members? 13.1 4 Focus on Object-Oriented 13.5 Focus on Software Engineering: Programming: Creating Separating Class Specification an Abstract Array Data Type from Implementation 13.15 Focus on Object-Oriented 13.6 Inline Member Functions Design: The Unified Modeling 13.7 Constructors Language (U Ml) 13.8 Passing Arguments to Constructors , 3.16 Focus on Object-Oriented 13.9 Destructors Design: Finding the Classes 13.10 Overloading Comtructors and Their Reponsibilities 13.11 Private Member Functions Procedural and Object-Oriented Programming CONCEPT: Procedural programming is a method of writ ing software. It is a programming practice cem ceed on the procedures or actions that take place in a program. Object-oriemed programming is centered around the object. Objects are created from abstract data types that encapsulate data and functions rogerhcr. There arc two common programming methods in practice today: procedural program­ ming and object-oriented programming (or OOP). Up to this chaprer, you have learned to write procedural programs. 729 730 Chapter 13 Introduction to Classes In a procedural program, you typically have data stored in a collection of variables and/or structures, coupled with a set of functions that perform operations on the data. The data and the functions arc separate entities. For example, in a program that works with the geometry of a rectangle you might have the variables in Table 13-1: Table 13-1 Variable Definition Description double width; Holds the rectangle's width double length; Holds the rectangle's length In addition to the variables listed in Table 13-1, you might also have the functions listed in Table 13-2, Table 13-2 Function Name Description setData( ) Sto res values in width and length displaywidth() Displays the rectangle's width displayLength{} Displays the rectangle'S length displayArea() Displays the rectangle's area Usually the variables and data strucrures in a procedural program are passed (0 the func­ tions that perform the desired operations. As you mighr imagine, the focus of procedural programming is on creating the functions thar operate on the program's data. Procedural programming has worked well for software developers fo r many years. How~ ever, as programs become larger and more compleX", rhe separation of a program's data and the code that operates on the data can lead (0 problems. For example, the data in a procedural program are stored in variables, as well as more complex structures that are created from variables. The procedures that operate on the data must be designed with chose variables and data structures in mind. But, what happens if the format of the data is altered? Quite often, a program's specifications change, resulting in redesigned data struc­ HIres. When the structure of the data changes, the code that operates on the data must also change to accept the new fo rmat. This results in additional work fo r programmers and a greater opportunity for bugs to appear in the code. This problem has helped influence the shift from procedural programming to object-oriented programming (OOP). Whereas procedural programming is centered on creating procedures or functions, object-orienred programming is cemered on creating objects. An object is a software entity that contains both data and procedures. The data that are contained in an object are known as the object's attributes. The procedures that an object performs are called member {unctions. The object is, conceptually, a self-contained unit consisting of attributes (data) and procedures (functions). This is illustrated in Figure 13-1. OOP addresses rhe problems that can result from [he separation of code and data through encapsulation and data hiding. Encapsulation refers to the combining of data and code into a single objeer. Data hidillg refers to an object's ability TO hide its data from code that , 3. 1 Procedural and Object-Oriented Programming 731 Figure 13-1 Object Data (Attributes) ••••••• t t 00 00 Functions That Operate on the Data NOTE: In other programming languages, the procedures that an object performs are <) often called methods. ~------------------------------------------~ is outside the object. Only the objecr's member functions may directly access and make changes to the object's data. An object typically hides its data, but allows outside code to access its member functions. As shown in Figure 13-2, [he object's member fu nctions pro­ vide programming sta tements outside the object with indirect access to the object's data. Figure 13· 2 Object Data (Attributes) ••••••• t t OIl Code 00 Outside the Object OIl 00 Functions That Operate on the Data 732 Chapter 13 Introduction to Classes When an object's internal data arc hidden from outside code, and access to that data is restricted to the object's member func tions, the dara arc protected from accidental corrup­ tiOIl, In addition, the programming code outside the object does not need to know about [he fo rmat or internal structure of the object's data. The code only needs to interact with the object's functions. When a programmer changes the structure of an object's internal data, he or she also modifies the object's member functions so they may properly operate on the data. The way in which outside code interacts with the member functions, however, does not change. An everyday example of objecr-oriented technology is the automobile. It has a ra ther sim­ ple interface that consists of an ignition switch, steering wheel, gas pedal, brake pedal, and a gear shift. Vehicles with manual transmissions also provide a ·clutch pedal. If you want to drive an automobile (to become its user), you o nl y have to [earn to operate these cle­ ments of its interface. To Start the motOr, you simply rurn the key in the ignition switch. What happens internally is irrelevant to the user. U you want to steer the auto to the left, you rOtate the steering wheel left. The movements of all the linkages connecting the steer­ ing wheel to the front tires occur transparently. Because automobiles have simple user interfaces, they can be driven by people who have no mechanical knowledge. This is good for the makers of automobiles because it means more people are likely to become custOmers. It's good for the users of amomobiles because they can learn just a few simple procedures and operate almost any ve hicle. These are also valid concerns In software deveiopmenr. A real-world program is rarely written by only one person. Even the programs you have created so far weren't written entirely by you . If you incorporated C++ library functions, or objects like cin and cout, you used code written by someone else. In the world of professional software develop­ rnent, programmers commonly work in teams, buy and sell theif code, and collaborate on projects. With OOP, programmers can create objects with powerful engines tucked away "under the hood," protected by simple interfaces that safeguard the object's algorithms. Object Reusability In add ition to solving the problems of code/data separation, d1e LIse of oor has also been encouraged by the tfend of object reusability. An object is not a stand-alone program, but is used by programs rhat need its se(vice. Foe example, Sharon is a pcogrammer who has developed an object for rendering 3D images. She is a math whiz and knows a lot about computer graphics, so her object is coded to perform all rhe necessary 3D mathematical operations and handle the computer's video hardware. Tom, who is writing a program for an architectural firm, needs his application to display 3D images of buildings. Because he is working under a right deadline and does nor possess a great deal of knowledge about computer graphics, he can use Sharo n's object to perform the 3D rendering (for a small fee, of course!). Classes and Objects Now let's discuss how objects are created in software. Before an object can be created, it mllst be designed by a programmer. The programmer determines the attributes and func­ tions that are necessa ry, and then creates a class. A class is code that specifics the attributes 13.1 Proc.edural and Obiect-Oriented Programming 733 and member functions that a parricular type of object may have. Think of a class as a "blueprint" that objects may be created from . It serves a similar purpose as the blueprint for a house. The blueprint itself is not a house, but is a detailed description of a house. Wben we use the blueprint to bu ild an actual house, we could say we are building an instance of the house described by the blueprinr. If we so desire, we can build several iden­ tical houses from rhe same blueprint. Each house is a separate instance of the house described by the blueprint. This idea is illustrated in Figure 13-3. Figure 1 3-3 Blueprint that describes a house. Instances of the house described by the blueprint. So, a class is not an object, but it is a description of an object. When the program is run­ ning, it uses the class to create, in memory, as many objects of a specific type as needed. Each object thar is created from a class is called an instance of the class. For example, Jessica is an entomologist (someone who studies insects) and she also enjoys writing computer programs.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages96 Page
-
File Size-