#include #include /////////////////////////////mydefs1.h////////////// class Istack { int sz; int top; int* stack; public: // Using only default constructor void init(int ) ; void push(int ) ; int pop() ; bool isempty() ; bool isfull() ; }; /////////////////////////stack1.cc//////////////// // Array implementation of stacks. // Grow from high index towards 0. // Using default constructor void Istack :: init (int nitems = 100) { sz = nitems; stack = new int[sz]; top = sz; } void Istack :: push(int x) { assert( !isfull() ); stack[--top] = x; } int Istack :: pop() { assert( !isempty() ); return( stack[top++] ); } bool Istack :: isempty() { return( top == sz ); } bool Istack :: isfull() { return( top == 0 ); } //////////////////////stackmain1.cc////////// void main() { Istack s1; // default constructor? s1.init(500); Istack s2; // default constructor? s2.init; Istack s3(); // a function which returns Istack Istack s6; s6.init(137); Istack s4[5]; // default constructor called 5 times here Istack* s5; // no space, just a pointer int i; for( i = 0; i < 20; i++ ) { s1.push(i); // s1 is a 500 element stack } for( i = 0; i < 20; i++ ) { cout << s1.pop() << ' '; } cout << endl; // Everything is OK up to here, but there are problems // with both of the following, depending on system used s4[3].push(11); // 3rd stack in the array cout << s4[3].pop() << ' '; s5 = new Istack; // get stack space, which constructor? s5->push(12); // pointing to a stack cout << s5->pop() << endl; delete s5; // explicit removal } /* 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Assertion (!isfull()) failed in "stack1.cc" on line 15 */