============================MODULE24======================================= | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Originally programs compiled using Borland C++. Examples compiled using VC++/VC++ .Net and gcc or g++ are given at the end of every Module. For example if you want to compile C++ codes using VC++/VC++ .Net, change the header file accordingly. Just need some modification for the header files...: ------------------------------------------------- #include //for system() #include ... { C++ codes... } ------------------------------------------------- should be changed to: ------------------------------------------------- #include //use C++ wrapper to call C functions from C++ programs... #include using namespace std; ... { C++ codes... } ------------------------------------------------- In VC++/VC++ .Net the iostream.h (header with .h) is not valid anymore. It should be C++ header, so that it comply to the standard. In older Borland C++ compiler this still works, but not proper any more... and for standard C/C++ the portability should be no problem or better you read Module23 at http://www.tenouk.com/Module23.html to get the big picture...For C codes, they still C codes :o) ========================================================================= =======================BUT HERE ALL C++ codes============================ #include using namespace std; //function template declaration and definition template any_data_type MyMax(any_data_type Var1, any_data_type Var2) { return Var1> Var2 ? Var1:Var2; } int main() { cout<<"MyMax(10,20) = "< class MyStack { private: //number of the stack’s element int size; //top position int top; //data type pointer any_data_type* StackPtr; public: //constructor... MyStack(int =10); //destructor... ~MyStack(){delete [] StacKPtr;} //put in data... int push(const any_data_type&); //take out data... int pop(any_data_type&); //test the emptiness... int IsEmpty() const {return top == -1;} //test the fullness... int IsFull() const {return top == size – 1;} }; ------------------------------------------------------------------------------------- //Another example of the template declaration: template class Vector { private: any_data_type *buffer; //copy constructor Vector (const Vector &Var1) //overloaded assignment operator Vector& operator=(const Vector& Var2) //destructor ~Vector(); //other member functions… any_data_type& operator [ ] (unsigned int index); const any_data_type& operator [ ] (unsigned int index) const; } ---------------------------------------------------------------------------------------- //simple class template program example //------declaration and definition part-------- template class MyStack { private: //number of the stack's element int size; //top position int top; //data type pointer any_data_type* StackPtr; public: //constructor... MyStack(int =10); //destructor... ~MyStack(){delete [] StacKPtr;} //put in data... int push(const any_data_type&); //take out data... int pop(any_data_type&); //test the emptiness... int IsEmpty() const {return top == -1;} //test the fullness... int IsFull() const {return top == size - 1;} }; //----the main() program-------- int main() { return 0; } ---------------------------------------------------------------------------------------------- //normal class class MyClass { //... //but, have template member function... template void MemberFunct(any_data_type) {}; }; int main() { return 0; } ------------------------------------------------------------------------------------------------ template class MyClass { //... //nested class template template class NestedClass {}; //... }; int main() { return 0; } ------------------------------------------------------------------------------------------------- #include using namespace std; //-------class template declaration part--- //-------test.h----- template class Test { public: //constructor Test(); //destructor ~Test(); //function template any_data_type Data(any_data_type); }; template any_data_type Test::Data(any_data_type Var0) {return Var0;} //------class template definition part-------- //----should be in the same header file with-- //----the class template declaration------ //constructor template Test::Test() {cout<<"Constructor, allocate..."< Test::~Test() {cout<<"Destructor, deallocate..."< Var1; Test Var2; Test Var3; Test Var4; cout<<"\nOne template fits all data type..."< using namespace std; //-------class template declaration part--- //-------test.h file----- template class Test { public: //constructor Test(); //destructor ~Test(); //function template any_data_type Data(any_data_type); }; template any_data_type Test::Data(any_data_type Var0) {return Var0;} //------class template definition part-------- //----should be in the same header file with-- //----the class template declaration------ //constructor template Test::Test() {cout<<"Constructor, allocate..."< Test::~Test() {cout<<"Destructor, deallocate..."< Var1; Test Var2; Test Var3; Test Var4; cout<<"\nOne template fits all data type..."< using namespace std; template class Test { public: //constructor Test(){}; //destructor ~Test(){}; //member function templates... any_data_type Funct1(any_data_type Var1) {return Var1;} any_data_type Funct2(any_data_type Var2) {return Var2;} }; //do some testing int main() { //Implicit instantiation generates class Test... Test Var1; //Implicit instantiation generates class Test... Test Var2; cout<<"Implicit instantiation..."<::Funct1() cout<<"Var1 = "<::Funct2() cout<<"Var2 = "< using namespace std; template class Test { public: //constructor Test(){}; //destructor ~Test(){}; //member functions... any_data_type Funct1(any_data_type Var1) {return Var1;} any_data_type Funct2(any_data_type Var2) {return Var2;} }; //explicit instantiation of class Test template class Test; //explicit instantiation of class Test template class Test; //do some testing int main() { Test Var1; Test Var2; cout<<"Var1 = "< #include //for strcmp() #include using namespace std; //primary template, for all type template any_data_type MyMax(const any_data_type Var1, const any_data_type Var2) { cout<<"Primary template..."< const char *MyMax(const char *Var1, const char *Var2) { cout<<"Specialization..."< using namespace std; //general, justice for all type:-) template any_data_type Test(any_data_type Var1) {return Var1;} //partial specialization for all pointers type template any_data_type * Test(any_data_type *Var2) {return Var2;} //specialization, just for const char * template <> const char * Test(const char *Var3) {return Var3;} //do some testing int main() { int p = 5; //calls Test(any_data_type int q = Test(p); double r = Test(3.1234); cout<<"General types = "< const char *MyMax(const char *Var1, const char *Var2) { cout<<"Specialization..."<