C + + Learning (30)

Source: Internet
Author: User

1 //Designing a sequential stack class with data elements of type int2 //requirements: When the stack operation is abnormal, the exception processing module outputs the value of the data element currently in the stack and designs a test program3#include <iostream.h>4 5 classpushonfull{6     Private:7         intvalue;8      Public:9Pushonfull (intx) {TenValue=x; One         } A         intValue () { -             returnvalue; -         } the }; -  - classpoponempty{ - }; +  - classseqstack{ +     Private: A         int*data; at         intmaxstacksize; -         inttop; -      Public: -Seqstack (intn); -~Seqstack (); -          in         voidPush (Const intitem); -         intPop (); to }; +  -Seqstack::seqstack (intN) { thetop=0; *Maxstacksize=N; $Data=New int[n];Panax Notoginseng } -  theseqstack::~Seqstack () { +     Deletedata; A } the  + voidSeqstack::P Ush (Const intItem) { -     if( This->top== This-maxstacksize) $         ThrowPushonfull (item);//Throw Exception $      -data[top]=item; -top++; the } - Wuyi intseqstack::P op () { the     if(top==-1){//here is a question, top==-1 or top==0 suitable -         Throwpoponempty (); Wu     } -top--; About     returnData[top]; $ } -  - intMain () { -Seqstack Mystack (Ten); A     Try{ +          for(intI=0;i< One; i++){ the Mystack.push (i); -         } $}Catch(Pushonfull obj) { thecout<<"the stack is full."<<Endl; thecout<<"the element that is currently going into the stack is"<<obj. Value () <<Endl; the     } the      - //try{ in //for (int i=0;i<11;i++) { the //Cout<<mystack.pop () <<endl; the //        } About //}catch (poponempty) { the //cout<< "Error! Stack is empty" <<endl; the //    } the     return 0; +}

C + + Learning (30)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.