03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

206 CHAPTER 7. EFFECTIVE PROGRAMMING: THE POLYMORPHIC WAY<br />

7.3 Programming with Objects<br />

Programming with objects may be viewed as an evolution from the structured imperative<br />

paradigm. It, on the one hand, tries to address the issue of code reuseability by providing a<br />

specific type of polymorphism, sub-typing. On the other hand it addresses the issue of unchecked<br />

modification of state by en<strong>for</strong>cing data encapsulation, thus en<strong>for</strong>cing changes through defined<br />

interfaces. Both of these notions are attached to an entity called an object. There<strong>for</strong>e an object<br />

serves as a self contained unit which interacts with the environment via messages. It thus<br />

accomplishes a decoupling of the internal implementation within the object and the interaction<br />

with the sourrounding environment. Thus en<strong>for</strong>cing (clean) interfaces, which is essential <strong>for</strong><br />

effective programming. The algorithms are expressed much more by the notion of what is to be<br />

done as an interaction and modification of objects, where the details of how, are encapsulated<br />

to a great extent within the objects themselves.<br />

Another benefit <strong>for</strong> programming with objects is, that these entities can be placed in libraries.<br />

This saves the ef<strong>for</strong>t of continually rewriting the same code <strong>for</strong> every new program. Furthermore,<br />

because objects can be made polymorphic, object libraries offer the programmer more flexibility<br />

and functionality than subroutine libraries (their counterparts in the procedural paradigm).<br />

Technically, object libraries are quite feasible, and the advantages of extensibility can be significant.<br />

However, the real challenge to making code reusable is not technical. Rather, it is<br />

identifying functionality that other people both understand and want. People who use procedural<br />

languages have been writing and using subroutine libraries <strong>for</strong> decades. These libraries are<br />

most successful when they per<strong>for</strong>m simple, clearly defined functions, such as calculating square<br />

roots or computing trigonometric functions. An object library can provide complex functions<br />

more easily than a subroutine library. However, unless those functions are clearly defined, well<br />

understood and generally useful, the library is unlikely to be used widely.<br />

To give an intuitive specification of the programming approach with objects, the following list<br />

specifies different points in the object world:<br />

• Identity is the quantization of data in discrete, distinguishable entities called objects<br />

• Classification is the grouping of objects with the same structure and behavior into classes<br />

• Polymorphism is the differentiation of behavior of the same operation on different classes<br />

• Inheritance is the sharing of structure and behavior among classes in a hierarchical relationship<br />

But one of the biggest problems of this programming approach is the interaction of objects with<br />

algorithms. The problem can easily be seen using the example of a simple sorting algorithm.<br />

Should the algorithm be placed into the object? Should an algorithm work on a class hierarchy<br />

with a common interface?<br />

The problem cannot be solved easily within this paradigm. A possible solution is some kind of<br />

polymorphism, which is explained in Section 7.5.2.<br />

7.3.1 Object-Based Programming<br />

In languages which support identity and classification the object-based paradigm can be used<br />

efficiently.

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

Saved successfully!

Ooh no, something went wrong!