CS210 Lab: Templates Prelab


Overview

This prelab exercise tests your knowledge of function overloading.

For a review, click here to go to "CS115 C++ Overloading Lab".

In summary, you can define functions with the same name but with different parameter types or different number of parameters. When you do this, you can say that you have overloaded a function.

For instance, given the following prototypes,
void timesTwo(int &num);
void timesTwo(double &num);
you can see that the function timesTwo is overloaded (because the first prototype has an int parameter and the second has a double parameter).

Prelab Exercise:

  1. Given a function prototype:
    void stradd(char *s1, char *s2); 
    

    Which of the following overload stradd?

    1. void stradd(char *s1, int i);
    2. void sstradd(int i, int j);
    3. void stradd(char *s1, char *s2, int i);
    4. void stradd(int i, int j);
    5. void stradd3(char *s1, char *s2, char *s3);

  2. Given the following function, which calculates the average of an array of numbers of type int:
    int average(int array[], int size);
    {
    	int total=0;   			//set total to 0
    	for (int j=0; j<size; j++) 	//for every array member,
    		total+=array[j];	//add it to total
    	return total/size;		//average (as integer)
    }
    
    Overload the function so that it averages arrays of type long.

For Answers, click here
Back to Templates Lab click here

CS Dept Home Page
CS Dept Class Files
CS210 Class Files

Copyright: Department of Computer Science, University of Regina.