(Latest Revision: Wed Apr 2 23:34:56 PST 2003 )
template <class T> class aClass { public: aClass(); aClass(T InitialData); void SetData(T NewData); T Data(); private: T TheData; }; // end class
#include "class.cpp"
/* Since class.h includes class.cpp, the include statement below has the effect of including both class.h and class.cpp. We are supposed to compile the program with a command such as: g++ main.cpp and not g++ main.cpp class.cpp Thus we do not allow separate compilation of main.cpp and class.cpp, but force all the source code to be compiled as one large file. Most compilers need this "help." */ #include "class.h" #include <iostream.h> int main() {
aClass<int> A; // TheData is an int aClass<double> B(4.8); //TheData is a double == 4.8 A.SetData(5); cout << B.Data() << endl; }
template <class T> aClass<T>::aClass() : TheData(0) /* Note that the compiler would not know what kind of code to generate to assign "0" to TheData if class.cpp were compiled independently from main.cpp. It would have to generate code that 'figures out' how to do the assignment at runtime. Because of this kind of problem, it makes the compiler's job easier when we include class.cpp in main.cpp. The compiler then knows it will be assigning an integer or real to TheData. */ { } // end default constructor template <class T> aClass<T>::aClass(T InitialData): TheData(InitialData) { } // end constructor template <class T> void aClass<T>::SetData(T NewData) { TheData = NewData; } // end SetData template <class T> T aClass<T>::Data() { return TheData; } // end Data