template class

来着:http://blog.csdn.net/nuoshueihe/article/details/8238553

template class with type parameter

  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4. const int SIZE = 10;
  5. template <class T>class MyClass {
  6. T a[SIZE];
  7. public:
  8. MyClass(void)
  9. {
  10. int i;
  11. for(i=0; i<SIZE; i++)
  12. a[i] = i;
  13. }
  14. T &operator[](int i);
  15. };
  16. template <class T> T &MyClass<T>::operator[](int i)
  17. {
  18. if(i<0 || i> SIZE-1)
  19. {
  20. cout << endl << "Index value of ";
  21. cout << i << " is out of bounds." << endl;
  22. }
  23. return a[i];
  24. }
  25. int main(void)
  26. {
  27. MyClass<int> int_array;
  28. MyClass<double> double_array;
  29. int i;
  30. cout << "Integer array: ";
  31. for(i=0; i<SIZE; i++)
  32. int_array[i] = i;
  33. for(i=0; i<SIZE; i++)
  34. cout << int_array[i] << " ";
  35. cout << endl;
  36. cout << "Double array: ";
  37. cout.precision(2);
  38. for(i=0; i<SIZE; i++)
  39. double_array[i] = (double)i/3;
  40. for(i=0; i<SIZE; i++)
  41. cout << double_array[i] << " ";
  42. cout << endl;
  43. int_array[12] = 100; // Calls overloaded array operator
  44. }
#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

  1. #include <iostream>
  2. using namespace std;
  3. template<class T>class Distance {
  4. public:
  5. Distance(T distance);
  6. void show_distance(void) {
  7. cout << "The distance is " << distance <<" miles\n";
  8. };
  9. private:
  10. T distance;
  11. };
  12. template<class T>
  13. Distance<T>::Distance(T distance) { Distance::distance = distance; };
  14. int main(void)
  15. {
  16. Distance<int> short_distance(100);
  17. Distance<long> long_distance(2000000L);
  18. short_distance.show_distance();
  19. long_distance.show_distance();
  20. }
#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.

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. // The exception type thrown by the safe pointer.
  5. class bad_ptr {
  6. public:
  7. string msg;
  8. bad_ptr(string str) { msg = str; }
  9. };
  10. // A class used to demonstrate the safe pointer.
  11. class myclass {
  12. public:
  13. int alpha;
  14. int beta;
  15. myclass(int p, int q) { alpha = p; beta = q; }
  16. };
  17. template <class T>class safe_ptr {
  18. T *ptr;
  19. public:
  20. safe_ptr() { ptr = 0; }
  21. T *operator->() {
  22. if(!ptr != 0) throw bad_ptr("Attempt to use -> on null pointer.");
  23. else return ptr;
  24. }
  25. T &operator*() {
  26. if(!ptr) throw bad_ptr("Attempt to dereference null pointer.");
  27. else return *ptr;
  28. }
  29. operator T *() { return ptr; }
  30. T *operator=(T *val) { ptr = val; return ptr; }
  31. };
  32. int main()
  33. {
  34. safe_ptr<int> intptr;
  35. try {
  36. *intptr = 23;
  37. cout << "The value pointed to by intptr is: " << *intptr << endl;
  38. } catch(bad_ptr bp) {
  39. cout << bp.msg << endl;
  40. }
  41. intptr = new int;
  42. try {
  43. *intptr = 23;
  44. cout << "The value pointed to by intpr is: " << *intptr <<"\n\n";
  45. } catch(bad_ptr bp) {
  46. cout << bp.msg << endl;
  47. }
  48. safe_ptr<myclass> mcptr;
  49. try {
  50. mcptr = new myclass(100, 200);
  51. cout << "The values of alpha and beta for mcptr are: "
  52. << mcptr->alpha << " and " << mcptr->beta << endl;
  53. mcptr->alpha = 27;
  54. cout << "New value for mcptr->alpha: " << mcptr->alpha << endl;
  55. cout << "Same as (*mcptr).alpha: " << (*mcptr).alpha << endl;
  56. mcptr->beta = 99;
  57. cout << "New value for mcptr->beta: " << mcptr->beta <<"\n\n";
  58. } catch(bad_ptr bp) {
  59. cout << bp.msg << endl;
  60. }
  61. safe_ptr<myclass> mcptr2;
  62. try {
  63. mcptr2->alpha = 88;
  64. } catch(bad_ptr bp) {
  65. cout << bp.msg << endl;
  66. }
  67. delete intptr;
  68. delete mcptr;
  69. return 0;
  70. }
#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

  1. template <typename T>
  2. class SimpleTemplate
  3. {
  4. public:
  5. SimpleTemplate(T &inObject);
  6. const T& get();
  7. void set(T& inObject);
  8. protected:
  9. T& mObject;
  10. };
  11. template <typename T>
  12. SimpleTemplate<T>::SimpleTemplate(T &inObject) : mObject(inObject)
  13. {
  14. }
  15. template <typename T>
  16. const T& SimpleTemplate<T>::get()
  17. {
  18. return mObject;
  19. }
  20. template <typename T>
  21. void SimpleTemplate<T>::set(T& inObject)
  22. {
  23. mObject = inObject;
  24. }
  25. #include <iostream>
  26. #include <string>
  27. using namespace std;
  28. int main(int argc,char** argv)
  29. {
  30. int i = 7;
  31. SimpleTemplate<int> intWrapper(i);
  32. i = 2;
  33. cout << "wrapper value is " << intWrapper.get() << endl;
  34. string str = "test";
  35. SimpleTemplate<string> stringWrapper(str);
  36. str += "!";
  37. cout << "wrapper value is " << stringWrapper.get() << endl;
  38. }
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

  1. #include <iostream>
  2. using namespace std;
  3. template <class T,int N>
  4. class sequence {
  5. T memblock [N];
  6. public:
  7. void setmember (int x, T value);
  8. T getmember (int x);
  9. };
  10. template <class T,int N>
  11. void sequence<T,N>::setmember (int x, T value) {
  12. memblock[x]=value;
  13. }
  14. template <class T,int N>
  15. T sequence<T,N>::getmember (int x) {
  16. return memblock[x];
  17. }
  18. int main () {
  19. sequence <int,5> myints;
  20. sequence <double,5> myfloats;
  21. myints.setmember (0,100);
  22. myfloats.setmember (3,3.1416);
  23. cout << myints.getmember(0) << '\n';
  24. cout << myfloats.getmember(3) << '\n';
  25. return 0;
  26. }
#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.

  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4. //A generic binary sorted tree.
  5. template <class T>class gen_tree; //forward decl
  6. template <class T>
  7. class bnode {
  8. private:
  9. friend class gen_tree<T>;
  10. bnode<T>* left;
  11. bnode<T>* right;
  12. T data;
  13. int count;
  14. bnode(T d, bnode<T>* l, bnode<T>* r) :
  15. data(d), left(l), right(r), count(1) { }
  16. void print() const
  17. { cout << data << " : " << count <<'\t'; }
  18. };
  19. template <class T>
  20. class gen_tree {
  21. public:
  22. gen_tree() { root = 0; }
  23. void insert(T d);
  24. T find(T d) const { return (find(root, d)); }
  25. void print() const { print(root); }
  26. private:
  27. bnode<T>* root;
  28. T find(bnode<T>* r, T d) const;
  29. void print(bnode<T>* r)const;
  30. };
  31. template <class T>
  32. void gen_tree<T>::insert(T d)
  33. {
  34. bnode<T>* temp = root;
  35. bnode<T>* old;
  36. if (root == 0) {
  37. root = new bnode<T>(d, 0, 0);
  38. return;
  39. }
  40. while (temp != 0) {
  41. old = temp;
  42. if (comp(temp -> data, d) == 0) {
  43. (temp -> count)++;
  44. return;
  45. }
  46. if (comp(temp -> data, d) > 0)
  47. temp = temp -> left;
  48. else
  49. temp = temp -> right;
  50. }
  51. if (comp(old -> data, d) > 0)
  52. old -> left = new bnode<T>(d, 0, 0);
  53. else
  54. old -> right = new bnode<T>(d, 0, 0);
  55. }
  56. template <class T>
  57. T gen_tree<T>::find(bnode<T>* r, T d) const
  58. {
  59. if (r == 0)
  60. return 0;
  61. else if (comp(r -> data, d) == 0)
  62. return (r -> data);
  63. else if (comp(r -> data, d) > 0)
  64. return (find( r -> left, d));
  65. else
  66. return (find( r -> right, d));
  67. }
  68. template <class T>
  69. void gen_tree<T>::print(bnode<T> *r)const
  70. {
  71. if (r != 0) {
  72. print( r -> left);
  73. r -> bnode<T>::print();
  74. print ( r -> right);
  75. }
  76. }
  77. template <class T>//general case
  78. int comp(T i, T j)
  79. {
  80. if (i == j) //assumes == < defined for T
  81. return 0;
  82. else
  83. return ( (i < j) ? -1 : 1 );
  84. }
  85. //specialization for const char*
  86. int comp(constchar* i, constchar* j){
  87. return (strcmp(i, j));
  88. }
  89. int main()
  90. {
  91. char dat[256];
  92. gen_tree<char*> t;
  93. char* p;
  94. while (cin>>dat){
  95. p = new char[strlen(dat) + 1];
  96. strcpy(p, dat);
  97. t.insert(p);
  98. }
  99. t.print();
  100. cout << "EOF" << endl << endl;
  101. gen_tree<int> i_tree;
  102. for (int i = 15; i > -5; --i)
  103. i_tree.insert(i);
  104. i_tree.print();
  105. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值