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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4.5. INHERITANCE OR GENERICS? 99<br />

For illustration purpose we like to show the conceptualized implementation of a generic sorting<br />

function as used in the library of the concept compiler:<br />

template<br />

requires LessThanComparable<br />

&& CopyAssignable<br />

&& Swappable<br />

&& CopyConstructible<br />

inline void sort(Iter first, Iter last);<br />

If the function is called erroneously, the compiler will detect this directly in the function call<br />

not deep inside its implementation.<br />

4.5 Inheritance or Generics?<br />

In this section we will discuss the commonalities and difference of/between object-oriented<br />

programming (OOP) and generic programming. People that do not know OOP so far will not<br />

learn it in this section. The purpose of this section is to motivate why we pay more attention<br />

to generic than to object-oriented programming in this book. The short answer is per<strong>for</strong>mance<br />

and applicability. If this answer is good enough <strong>for</strong> you, you can skip this section and continue<br />

with the next one. Programmers that are used to OOP and think they can implement the<br />

functionality with inheritance instead of templates should take the time and read this section.<br />

Inheritance and generic programming are similar in the sense that most programming problems<br />

that can be solved by inheritance have a generic alternative solution and vice versa. The<br />

following table summarizes the basic components of inheritance and the corresponding building<br />

blocks of generic programming:<br />

Inheritance Generic Programming<br />

base class concept<br />

derived class model<br />

In the remainder of this section we will discuss the differences between generic programming<br />

and inheritance.<br />

We will focus on functions, but similar arguments hold <strong>for</strong> templated classes. The advantage<br />

of using a base class reference or pointer as argument type of a function is that we are sure<br />

that all derived classes can be used as argument too, see § ??. 6 Inheritance in C ++ and other<br />

OOP languages is designed in a fashion that a function in a derived class can substitute (hide)<br />

the one in the base class with the identic signature. Thus calling the function <strong>for</strong> a base class<br />

argument will either use the base class’s implementation or those of the derived class (if the<br />

function is virtual). In both cases we can rely on the existance. We will explain OOP in more<br />

detail in Section ??. Here, we only name advantages and disadvantages of the two approaches<br />

regarding different aspects of programming.<br />

Compile time: With the OOP approach, function is only compiled once. The distinction<br />

between the different calculations is realized at run-time. The generic implementation requires<br />

6 TODO: OOP section is still not written yet.

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

Saved successfully!

Ooh no, something went wrong!