Thankyou kmdv,
You answered what exactly i was asking...
i made a sample program using the things i understood;
Code:
include <iostream>
using namespace std;
template <typename T>
class Alpha;
template <>
class Alpha <int>{
public:
Alpha(){
cout << "INTEGER";
}
};
template <>
class Alpha <double>{
public:
Alpha(){
cout << "DOUBLE";
}
};
int main(){
Alpha <int> a;
Alpha <double> b;
}
this program should display: INTEGERDOUBLE
one more thing. what i deduced from the lesson is that by using the "template <>" thing, you can cause the template to work in different ways for different datatypes... while normal templates work for all data types but they work the same everytime...
i just wanna know if that assumption is correct...
i also found an alternate method to make your template work in different ways depending on the datatype
here it is:
Code:
#include <iostream>
using namespace std;
template <typename T>
class Alpha{
public:
Alpha(){
if(sizeof(T) == sizeof(int)){
cout << "INTEGER\n";
}else if(sizeof(T) == sizeof(double)){
cout << "DOUBLE\n";
}else if(sizeof(T) == sizeof(char)){
cout << "CHARACTER\n";
}else if(sizeof(T) == sizeof(float)){
cout << "FLOAT\n";
}else{
cout << "DUNNO!";
}
}
};
int main(){
Alpha <int> a1;
Alpha <double> a2;
Alpha <char> a3;
Alpha <float> a4;
Alpha <short> a5;
}
i just wanna know which way is a better way..