Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

inheritance, push(), pop(), it doesn't run

inheritance

  • Please log in to reply
3 replies to this topic

#1 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 02 December 2011 - 07:14 AM

Hi

What is wrong with this code? It's not running. It compiles okay but when I run it, it simply says, "Press any key to continue . . .". Please help me with it. Thank you.

#include <iostream>
using namespace std;
#include <cstdlib>
#include <string>

const int MAX = 10;
////////////////////////////////////////////////////////////////
class Stack
{
        protected:
            //static const int MAX=10;
            int st[MAX];
            int top;
        public:
            Stack(): top(-1)
                {/*empty body*/}
        void push(int var)
                { st[++top] = var; }
        int pop()
                { return st[top--]; }
};
////////////////////////////////////////////////////////////////

class Stack2 : public Stack
{
        public:
            void push(int var)
                {
                    if(top >= MAX-1) // (MAX-1) because array starts from 0
                        { cout << "\nError: stack is full"; exit(1); }

                    Stack::push(var);
                }

            int pop()
                {
                    if(top < 0)
                        { cout << "\nError: stack is empty\n"; exit(1); }
                    return Stack::pop();
                }
};
////////////////////////////////////////////////////////////////

int main()
{
   Stack2 s1; int stackvar; int count = -1;

   for(int i=0; i>=(MAX-1); i++)
   {
       cout << "enter the number into stack: "; cin >> stackvar;
       s1.push(stackvar);
       count++;
   }

   cout << endl;

   while(count != -1)
   {
       s1.pop();
       count--;
   }

    cout << endl;

    system("pause;");
    return 0;
}

  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 02 December 2011 - 08:14 AM

Your for loop exits immediately in main():

for(int i=0; i>=(MAX-1); i++)

should be
for(int i=0; i<=(MAX-1); i++)
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 02 December 2011 - 10:21 AM

Thank you, WP. I have corrected and it works now.

By the way, don't you think the if statements within the function bodies of Stack2 class become useless because of the for and while loops in the main?

Is there any way that I can use define MAX within the body of Stack class and still be able to do what I want to? As you can see, I initially tried to define MAX within the class body, but then I wanted to use it within the main() so I had to make it global instead.


#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

const int MAX = 3;
////////////////////////////////////////////////////////////////
class Stack
{
        protected:
            [B][COLOR="#FF0000"]//static const int MAX=10;[/COLOR][/B]
            int st[MAX];
            int top;
        public:
            Stack(): top(-1)
                {/*empty body*/}
        void push(int var)
                { st[++top] = var; }
        int pop()
                { return st[top--]; }
};
////////////////////////////////////////////////////////////////

class Stack2 : public Stack
{
        public:
            void push(int var)
                {
                    [B][COLOR="#FF0000"]if(top >= MAX-1)[/COLOR][/B] // (MAX-1) because array starts from 0
                        { cout << "\nError: stack is full"; exit(1); }

                    Stack::push(var);
                }

            int pop()
                {
                   [B][COLOR="#FF0000"] if(top < 0)[/COLOR][/B]
                        { cout << "\nError: stack is empty\n"; exit(1); }
                    return Stack::pop();
                }
};
////////////////////////////////////////////////////////////////

int main()
{
   Stack2 s1; int stackvar; int count = -1;

   for(int i=0; i<=(MAX-1); i++)
   {
       cout << "enter the number to be pushed onto the stack: "; cin >> stackvar;
       s1.push(stackvar);
       count++;
   }

   cout << endl;

   while(count > -1)
   {
       cout << s1.pop() << endl;
       count--;
   }

    cout << endl;

    system("pause;");
    return 0;
}

  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 02 December 2011 - 11:28 AM

If you make MAX a public member of stack, you could access it as Stack2.MAX.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/






Also tagged with one or more of these keywords: inheritance