C++ 模板类

定义模板类

stacktp.h

  1. #ifndef PRO1_STACKTP_H
  2. #define PRO1_STACKTP_H
  3. template <class Type>
  4. class Stack {
  5. private:
  6. enum {MAX = 10};
  7. Type items[MAX];
  8. int top;
  9. public:
  10. Stack();
  11. bool isempty();
  12. bool isfull();
  13. bool push(const Type &item);
  14. bool pop(Type &item);
  15. };
  16. template <class Type>
  17. Stack<Type>::Stack() {
  18. top = 0;
  19. }
  20. template <class Type>
  21. bool Stack<Type>::isempty() {
  22. return top == 0;
  23. }
  24. template <class Type>
  25. bool Stack<Type>::isfull() {
  26. return top == MAX;
  27. }
  28. template <class Type>
  29. bool Stack<Type>::push(const Type &item) {
  30. if (top < MAX) {
  31. items[top++] = item;
  32. return true;
  33. } else
  34. return false;
  35. }
  36. template <class Type>
  37. bool Stack<Type>::pop(Type &item) {
  38. if (top > 0) {
  39. item = items[--top];
  40. return true;
  41. } else
  42. return false;
  43. }
  44. #endif //PRO1_STACKTP_H

stacktp.cpp

  1. #include "stacktp.h"

使用模板类

main.cpp

  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <cctype>
  5. #include "module11_class_template/stacktp.h"
  6. using namespace std;
  7. // 使用模板类
  8. void useTemplateClass(){
  9. Stack<string> st;
  10. char ch;
  11. string po;
  12. cout << "Please enter A to add a purchase order, \n" << "P to process a PO, or to quit.\n";
  13. while (cin >> ch && toupper(ch) != 'Q') {
  14. while (cin.get() != '\n') {
  15. continue;
  16. }
  17. if (!isalpha(ch)) {
  18. cout << '\a';
  19. continue;
  20. }
  21. switch (ch){
  22. case 'A':
  23. case 'a': cout << "Enter a PO number to add: ";
  24. cin >> po;
  25. if (st.isfull())
  26. cout << "Stack already full\n";
  27. else
  28. st.push(po);
  29. break;
  30. case 'P':
  31. case 'p': if (st.isempty())
  32. cout << "Stack already empty\n";
  33. else{
  34. st.pop(po);
  35. cout << "PO #" << po << " popped\n";
  36. break;
  37. }
  38. }
  39. cout << "Please enter A to add a purchase order ,\n" << "P to process a PO , or Q to quit.\n";
  40. }
  41. cout << "Bye\n";
  42. }
  43. int main() {
  44. useTemplateClass();
  45. return 0;
  46. }

模板类的非模板友元函数

tempfriend.cpp

  1. #include <iostream>
  2. using namespace std;
  3. template <typename T>
  4. class HasFriend{
  5. private:
  6. T item;
  7. static int ct;
  8. public:
  9. HasFriend(const T & i):item(i){ ct++; }
  10. ~HasFriend();
  11. friend void counts();
  12. friend void reports(HasFriend<T> &);
  13. };
  14. // each specialization has it's own static data member
  15. template <typename T>
  16. int HasFriend<T>::ct = 0;
  17. // non-template friend to all HasFriend<T> classes
  18. void counts(){
  19. cout << "int count: " << HasFriend<int >::ct << "; ";
  20. cout << "double count: " << HasFriend<double >::ct << endl;
  21. }
  22. // non-template friend to the HasFriend<int> class
  23. void reports(HasFriend<int> & hf){
  24. cout << "HasFriend<int>: " << hf.item << endl;
  25. }
  26. // non-template friend to the HasFriend<double> class
  27. void reports(HasFriend<double> & hf){
  28. cout << "HasFriend<double>: " << hf.item << endl;
  29. }
  30. void test(){
  31. cout << "No objects declared: ";
  32. counts();
  33. HasFriend<int > hfil(10);
  34. cout << "After hfil declared: ";
  35. counts();
  36. HasFriend<int > hfi2(20);
  37. cout << "After hfi2 declared: ";
  38. counts();
  39. HasFriend<double > hfdb(10.5);
  40. cout << "After hfdb declared: ";
  41. counts();
  42. reports(hfil);
  43. reports(hfi2);
  44. reports(hfdb);
  45. }

模板类的约束模板友元函数

tmp2tmp.cpp

  1. #include <iostream>
  2. using namespace std;
  3. template <typename T> void counts();
  4. template <typename T> void report(T &);
  5. template <typename TT>
  6. class HasFriendT{
  7. private:
  8. TT item;
  9. static int ct;
  10. public:
  11. HasFriendT(const TT & i) : item(i){ ct++; }
  12. ~HasFriendT();
  13. friend void counts<TT>();
  14. friend void report<>(HasFriendT<TT> &);
  15. };
  16. template<typename T>
  17. int HasFriendT<T>::ct = 0;
  18. // template friend functions definitions
  19. template <typename T>
  20. void counts(){
  21. cout << "template size: " << sizeof(HasFriendT<T>) << "; ";
  22. cout << "template counts()" << HasFriendT<T>::ct << endl;
  23. }
  24. template <typename T>
  25. void report(T & hf){
  26. cout << hf.item << endl;
  27. }
  28. void test(){
  29. counts<int>();
  30. HasFriendT<int> hfil(10);
  31. HasFriendT<int> hfi2(20);
  32. HasFriendT<double > hfdb(10.5);
  33. report(hfil);
  34. report(hfi2);
  35. report(hfdb);
  36. cout << "counts<int>() output:\n";
  37. counts<int>();
  38. cout << "counts<double >() output:\n";
  39. counts<double >();
  40. }

模板类的非约束模板友元函数

manyfrnd.cpp

  1. #include <iostream>
  2. using namespace std;
  3. template <typename T>
  4. class ManyFriend{
  5. private:
  6. T item;
  7. public:
  8. ManyFriend(const T & i) : item(i) {}
  9. template <typename C, typename D> friend void show2(C &, D &);
  10. };
  11. template <typename C, typename D> void show2(C & c, D & d){
  12. cout << c.item << ". " << d.item << endl;
  13. }
  14. void run(){
  15. ManyFriend<int> hfi1(10);
  16. ManyFriend<int> hfi2(20);
  17. ManyFriend<double > hfdb(10.5);
  18. cout << "hfi1, hfi2: ";
  19. show2(hfi1, hfi2);
  20. cout << "hfdb, hfi2: ";
  21. show2(hfdb, hfi2);
  22. }