来着:http://blog.csdn.net/nuoshueihe/article/details/8238553
template class with type parameter
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- const int SIZE = 10;
- template <class T>class MyClass {
- T a[SIZE];
- public:
- MyClass(void)
- {
- int i;
- for(i=0; i<SIZE; i++)
- a[i] = i;
- }
- T &operator[](int i);
- };
- template <class T> T &MyClass<T>::operator[](int i)
- {
- if(i<0 || i> SIZE-1)
- {
- cout << endl << "Index value of ";
- cout << i << " is out of bounds." << endl;
- }
- return a[i];
- }
- int main(void)
- {
- MyClass<int> int_array;
- MyClass<double> double_array;
- int i;
- cout << "Integer array: ";
- for(i=0; i<SIZE; i++)
- int_array[i] = i;
- for(i=0; i<SIZE; i++)
- cout << int_array[i] << " ";
- cout << endl;
- cout << "Double array: ";
- cout.precision(2);
- for(i=0; i<SIZE; i++)
- double_array[i] = (double)i/3;
- for(i=0; i<SIZE; i++)
- cout << double_array[i] << " ";
- cout << endl;
- int_array[12] = 100; // Calls overloaded array operator
- }
#include <iostream>
#include <stdlib.h>
using namespace std;
const int SIZE = 10;
template <class T> class MyClass {
T a[SIZE];
public:
MyClass(void)
{
int i;
for(i=0; i<SIZE; i++)
a[i] = i;
}
T &operator[](int i);
};
template <class T> T &MyClass<T>::operator[](int i)
{
if(i<0 || i> SIZE-1)
{
cout << endl << "Index value of ";
cout << i << " is out of bounds." << endl;
}
return a[i];
}
int main(void)
{
MyClass<int> int_array;
MyClass<double> double_array;
int i;
cout << "Integer array: ";
for(i=0; i<SIZE; i++)
int_array[i] = i;
for(i=0; i<SIZE; i++)
cout << int_array[i] << " ";
cout << endl;
cout << "Double array: ";
cout.precision(2);
for(i=0; i<SIZE; i++)
double_array[i] = (double)i/3;
for(i=0; i<SIZE; i++)
cout << double_array[i] << " ";
cout << endl;
int_array[12] = 100; // Calls overloaded array operator
}
template class with generic parameter
- #include <iostream>
- using namespace std;
- template<class T>class Distance {
- public:
- Distance(T distance);
- void show_distance(void) {
- cout << "The distance is " << distance <<" miles\n";
- };
- private:
- T distance;
- };
- template<class T>
- Distance<T>::Distance(T distance) { Distance::distance = distance; };
- int main(void)
- {
- Distance<int> short_distance(100);
- Distance<long> long_distance(2000000L);
- short_distance.show_distance();
- long_distance.show_distance();
- }
#include <iostream>
using namespace std;
template<class T> class Distance {
public:
Distance(T distance);
void show_distance(void) {
cout << "The distance is " << distance << " miles\n";
};
private:
T distance;
};
template<class T>
Distance<T>::Distance(T distance) { Distance::distance = distance; };
int main(void)
{
Distance<int> short_distance(100);
Distance<long> long_distance(2000000L);
short_distance.show_distance();
long_distance.show_distance();
}
Demonstrate a very simple safe pointer class.
- #include <iostream>
- #include <string>
- using namespace std;
- // The exception type thrown by the safe pointer.
- class bad_ptr {
- public:
- string msg;
- bad_ptr(string str) { msg = str; }
- };
- // A class used to demonstrate the safe pointer.
- class myclass {
- public:
- int alpha;
- int beta;
- myclass(int p, int q) { alpha = p; beta = q; }
- };
- template <class T>class safe_ptr {
- T *ptr;
- public:
- safe_ptr() { ptr = 0; }
- T *operator->() {
- if(!ptr != 0) throw bad_ptr("Attempt to use -> on null pointer.");
- else return ptr;
- }
- T &operator*() {
- if(!ptr) throw bad_ptr("Attempt to dereference null pointer.");
- else return *ptr;
- }
- operator T *() { return ptr; }
- T *operator=(T *val) { ptr = val; return ptr; }
- };
- int main()
- {
- safe_ptr<int> intptr;
- try {
- *intptr = 23;
- cout << "The value pointed to by intptr is: " << *intptr << endl;
- } catch(bad_ptr bp) {
- cout << bp.msg << endl;
- }
- intptr = new int;
- try {
- *intptr = 23;
- cout << "The value pointed to by intpr is: " << *intptr <<"\n\n";
- } catch(bad_ptr bp) {
- cout << bp.msg << endl;
- }
- safe_ptr<myclass> mcptr;
- try {
- mcptr = new myclass(100, 200);
- cout << "The values of alpha and beta for mcptr are: "
- << mcptr->alpha << " and " << mcptr->beta << endl;
- mcptr->alpha = 27;
- cout << "New value for mcptr->alpha: " << mcptr->alpha << endl;
- cout << "Same as (*mcptr).alpha: " << (*mcptr).alpha << endl;
- mcptr->beta = 99;
- cout << "New value for mcptr->beta: " << mcptr->beta <<"\n\n";
- } catch(bad_ptr bp) {
- cout << bp.msg << endl;
- }
- safe_ptr<myclass> mcptr2;
- try {
- mcptr2->alpha = 88;
- } catch(bad_ptr bp) {
- cout << bp.msg << endl;
- }
- delete intptr;
- delete mcptr;
- return 0;
- }
#include <iostream>
#include <string>
using namespace std;
// The exception type thrown by the safe pointer.
class bad_ptr {
public:
string msg;
bad_ptr(string str) { msg = str; }
};
// A class used to demonstrate the safe pointer.
class myclass {
public:
int alpha;
int beta;
myclass(int p, int q) { alpha = p; beta = q; }
};
template <class T> class safe_ptr {
T *ptr;
public:
safe_ptr() { ptr = 0; }
T *operator->() {
if(!ptr != 0) throw bad_ptr("Attempt to use -> on null pointer.");
else return ptr;
}
T &operator*() {
if(!ptr) throw bad_ptr("Attempt to dereference null pointer.");
else return *ptr;
}
operator T *() { return ptr; }
T *operator=(T *val) { ptr = val; return ptr; }
};
int main()
{
safe_ptr<int> intptr;
try {
*intptr = 23;
cout << "The value pointed to by intptr is: " << *intptr << endl;
} catch(bad_ptr bp) {
cout << bp.msg << endl;
}
intptr = new int;
try {
*intptr = 23;
cout << "The value pointed to by intpr is: " << *intptr << "\n\n";
} catch(bad_ptr bp) {
cout << bp.msg << endl;
}
safe_ptr<myclass> mcptr;
try {
mcptr = new myclass(100, 200);
cout << "The values of alpha and beta for mcptr are: "
<< mcptr->alpha << " and " << mcptr->beta << endl;
mcptr->alpha = 27;
cout << "New value for mcptr->alpha: " << mcptr->alpha << endl;
cout << "Same as (*mcptr).alpha: " << (*mcptr).alpha << endl;
mcptr->beta = 99;
cout << "New value for mcptr->beta: " << mcptr->beta << "\n\n";
} catch(bad_ptr bp) {
cout << bp.msg << endl;
}
safe_ptr<myclass> mcptr2;
try {
mcptr2->alpha = 88;
} catch(bad_ptr bp) {
cout << bp.msg << endl;
}
delete intptr;
delete mcptr;
return 0;
}
template extending
- template <typename T>
- class SimpleTemplate
- {
- public:
- SimpleTemplate(T &inObject);
- const T& get();
- void set(T& inObject);
- protected:
- T& mObject;
- };
- template <typename T>
- SimpleTemplate<T>::SimpleTemplate(T &inObject) : mObject(inObject)
- {
- }
- template <typename T>
- const T& SimpleTemplate<T>::get()
- {
- return mObject;
- }
- template <typename T>
- void SimpleTemplate<T>::set(T& inObject)
- {
- mObject = inObject;
- }
- #include <iostream>
- #include <string>
- using namespace std;
- int main(int argc,char** argv)
- {
- int i = 7;
- SimpleTemplate<int> intWrapper(i);
- i = 2;
- cout << "wrapper value is " << intWrapper.get() << endl;
- string str = "test";
- SimpleTemplate<string> stringWrapper(str);
- str += "!";
- cout << "wrapper value is " << stringWrapper.get() << endl;
- }
template <typename T>
class SimpleTemplate
{
public:
SimpleTemplate(T &inObject);
const T& get();
void set(T& inObject);
protected:
T& mObject;
};
template <typename T>
SimpleTemplate<T>::SimpleTemplate(T &inObject) : mObject(inObject)
{
}
template <typename T>
const T& SimpleTemplate<T>::get()
{
return mObject;
}
template <typename T>
void SimpleTemplate<T>::set(T& inObject)
{
mObject = inObject;
}
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
int i = 7;
SimpleTemplate<int> intWrapper(i);
i = 2;
cout << "wrapper value is " << intWrapper.get() << endl;
string str = "test";
SimpleTemplate<string> stringWrapper(str);
str += "!";
cout << "wrapper value is " << stringWrapper.get() << endl;
}
sequence template
- #include <iostream>
- using namespace std;
- template <class T,int N>
- class sequence {
- T memblock [N];
- public:
- void setmember (int x, T value);
- T getmember (int x);
- };
- template <class T,int N>
- void sequence<T,N>::setmember (int x, T value) {
- memblock[x]=value;
- }
- template <class T,int N>
- T sequence<T,N>::getmember (int x) {
- return memblock[x];
- }
- int main () {
- sequence <int,5> myints;
- sequence <double,5> myfloats;
- myints.setmember (0,100);
- myfloats.setmember (3,3.1416);
- cout << myints.getmember(0) << '\n';
- cout << myfloats.getmember(3) << '\n';
- return 0;
- }
#include <iostream>
using namespace std;
template <class T, int N>
class sequence {
T memblock [N];
public:
void setmember (int x, T value);
T getmember (int x);
};
template <class T, int N>
void sequence<T,N>::setmember (int x, T value) {
memblock[x]=value;
}
template <class T, int N>
T sequence<T,N>::getmember (int x) {
return memblock[x];
}
int main () {
sequence <int,5> myints;
sequence <double,5> myfloats;
myints.setmember (0,100);
myfloats.setmember (3,3.1416);
cout << myints.getmember(0) << '\n';
cout << myfloats.getmember(3) << '\n';
return 0;
}
Template Version of Generic binary sorted Tree.
- #include <iostream>
- #include <string.h>
- using namespace std;
- //A generic binary sorted tree.
- template <class T>class gen_tree; //forward decl
- template <class T>
- class bnode {
- private:
- friend class gen_tree<T>;
- bnode<T>* left;
- bnode<T>* right;
- T data;
- int count;
- bnode(T d, bnode<T>* l, bnode<T>* r) :
- data(d), left(l), right(r), count(1) { }
- void print() const
- { cout << data << " : " << count <<'\t'; }
- };
- template <class T>
- class gen_tree {
- public:
- gen_tree() { root = 0; }
- void insert(T d);
- T find(T d) const { return (find(root, d)); }
- void print() const { print(root); }
- private:
- bnode<T>* root;
- T find(bnode<T>* r, T d) const;
- void print(bnode<T>* r)const;
- };
- template <class T>
- void gen_tree<T>::insert(T d)
- {
- bnode<T>* temp = root;
- bnode<T>* old;
- if (root == 0) {
- root = new bnode<T>(d, 0, 0);
- return;
- }
- while (temp != 0) {
- old = temp;
- if (comp(temp -> data, d) == 0) {
- (temp -> count)++;
- return;
- }
- if (comp(temp -> data, d) > 0)
- temp = temp -> left;
- else
- temp = temp -> right;
- }
- if (comp(old -> data, d) > 0)
- old -> left = new bnode<T>(d, 0, 0);
- else
- old -> right = new bnode<T>(d, 0, 0);
- }
- template <class T>
- T gen_tree<T>::find(bnode<T>* r, T d) const
- {
- if (r == 0)
- return 0;
- else if (comp(r -> data, d) == 0)
- return (r -> data);
- else if (comp(r -> data, d) > 0)
- return (find( r -> left, d));
- else
- return (find( r -> right, d));
- }
- template <class T>
- void gen_tree<T>::print(bnode<T> *r)const
- {
- if (r != 0) {
- print( r -> left);
- r -> bnode<T>::print();
- print ( r -> right);
- }
- }
- template <class T>//general case
- int comp(T i, T j)
- {
- if (i == j) //assumes == < defined for T
- return 0;
- else
- return ( (i < j) ? -1 : 1 );
- }
- //specialization for const char*
- int comp(constchar* i, constchar* j){
- return (strcmp(i, j));
- }
- int main()
- {
- char dat[256];
- gen_tree<char*> t;
- char* p;
- while (cin>>dat){
- p = new char[strlen(dat) + 1];
- strcpy(p, dat);
- t.insert(p);
- }
- t.print();
- cout << "EOF" << endl << endl;
- gen_tree<int> i_tree;
- for (int i = 15; i > -5; --i)
- i_tree.insert(i);
- i_tree.print();
- }