Information Technology Reference
In-Depth Information
Suppose now that you would like the same functionality for values of type float . There are
several ways you could achieve this. One way is to perform the following steps to produce the
subsequent code:
Cut and paste the code for class MyIntStack .
￿
￿
Change the class name to MyFloatStack .
Change the appropriate int declarations to float declarations throughout the class
declaration.
￿
class MyFloatStack // Stack for floats
{
int StackPointer = 0;
float [] StackArray; // Array of float
public void Push( float x ) // Input type: float
{
...
}
public float Pop() // Return type: float
{
...
}
...
}
This method works, but is error-prone, and has the following drawbacks:
￿
You need to inspect every part of the class carefully to determine which type declara-
tions need to be changed, and which should be left alone.
You need to repeat the process for each new type of stack class you need ( long , double ,
string , etc.).
￿
￿
After the process, you end up with multiple copies of nearly identical code, taking up
additional space.
￿
Debugging and maintaining the parallel implementations is inelegant and error-prone.
Search WWH ::




Custom Search