jeudi 23 février 2017

store the variables from my buffer class into the stack class.

Can you please help me in fixing this code.How to store the variables from the buffer class to the stack class and pass the variables as objects into the stack.The buffer length has to automatically increment. Note : There are plenty of errors.

        #include <iostream>
            #include <stack> 
            #include <new>
            using namespace std;

            class buffer;
  /*  Stack class:
    >Stack-pointer
    >push and pop operations
    >print the stack content
    >We should NOT be able to create the copies of this class (from original object)
*/
            class Stack{
                private:
                    buffer *p ;
                    int top, length;

                 public:

                   Stack(int s=0);
                   Stack(const Stack &obj) = delete;
                    void operator = (const Stack &obj) = delete;
                    ~Stack();

                    void push(buffer ob);
                    object pop();
                    void display();
            };

            Stack::Stack(int size){
                top = -1;
                length = size;
                if(size == 0){
                    p = 0;
                }
                else{
                    p = new Stack[length];
                }
            }

            Stack::~Stack(){
                if(p!=0)
                    delete [] p;
            }

            void Stack:: push(buffer &elem){
                if(p==0){
                    cout<<"Stack size is zero"<<endl;
                    cout<<"Enter Stack Size : ";
                    cin>>length;
                    p = new Stack[length];
                }
                if(top == (length-1)){
                    cout<<"Stack Full !!!"<<endl;
                    return;
                }
                else{
                    ++top;
                    p[top] = elem;
                }
            }

            object Stack :: pop(){
                if(p==0 || top == -1){
                    cout<<"Stack Empty !";
                    return -1;
                }
                else{
                    object ret = p[top];
                    --top;
                    return ret;
                }
            }

            void Stack :: display(){
                for(int i=0; i<=top;i++){
                    cout<<p[i]<<" "<<endl;
                }
            }

/*Buffer class:
    >Buffer: char * _pchbuffer - will be allocated at construction time
    >Buffer: length of the buffer
    >Buffer: int ID;
    >We should be able to create copies of this Buffer class (from original object) */

            class buffer{

                public:
                char *_pchbuffer;
                int id;
                int bl;

                public:
                buffer(const buffer &obj){              //default CC
                    _pchbuffer = obj._pchbuffer;
                    bl = obj.bl;
                    id = obj.id;
                    buffer_length();
                }             
                void operator = (const buffer &obj){    //Assignment operator
                    _pchbuffer = obj._pchbuffer;
                    bl = obj.bl;
                    id = obj.id;
                    buffer_length();
                }   

                int buffer_length(){
                    return bl++;
                }

                buffer(char *c, int l, int i){
                    _pchbuffer = c;
                    bl = l;
                    id = i;
                    buffer_length();
                }

            };

            int main() {
                // your code goes here
                Stack s(100);

                buffer bf("A",1,1000);  //pushing values at object creation
                s.push(bf);
                buffer bf1("B",2,1001);
                s.push(bf1);
                s.display();
                return 0;
            }

Aucun commentaire:

Enregistrer un commentaire