INTRO TO COMPUTER SCIENCE II
TEMPLATES
CS162
Program 4
Hunt the Wumpus
Text based adventure game made in 1973
HuntTheWumpus-VideoGameHistoryProject
Go through a series of caves with hazards to find and kill the Wumpus and
escape with the gold
Use polymorphism and template class (vector)
Two modes: Normal and debug Hint:Buildthedebugversionfirst!
Last Time…Exceptions
Function Templates
Functions and classes are useful tools, but can be limiting due to needing to specify all of the parameter types
If you have a general algorithm for a function that doesn’t change, but just the possible types change, it is best to make a function template
Minimizesduplicatecode
Function templates are functions that serve as a pattern for
int min(int x, int y){
return (x < y) ? x : y;
}
T min(T x, T y){
return (x < y) ? x : y;
}
Conditional ternary operator
creatingothersimilarfunctions
Define the function using placeholder types, aka template type parameters
Function Templates
Change defined types to template types
Generally the letter “T” is used for C++
Needs a template parameter declaration to compile
Known as template prefix
Syntax: template
If multiple template type parameters, separate by comma
template
template
const T& min(const T& x, const T& y){
if (x < y)
return x;
else
return y;
}
template
void swap(T& v1, T& v2){
T temp;
temp = v1;
v1 = v2;
v2 = temp;
}
Function Templates
Compilers don’t actually produce definition for every type Only for each type that uses the template in the program Called the function template instance
Template functions will work with both built-in datatypes and classes
If using your own datatypes, you must define any operators or function calls used by template or you will get a compile error Wouldneedtooverload“>”operatorformaxfunctiontoworkwithPointobjects
template
const T& min(const T& x, const T& y){
return (x > y) ? x : y;
}
Function Templates
Pros
Reducecodemaintenance Canbesafer
Cons
Maynotworkperfectlywitholdercompilers Errormessageshardertoread
Increasecompiletimeandcodesize
Due to compiler inconsistency, you need to define the template in the same file it is invoked
}
//prog.cpp
#include
#include “point.h”
template
const T& min(const T& x, const T& y){
return (x < y) ? x : y;
}
int main(){
int i = min(3, 9);
double d = min(3.14, 2.718);
return 0;
Template Classes
Works the same way as templated functions All functions within the class will operate on
the provided types
Scope with ClassName
For the compiler to use a template, it must see both the template definition and declaration in the same file
Commonworkaroundistoputbothina“.hpp”file instead of separating into “.cpp” and “.h”
//custom_array.hpp
#ifndef CUSTOM_ARRAY_H #define CUSTOM_ARRAY_H
template
private:
int m_length; T *m_data;
public:
Custom_Array(){} Custom_Array(int length){} ~Custom_Array(){}
T& operator[](int index){} };
template
int Custom_Array
return m_length; }
#endif
Template Classes – Vector
Standard library has a wide variety of template classes StandardTemplateLibrary(STL)
VectorisformedfromatemplateclassintheStandardTemplate Library
Arrayscangrowandshrinkwhiletheprogramisrunning
Has a base type and stores collections of this base type
Still starts indexing at zero, can still use hard brackets to access things
Use push_back to add one element at the end
Numberofelements==size
Capacity==howmuchmemoryiscurrentlyallocated
Reference: http://www.cplusplus.com/reference/vector/vector/
#include
#include
Using namespace std;
int main(){
vector
for (int i=0; i<6; ++i)
v.push_back(10-i);
for (int i=0; i