Problem while implementing inheritance

Discussion in 'C++' started by bintom, Mar 20, 2011.

  1. bintom

    bintom Guest

    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
     
    bintom, Mar 20, 2011
    #1
    1. Advertising

  2. bintom

    SG Guest

    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.

    SG
     
    SG, Mar 20, 2011
    #2
    1. Advertising

  3. bintom

    Paul Guest

    "bintom" <> wrote in message
    news:...
    >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.
     
    Paul, Mar 20, 2011
    #3
  4. bintom

    bintom Guest

    On Mar 20, 4:37 pm, SG <> wrote:
    > 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.
    >
    > 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
     
    bintom, Mar 20, 2011
    #4
  5. bintom

    puppi Guest

    On Mar 20, 10:48 am, bintom <> wrote:
    > On Mar 20, 4:37 pm, SG <> wrote:
    >
    >
    >
    > > 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.

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


    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.
     
    puppi, Mar 20, 2011
    #5
  6. bintom

    bintom Guest

    A big thanks to puppi for holding my hand while I found the solution.

    Bintom
     
    bintom, Mar 21, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,148
    Martijn van Steenbergen
    Dec 21, 2003
  2. Replies:
    4
    Views:
    5,409
    Lorenzo Bettini
    Feb 14, 2005
  3. invy
    Replies:
    4
    Views:
    392
    CBFalconer
    Dec 28, 2006
  4. Jaco Naude
    Replies:
    5
    Views:
    573
    Jaco Naude
    Apr 1, 2009
  5. Sarkar
    Replies:
    6
    Views:
    610
    Sarkar
    Apr 23, 2010
Loading...

Share This Page