Problem while implementing inheritance

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::push(var);
else
cout << "Stack full\n";
}

int pop()
{ if(top > 0)
return Stack::pop();
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
 
S

SG

[...]
      int pop()
      { if(top > 0)
           return Stack::pop();
        else
           cout << "Stack empty\n";
      }
[...]
33487908

It is the last line that beats the hell out of me. Thanks in advance.

You declared the function pop to return an int. But there is an
excecution path that does not end in a return expression. This causes
undefined behaviour. You should increase your compiler's warning
level.

SG
 
P

Paul

bintom said:
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::push(var);
else cout << "Stack full\n";
}
int pop(){
if(top > 0) return Stack::pop();
else cout << "Stack empty\n";


else branch doesn't return any int varaible.
}
};

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";

What does NS.pop() evaluate to, if else branch is accessed?
}


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.
It's probably some stack address but it doesn't matter what it is , its a
bug. You need to return an int from pop.

HTH.
 
B

bintom

[...]
      int pop()
      { if(top > 0)
           return Stack::pop();
        else
           cout << "Stack empty\n";
      }
[...]
  33487908
It is the last line that beats the hell out of me. Thanks in advance.

You declared the function pop to return an int. But there is an
excecution path that does not end in a return expression. This causes
undefined behaviour. You should increase your compiler's warning
level.

SG


I've given the return 0; statement, but that doesn't help either.

int pop()
{ if(top > 0)
return Stack::pop();
else
{ cout << "Stack empty\n";
return 0;
}
}

Bintom
 
P

puppi

On 20 Mrz., 13:55, bintom wrote:
[...]
      int pop()
      { if(top > 0)
           return Stack::pop();
        else
           cout << "Stack empty\n";
      }
[...]
  33487908
It is the last line that beats the hell out of me. Thanks in advance.
You declared the function pop to return an int. But there is an
excecution path that does not end in a return expression. This causes
undefined behaviour. You should increase your compiler's warning
level.

I've given the return 0; statement, but that doesn't help either.

int pop()
{ if(top > 0)
    return Stack::pop();
  else
  { cout << "Stack empty\n";
    return 0;
  }

}

Bintom

Let me guess: now the "junk" number printed below "Stack empty" is 0.
The thing is you are printing the result of NS.pop() to stdout anyway.
If NS is empty, it will return 0, and that number will be printed. If
you just want to print when a number was actually poped, you should
make the NewStack::pop() function "warn" that the stack is empty in
some unambiguous way, and then check it. If it simply returns 0, you
could declare a variable int, say int tmp, and change the main()
fragment "cout << NS.pop() << "\n";" to:
int tmp = NS.pop();
if(tmp != 0)
cout << tmp << "\n";
However, if the stack wasn't empty, and returned 0 because it had an
element with value 0, you will have discarded a valid pop. You could
instead define a NewStack::is_empty() function, which returns a bool,
and do:
if(!NS.is_empty())
cout << NS.pop() << "\n";
Another alternative would be make NewStack::pop() throw an exception
when the stack is empty, and then embrace the NS.pop() call in a try-
catch block.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top