c++异常模板复习

  1 // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include<iostream>
  6 #include"TEST2.h"
  7 #include<exception>
  8 using namespace std;
  9 using std::cout; using std::endl;
 10 
 11 template<typename T1,typename T2>
 12 T2 my_add(T1 NumA, T2 NumB){
 13     return NumA + NumB;
 14 }
 15 
 16 
 17 
 18 template<typename T>
 19 T MyMax(const T NumA, const T NumB){
 20 
 21     return NumA < NumB ? NumB : NumA;
 22 }
 23 template<>
 24 const char* MyMax<const char*>(const char* pStrA, const char* pStrB){
 25     return (strcmp(pStrA, pStrB) < 0) ? pStrB : pStrA;
 26 
 27 }
 28 
 29 
 30 template<class T>
 31 class CVector{
 32 public:
 33     CVector(int nCount) :m_nSize(nCount){
 34         m_pData = new T[nCount];
 35         memset(m_pData, 0, nCount);
 36     }
 37     ~CVector(){ delete m_pData; }
 38     T operator[](int nIndex){
 39         return m_pData[nIndex];
 40     }
 41 
 42 private:
 43     T * m_pData;
 44     int m_nSize;
 45 };
 46 
 47 class StackOverfloawExp{//栈满异常类
 48 public:
 49     StackOverfloawExp(){}
 50     ~StackOverfloawExp(){}
 51     void getMessage(){
 52         cout << "异常:栈满不能入栈" << endl;
 53     }
 54 
 55 };
 56 class StackEmptyExp{//栈空异常类
 57 public:
 58     StackEmptyExp(){}
 59     ~StackEmptyExp(){}
 60     void getMessage(){
 61         cout << "异常:栈空不能出战" << endl;
 62     
 63     }
 64 };
 65 
 66 template<class TYPE>
 67 class CStack{
 68 public :
 69     CStack(int nCount){
 70         m_pStack = new TYPE[nCount];
 71         memset(m_pStack, 0, nCount);
 72         if (!m_pStack) throw StackEmptyExp();
 73         m_nLength = nCount;
 74         m_nTos = 0;
 75     }
 76     ~CStack(){ delete[] m_pStack; }
 77     void push(TYPE Data){
 78         if (m_nTos == m_nLength) throw StackOverfloawExp();
 79         m_pStack[m_nTos++] = Data;
 80     }
 81     TYPE pop(){
 82         if (!m_nTos) throw StackEmptyExp();
 83         return m_pStack[--m_nTos];
 84     }
 85 
 86 private:
 87     TYPE *m_pStack;
 88     int  m_nTos, m_nLength;
 89 };
 90 
 91 
 92 
 93 class CArrayOverflow :public exception{
 94 public:
 95     CArrayOverflow(int nNum) :exception("数组越界异常!
"){
 96         m_nNum = nNum;
 97     }
 98     const char * what(){//重定义的what()函数
 99         cout << "数组下标" << m_nNum << "越界
";
100         return exception::what();
101     }
102 private:
103     int m_nNum;
104 
105 };
106 
107 int _tmain(int argc, _TCHAR* argv[])
108 {
109 
110     cout << my_add(1, 3.4) << endl;
111     cout << my_add(1.2, 'A') << endl;
112     cout << my_add(2, 'A') << endl;
113     cout << MyMax("15PB", "A1Pass") << endl;
114     CVector<int> objNum(15);
115     cout << objNum[1] << endl;
116 
117 
118     CStack<int> obj(3);
119     for (int i = 0; i < 5; i++){
120         try{
121             cout << obj.pop() << endl;
122 
123         }
124         catch (StackOverfloawExp &e){
125             e.getMessage();
126         }
127         catch (StackEmptyExp &e){
128             e.getMessage();
129           
130         }
131     }
132     system("pause");
133     return 0;
134 }
让数据变得更安全!
原文地址:https://www.cnblogs.com/Alyoyojie/p/5236144.html