{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

AVIQ2 - data[i = q.data[i stack& stack:operator=(const...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; class stack { private: int max; int count; int *data; static int grow; public: stack(void); ~stack(void); stack(const stack& q); stack& operator=(const stack& q); void push(int item); int pop(void); void show(ostream& out) const; }; int stack::grow = 5; int main() { int i, x; stack s; ofstream fout ("stackgrows.out"); for(i=1; i<=12; i++) s.push(i); cout << "The stack is:\n"; s.show(cout); cout << "\n\n"; fout << "The stack is:\n"; s.show(fout); fout << "\n\n"; fout.close(); system("notepad stackgrows.out"); return 0; } stack::stack(void) { max = 0; data = NULL; count = 0; } stack::~stack(void) { delete [] data; } stack::stack(const stack& q) { int i; max = q.max; count = q.count; data = new int[max]; for(i=0; i < count; i++)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: data[i] = q.data[i]; } stack& stack::operator=(const stack& q) { int i; if(this != &q) { delete data; max = q.max; count = q.count; data = new int[max]; for(i=0; i < count; i++) data[i] = q.data[i]; } return *this; } void stack::push(int iteam){ int i; if(count==max){ int*temp; temp=new int [count]; for(i=0;i<max;i++) temp[i]=data[i]; delete data; max=max+grow; data=new int [max]; for(i=0;i<max;i++) data[i]=temp[i]; delete temp; } data[count]=iteam; count++; } int stack::pop(){ if(count==0){ cerr<<"stack is empty\n"; exit(1); } int y=data[count-1]; count--; return y; } void stack::show(ostream& out) const{ int i; for(i=0;i<=count-1;i++) out<<data[i]<<","; out<<endl; }...
View Full Document

{[ snackBarMessage ]}