Monday, November 29, 2010

Characteristics of Object-Oriented Languages


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. This
process is described in detail in Chapter 16, “Object-Oriented Software
Development.”

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
The Big Picture

• 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++ (as we’ll see in Chapter 3,
“Loops and Decisions”). 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, as shown in Figure 1.5. A
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 int doesn’t create any variables.
A class is thus a description of a number of similar objects. This fits our non-technical understanding
of the word class. Prince, Sting, and Madonna are members of the rock musician
class. There is no one person called “rock musician,” but specific people with specific names
are members of this class if they possess certain characteristics. An object is often called an
“instance” of a class.
Inheritance
The idea of classes leads to the idea of inheritance. In our daily lives, we use the concept
of classes divided into subclasses. We know that the animal class is divided into
mammals, amphibians, insects, birds, and so on. The vehicle class is divided into cars,
trucks, buses, motorcycles, and so on.

Reusability
Once a class has been written, created, and debugged, it can be distributed to other
programmers for use in their own programs. This is called reusability. It is similar to
the way a library of functions in a procedural language can be incorporated into different
programs.
However, in OOP, the concept of inheritance provides an important extension to the idea of
reusability. A programmer can take an existing class and, without modifying it, add additional
features and capabilities to it. This is done by deriving a new class from the existing one. The
new class will inherit the capabilities of the old one, but is free to add new features of its own.
For example, you might have written (or purchased from someone else) a class that creates a
menu system, such as that used in Windows or other Graphic User Interfaces (GUIs). This
class works fine, and you don’t want to change it, but you want to add the capability to make
some menu entries flash on and off. To do this, you simply create a new class that inherits all
the capabilities of the existing one but adds flashing menu entries.
The ease with which existing software can be reused is an important benefit of OOP. Many
companies find that being able to reuse classes on a second project provides an increased
return on their original programming investment. We’ll have more to say about this in later
chapters.

Creating New Data Types
One of the benefits of objects is that they give the programmer a convenient way to
construct new data types. Suppose you work with two-dimensional positions (such as
x and y coordinates, or latitude and longitude) in your program. You would like to
express operations on these positional values with normal arithmetic operations,
such as
position1 = position2 + origin
where the variables position1, position2, and origin each represent a pair of independent
numerical quantities. By creating a class that incorporates these two values,
and declaring position1, position2, and origin to be objects of this class, we can,
in effect, create a new data type. Many features of C++ are intended to facilitate the
creation of new data types in this manner.
Polymorphism and Overloading
Note that the = (equal) and + (plus) operators, used in the position arithmetic shown
above, don’t act the same way they do in operations on built-in types such as int. The
objects position1 and so on are not predefined in C++, but are programmer-defined

objects of class Position. How do the = and + operators know how to operate on
objects? The answer is that we can define new behaviors for these operators. These
operations will be member functions of the Position class.
Using operators or functions in different ways, depending on what they are operating on, is
called polymorphism (one thing with several distinct forms). When an existing operator, such
as + or =, is given the capability to operate on a new data type, it is said to be overloaded.
Overloading is a kind of polymorphism; it is also an important feature of OOP

No comments:

Post a Comment