Select this to skip to main content [CSUSB] >> [CNS] >> [Comp Sci Dept] >> [R J Botting] >> [CSci202] >> lab09
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Grading]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16]
Mon May 21 16:59:32 PDT 2007

CSci 202 Lab 9: Templates [Zemoudeh]

Goal

To improve the expression evaluation program of lab08 so that 1/2 is 0.5 not 0! To learn about using template classes.

  1. In this laboratory you will learn how to work with generic or parameterized classes by using the C++ template feature:
    See C++ templates.

  2. Here [tstack.h] is the template version of Stack.h. If this file is #included in your program the compiler can create a stack of any type of object by filling in the blanks. The blank is called T in this code. The T template parameter can be replaced by any type name: double, int, float, short, long, char,string, or even a class that you have created yourself.

    You will also need the reverse.cpp and exprEval.cpp programs from the last lab. You probably won't need the makefile.

    Note member function implementations are included in this file. This is because the file defines templates. It is difficult for normal loaders to link compiled code correctly when the original source code defines a template. The sizes of all objects are needed to generate a linkable *.o file. This is not possible with all templates because the size of the objects often depend on the actual data type used in place of T.

  3. Rewrite your reverse.cpp program to #include "tstack.h" instead of #include "stack.h". You will then need to change one declaration to declare a Stack<char> (a stack of chars) instead of a "Stack".

    Note I have removed the word 'template' from somewhere in tstack.h. Use the compiler to find where and fix it. A simple compilation like

    		g++ -o reverse reverse.cpp
    
    should work because program because tstack.h uses inline code. Use the command above to correct your program and tstack.h. You can then test the program with this command with
    		./reverse
    

  4. We will now make our lab08 calculator work with floating point numbers so that
    		1 / 2 =
    
    gives the answer 0.5.

    Start by copying Evalexpr.cpp to a new file called ev.cpp by using this UNIX command:

    		cp evalExpr.cpp ev.cpp
    
  5. Rework ev.cpp program from lab08 so that it uses tstack.h to do double-length floating point calculations. It will need a Stack of double operands and a stack of intoperators. A simple compilation command like
    		g++ -o ev ev.cpp
    
    can be used.

    Hint. We used a function atoi to convert a char* into an int. There is a similar function in the same library called atof which converts ASCII into floating point numbers. You will find it very helpful in this lab.

  6. Perhaps you can also shift the int stack to a stack of chars. The program might even get simpler. If you have time explore this.
  7. If you still have time, explore the possibility of not using spaces to delimit tokens.


Kay Zemoudeh
2/16/1998
Dick Botting
11/15/2004
05/21/2007