1 //the process of exception handling is mainly three steps2 //throwing exceptions, catching exceptions, and handling exceptions3#include <iostream.h>4 5 classpushonfull{6 };7 8 classpoponempty{9 };Ten One classseqstack{ A Private: - int*data; - intmaxstacksize; the inttop; - Public: -Seqstack (intn); -~Seqstack (); + - voidPush (Const intitem); + intPop (); A }; at -Seqstack::seqstack (intN) { -top=0; -Maxstacksize=N; -Data=New int[n]; - } in -seqstack::~Seqstack () { to Deletedata; + } - the voidSeqstack::P Ush (Const intItem) { * if( This->top== This-maxstacksize) $ ThrowPushonfull ();//Throw ExceptionPanax Notoginseng -data[top]=item; thetop++; + } A the intseqstack::P op () { + if(top==-1){//here is a question, top==-1 or top==0 suitable - Throwpoponempty (); $ } $top--; - returnData[top]; - } the - intMain () {WuyiSeqstack Mystack (Ten); the //try{ - //for (int i=0;i<11;i++) { Wu //Mystack.push (i); - // } About //}catch (pushonfull) { $ //cout<< "Error! Stack is full" <<endl; - // } - - Try{ A for(intI=0;i< One; i++){ +Cout<<mystack.pop () <<Endl; the } -}Catch(poponempty) { $cout<<"the stack is empty."<<Endl; the } the return 0; the}
C + + Learning (29)