hey all,
i just learned templates in C++ and wanna know how to put them to practical use...
the following example will illustrate my doubt:
Code:
#include<iostream>
using namespace std;
template <class aType>
class com{
public:
com(aType x){
cout << x + 5;
}
};
int main(){
int a = NULL;
double b = NULL;
cout << "enter a number: ";
/* here wha' i wanna do is,
if the user enters a number,
i wanna store it in a... USING A CIN STATEMENT WHICH I HAVEN'T MENTIONED
if the user enters a number with a fraction store it in b... USING A CIN STATEMENT WHICH I HAVEN'T MENTIONED
HOW TO DO THAT IS MY QUESTION...
*/
// if i could do that then i could practically use templates in real programs
//like:
if(a == NULL && b != NULL){
com <double> x(b);
}else if(a != NULL && b == NULL){
com <int> x(a);
}else{
cout << "Sorry there was an error...";
}
}