#include #include //////////////////////////mydefs1c.h///////////// class Istack { int sz; int top; int* stack; public: Istack(void) // One creator { sz = 97; stack = new int[sz]; top = sz; cout << "using default creator 97\n";} Istack(int Nitems ) // Another creator { sz = Nitems; stack = new int[sz]; top = sz; cout << "using pre-set creator " << Nitems << "\n"; } ~Istack() // The destructor { delete[] stack; } // delete[] removes array space void push(int x) { assert( !isfull() ); stack[--top] = x; } int pop() { assert( !isempty() ); return( stack[top++] ); } bool isempty() { return( top == sz ); } bool isfull() { return( top == 0); } }; //////////////////////stackmain1c.cc/////////////// void main() { Istack s1(500); Istack s2; // default constructor? Istack s3(); // a function which returns Istack Istack s6(137); Istack s4[5]; // default constructor called 5 times here Istack s9(23); Istack* s5; // no space, just a pointer // Istack s8[2](137); // array of two stacks size 137, NO // Istack s7()[2]; // s7() is a function returning an array! // Istack s10(23)[2]; // two 23-element stacks? No way, can only use default 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; // s3.push(10); // s3 is a function, not a stack // cout << s3.pop() << ' '; 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 } /* using pre-set creator 500 using default creator 97 using pre-set creator 137 using default creator 97 using default creator 97 using default creator 97 using default creator 97 using default creator 97 using pre-set creator 23 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 11 using default creator 97 12 */