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.

7.5. FROM MONOMORPHIC TO POLYMORPHIC BEHAVIOR 213<br />

7.5.1 Ad-hoc Polymorphism<br />

This kind of polymorphic behavior is expressed with ad-hoc, which should point out, that this<br />

kind of behavior is locality. Common to these two types (overloading and coercion) is the fact<br />

that the programmer has to specify exactly what types are to be usable with the polymorphic<br />

function.<br />

Overloading<br />

Is a simple convenient way of programming, to ease the programmer’s life.<br />

class my stack<br />

{<br />

virtual bool push(int ..) {}<br />

virtual bool push(double ..) {}<br />

virtual bool push(complex ..) {}<br />

virtual int pop() {..}<br />

virtual double pop() {..}<br />

// ....<br />

};<br />

Coercion<br />

Coercion is automatic type conversion. The following stack example can be used with all<br />

numerical data types, which can be converted to double:<br />

class my stack<br />

{<br />

virtual bool push(double ..) {}<br />

virtual double pop() {..}<br />

// ....<br />

};

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

Saved successfully!

Ooh no, something went wrong!