- Create two files
- The class definition goes in list.hpp file
- Implementation of member functions go in list.cpp
- This distinction is important because the user of the data
structure only should be looking at (#including) the HPP file
- CPP file is NORMALLY included in the project
- For templates, this doesn't work.
- You need to #include CPP instead of HPP
- templates are not translatable code.
- the code can be generated only when the values of the parameters
to the template are known
- list<int,20> will allow compile to create a type based
on the template of list.
- list.hpp
template<class item_type,int max_size>
class list
{
item_type a[max_size];
int size,cursor;
public:
list();
boolean is_empty();
boolean is_full();
void reset();
boolean end_of_list();
void advance();
item_type current_item();
void insert_before(item_type some_item);
void insert_after(item_type some_item);
void erase();
~list();
};
- CPP
- implementation of member functions
template<class item_type,int max_size>
list<item_type,max_size>::list()
{
size = 0;
cursor = 1;
}
template<class item_type,int max_size>
boolean list<item_type,max_size>::is_empty()
{
return size == 0;
}