my program is

class stack
{
   protected:
                  int tos;
                  int store[10];

       stack()
      {
          tos = 0;
      }

    public:

    void push(int x);
    int pop();
};

    void stack::push(int x)
   { 
        cout<<"put the values into stack";
        store[tos++] = x;
    }

   int stack::pop()
  {
     cout<<" get the values from stack ";
     return store[--tos];
  }

main()
{
    stack stk;
    int i;

  for(i=0;i<=9;i++)
  {
      stk.push(i+10);
  }

  for(i=0;i<=9;i++)
  {
     cout<<stk.pop();
  }
     getch();
  }

here int store[10] array only store 10 items only. I want another class that inherits from stack class . and using the new class how to store infinite items . without touching the main class ( stack class ) .

// suppose the bug of this program is , if user types more than 10 items it will crash .
We are trying to write a bugfix class without touching the main class.

you can only access the functions push() , pop() from the main class

/// ( using inheritance) // iam trying to create a classs named Bugfix .

class Bugfix:public stack
{
        protected:

            ////

       public:
                void push(int x)
                 int pop();
};
          void Bugfix::push(int x)
          {
           
                       //// 
            }
          int Bugfix::pop()
         {
          
                //
          }

only change in main

main()
{
Bugfix stk;  ///  only change



// same as



}

how to do it. plz help me.....give some ideas ......plz help...

Recommended Answers

All 2 Replies

One solution: replace int store[10]; with either std::vector or std::valarray. With these you can store seemingly infinite amuont of numbers (of course it isn't really infinite, but close enough for most practical purposes).

There is no real point in writing a "bug-fix class". Essentially the derived class will have to overload all the methods of the base class anyway - so none of the parts of the base class will be used. Either make a new class with the functionality you want, or rewrite the old class.

As already mentioned, you probably want to use a vector or something of that sort.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.