Feb 18, 2013 at 11:25pm UTC
This is my .h file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#ifndef CONTAINER3_H
#define CONTAINER3_H
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include "item.h"
#include "auto.h"
template <class part2>
class Container3
{
public :
part2 Container3();
void add_item(part2 item);
part2 numberofitems();
part2 displayitems();
void removeitems(part2 id);
private :
part2 _items[100];
part2 _count;
};
#endif
template <class part2>
part2 Container3<part2>::Container3()
{
_count=0;
}
template <class part2>
void Container3<part2>::add_item(part2 item)
{
_items[_count]= item;
_count++;
}
template <class part2>
part2 Container3<part2>::displayitems()
{
for (int i=0; i < _count; i++)
{
cout<<_items[i].toString()<<endl;
}
}
template <class part2>
void Container3<part2>::removeitems(part2 id)
{
for (int i=0; i < _count; i++)
{
if (id==_items[i].getId())
{
_items[i] = _items[i+1];
_count--;
}
}
}
template <class part2>
part2 Container3<part2>::numberofitems()
{
cout<<endl<<"Number of items equals: " <<_count<<endl;
}
and my main.cpp file...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
#include <string>
#include "auto.h"
#include "item.h"
#include "container.h"
#include "container2.h"
#include "container3.h"
template <class part2>;
using namespace std;
int main()
{
//Container3 <Auto> c; ... both Auto and Item are classes
Container3<Item> c;
system("PAUSE" );
return 0;
}
I'm having trouble understanding how to use the methods using the template, please help.
Last edited on Feb 18, 2013 at 11:28pm UTC
Feb 19, 2013 at 12:25am UTC
why you use
template <class part2>;
top of using namespace std;
template has for classes or functions that we don't know type of it
like vector class it's good way for Software Engineering and reuse your app in other projects
Last edited on Feb 19, 2013 at 12:29am UTC