B
bintom
I have a base class Stack and a derived class NewStack. NewStack is
supposed to check the array bounds of the stack (st). However, I get
some junk at the end of the normal output. I'm going nuts figuring
why. Could anybody help?
#include <iostream>
const int MAX = 10;
class Stack
{ protected: int st[MAX], top;
public : Stack()
{ top = 0; }
void push(int var)
{ st[top++] = var; }
void display()
{ cout << "Stack is: \n";
for(int i=top-1; i>=0; i--)
cout << st << "\n";
cout << "\n";
}
int pop()
{ cout << "Popping ";
return st[--top];
}
};
class NewStack : public Stack
{ public : void push(int var)
{ if(top < MAX)
Stack:ush(var);
else
cout << "Stack full\n";
}
int pop()
{ if(top > 0)
return Stack:op();
else
cout << "Stack empty\n";
}
};
int main()
{ NewStack NS; NS.push(101); NS.push(202); NS.display();
cout << NS.pop() << "\n"; cout << NS.pop() << "\n";
cout << "\n";
cout << NS.pop() << "\n";
}
OUTPUT:
Stack is:
202
101
Popping 202
Popping 101
Stack empty
33487908
It is the last line that beats the hell out of me. Thanks in advance.
Bintom
supposed to check the array bounds of the stack (st). However, I get
some junk at the end of the normal output. I'm going nuts figuring
why. Could anybody help?
#include <iostream>
const int MAX = 10;
class Stack
{ protected: int st[MAX], top;
public : Stack()
{ top = 0; }
void push(int var)
{ st[top++] = var; }
void display()
{ cout << "Stack is: \n";
for(int i=top-1; i>=0; i--)
cout << st << "\n";
cout << "\n";
}
int pop()
{ cout << "Popping ";
return st[--top];
}
};
class NewStack : public Stack
{ public : void push(int var)
{ if(top < MAX)
Stack:ush(var);
else
cout << "Stack full\n";
}
int pop()
{ if(top > 0)
return Stack:op();
else
cout << "Stack empty\n";
}
};
int main()
{ NewStack NS; NS.push(101); NS.push(202); NS.display();
cout << NS.pop() << "\n"; cout << NS.pop() << "\n";
cout << "\n";
cout << NS.pop() << "\n";
}
OUTPUT:
Stack is:
202
101
Popping 202
Popping 101
Stack empty
33487908
It is the last line that beats the hell out of me. Thanks in advance.
Bintom