I have written this code using templates which works very perfectly well. I am a beginner in using templates, so I really want someone to check if its written in a correct format. Because when I am deleting template<elemType> from the beginning of each function and main, the code is still working perfectly. So is it fine or is there something wrong? Please let me know since I have an exam related to it on Saturday.
Many Thanks
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
template<class elemType>
class revElement
{
public:
void reverseInt();
void reverseString();
};
template<class elemType>
void revElement::reverseInt()
{
vector<int>intList;
vector<int>::iterator i;
intList.push_back(1);
intList.push_back(2);
intList.push_back(3);
intList.push_back(4);
intList.push_back(5);
reverse(intList.begin(),intList.end());
cout<<"The reversed integers are: "<<endl;
for(i=intList.begin();i!=intList.end();++i)
{
cout<<" "<<*i;
}
}
template<class elemType>
void revElement::reverseString()
{
vector<string>stringList;
vector<string>::iterator s;
stringList.push_back("This");
stringList.push_back("is");
stringList.push_back("my");
stringList.push_back("C++");
stringList.push_back("Lab");
reverse(stringList.begin(),stringList.end());
cout<<"The reversed strings are: "<<endl;
for(s=stringList.begin();s!=stringList.end();++s)
{
cout<<" "<<*s;
}
}
template<class elemType>
int main()
{
revElement r;
r.reverseInt();
cout<<endl;
cout<<endl;
r.reverseString();
getch();
return 0;
}