Sunday, December 27, 2009

Object Oriented Programming Concepts

Let’s briefly examine a few of the major elements of object-oriented languages in general, and C++ in particular.

Objects


When you approach a programming problem in an object-oriented language, you no longer ask how the problem will be divided into functions, but how it will be divided into objects. Thinking in terms of objects, rather than functions, has a surprisingly helpful effect on how easily programs can be designed. This results from the close match between objects in the programming sense and objects in the real world.

What kinds of things become objects in object-oriented programs? The answer to this is limited only by your imagination, but here are some typical categories to start you thinking:

*Physical objects
Automobiles in a traffic-flow simulation
Electrical components in a circuit-design program
Countries in an economics model
Aircraft in an air traffic control system

*Elements of the computer-user environment
Windows
Menus
Graphics objects (lines, rectangles, circles)
The mouse, keyboard, disk drives, printer

*Data-storage constructs
Customized arrays
Stacks
Linked lists
Binary trees

*Human entities
Employees
Students
Customers
Salespeople

* Collections of data
An inventory
A personnel file
A dictionary
A table of the latitudes and longitudes of world cities

* User-defined data types
Time
Angles
Complex numbers
Points on the plane

* Components in computer games
Cars in an auto race
Positions in a board game (chess, checkers)
Animals in an ecological simulation
Opponents and friends in adventure games

The match between programming objects and real-world objects is the happy result of combining data and functions: The resulting objects offer a revolution in program design. No such close match between programming constructs and the items being modeled exists in a procedural language.

Classes


In OOP we say that objects are members of classes. What does this mean? Let’s look at an analogy. Almost all computer languages have built-in data types. For instance, a data type int, meaning integer, is predefined in C++
You can declare as many variables of type int as you need in your program:

int day;
int count;
int divisor;
int answer;

In a similar way, you can define many objects of the same class. Class serves as a plan, or blueprint. It specifies what data and what functions will be included in objects of that class. Defining the class doesn’t create any objects, just as the mere existence of data type intdoesn’t create any variables. A class is thus a description of a number of similar objects.

Encapsulation


When an engineer needs to add a resistor to the device she is creating, she doesn’t typically build a new one from scratch. She walks over to a bin of resistors, examines the colored bands that indicate the properties, and picks the one she needs. The resistor is a “black box” as far as the engineer is concerned—she doesn’t much care how it does its work, as long as it conforms to her specifications. She doesn’t need to look inside the box to use it in her design. The property of being a self-contained unit is called encapsulation. With encapsulation, you can accomplish data hiding. Data hiding is the highly valued characteristic that an object can be used without the user knowing or caring how it works internally. Similarly, when the engineer uses the resistor, she need not know anything about the internal state of the resistor. All the properties of the resistor are encapsulated in the resistor object; they are not spread out through the circuitry. It is not necessary to understand how the resistor works to use it effectively. Its workings are hidden inside the resistor’s casing.
C++ supports encapsulation through the creation of user-defined types, called classes. The actual inner workings of the class can be hidden. Users of a well-defined class do not need to know how the class works; they just need to know how to use it.

Inheritance and Reuse


When the engineers at Acme Motors want to build a new car, they have two choices: They can start from scratch, or they can modify an existing model called Star. Perhaps their Star model is nearly perfect, but they want to add a turbocharger and a six-speed transmission. The chief engineer prefers not to start from the ground up, but rather to say, “Let’s build another Star, but let’s add these additional capabilities. We’ll call the new model a Quasar.” A Quasar is a kind of Star, but a specialized one with new features.

C++ supports inheritance. With inheritance, you can declare a new type that is an extension of an existing type. This new subclass is said to derive from the existing type and is sometimes called a derived type. If the Quasar is derived from the Star and, thus, inherits all the Star’s qualities, the engineers can add to them or modify those qualities as needed.

Polymorphism


A new Quasar might respond differently than a Star does when you press down on the accelerator. The Quasar might engage fuel injection and a turbocharger, whereas the Star simply lets gasoline into its carburetor. A user, however, does not have to know about these differences. He can just floor it, and the right thing happens, depending on which car he’s driving.

C++ supports the idea that different objects can be treated similarly and still do the right thing through what is called function polymorphism and class polymorphism. Poly means many, and morph means form. Polymorphism refers to the same name taking many forms.

No comments:

Post a Comment

Subscribe Via Email

Enter your email address:

Delivered by FeedBurner