Extra Credit Assignment for CSCI 431

Overview

In this assignment you will investigate the concept of polymorphism as expressed in the language C++. This assignment is worth 25 points that can be added as extra credit to your class grade. You are not required to do this assignment. If you choose to do this assignment, it must be turned in on the Monday immediately following Fall Break (Monday, Oct26); no late assignments will be accepted.

Part 1

Examine the template function square in the file lab4-2.cpp, this function squares its argument. Now examine the program in the file lab4-2.cpp. This program instantiates three instances of the generic function square. Note that the new functions are all called square (an example of overloading) and that the "squaring" operator is defined to be "*" for all data types except vectors. For vector data the square function uses the function CrossProduct.

Compile and test the program in lab4-2.cpp.

There is nothing to turn in for this part of the assignment.

Part 2

Write a function with the prototype:
   ostream & operator << (ostream &s, const vector &v)

for the vector data type. This is one of the more commonly overloaded functions since each data type needs its own routine for displaying values.

The output of the vector should be displayed with the format:

   <c1,c2,c3>

where c1, c2, and c3 are the components of the vector. For example, the vector v1 in lab4-2.cpp would be displayed as:

   <10,20,30>

Replace the code in lab4-2.cpp which displays the individual components of the vectors v1 and v2 using this new output function.

Turn in the modified source code for lab4-2 with your new function and the modified version of main.

Part 3

Implement a function to compute the length of integers, floats and vectors. Geometrically, the length of a number or vector is the distance from the origin. For integers and real numbers this is just the absolute value, for vectors, this is the square root of the sum of the squares of their components.

Create a new generic funstion called length in a fashion to similar to the square function. One difference is that the declaration of length requires two template parameters, one for the argument type and one for the return type. The generic declaration will therefore begin with:


   template <class argtype, class returntype>
   returntype length(argtype x)
   { return abs(x); }
Add the necessary functions, declarations, etc to the version of lab4-2 created in Part 2 to test the generic function length on integers, floats and vector data.

Turn in the modified source code for lab4-2 (including the modifications made in Part 2) with your new function and the modified version of main.

Part 4

As a final example of polymorphism in C++, you will work with a template class that implements a stack. Examine the code in the files stack.h and stack.cpp Note in the specification that the standard stack operations are provided. An important operation is the destructor. It is called when a stack is no longer needed in a program; its purpose is to allow for the reclamation of the memory space in the event that the implementation involves dynamically allocated variables. With a fixed array implementation, a destructor is not technically needed, but it is included for completeness.

The program lab4-2-1.cpp uses the generic stack to implement an algorithm for evaluating infix expressions. The expressions may use parenthesis but are restricted to the operation '*', '/', '+', and '-'. The operands must be of type float. Two stacks are used by the algorithm, a stack of operands (float) and a stack of operators (character).

Expressions are read from standard input in a format to simplify the input code. The expression:

   (20+30)/6.2

Is input as:
   C(
   N20
   C+ 
   N30
   C)
   C/
   N6.2
   E

There is one operand/operator/parenthesis per line. "C" precedes characters, "N" precedes numbers, and "E" indicates the end of the expression.

Add the code for push and pop to the template stack class.

Compile the stack class and make the changes required to fix all compilation errors.

Turn in the modified source code for lab4-2-1.cpp.