MVNU Computer Science II - Lab 8: Templates

Spring 2010


In this lab you are going to experiment with creating classes with templates.


Templates

Function templates

Read the Function Template section of Templates Tutorial

Experiment 1

Create a new template function MaxValue(...), which is passed and array and size, and returns the maximum value in the array.

To get you started, below is a proposed beginning:

template <class T> 
T MaxValue(T data[], int len) { 
  ... 
}

Write the function, then test it with:

  • An Integer Array: 3, 8, 33, 51, 43, 7, 9, 12
  • A Float Array: 3.5, 7, 16.78, 43.775 13, 3.14
  • A string array "MVNU", "CS", "C++", "Templates", "Linux"

Class Templates

Read the Class Template section of Templates Tutorial

Experiment 2

Consider the list class from Lab 6. Modify the list class so that it is a templated class, e.g. the type of information in the list is determined by the template.

Below is the start of such a class definition:

#include <iostream>; 
 using namespace std;
 template <class T>;
 class list { 
     private:  
           struct node { T data;  node *link } *p; 
 ... 

Next test this class by modifying your previous driver program to create and integer, float, and string version of the list. Put at least 5 elements in each list, and test every class fuction.

What to turn in on Moodle

The laboratory report should include the following elements:


1. Brief statement of the objectives of the lab and a description of what you did during the lab session (1/2 – 1 page).
2. Lab results such as program listings, input and/or output files, tables of data produced, answers to specific questions posed, etc. as appropriate to the lab assignment. If there were no results, indicate this with “No Results.”
3. Discussion of any difficulties you encountered and what you did to resolve them (1/2 – 1 page).
4. Discussion of what you learned or how the lab experience reinforced things you already knew (1/2 – 1 page).
5. Your overall rating of the lab on a scale of 1-4 and an explanation of your rating. Ratings are as follows:

1 = poor
2 = fair
3 = good
4 = excellent

Topic revision: r5 - 2011-05-12 - JimSkon
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback