store the object from my buffer class into the stack class.
Feb 23, 2017 at 7:12am Feb 23, 2017 at 7:12am UTC
Write your question here.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#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;
}
Feb 23, 2017 at 12:08pm Feb 23, 2017 at 12:08pm UTC
There are compiler errors which are rather obvious. What is the error you don't understand?
Some errors could be solven if you move the class buffer
before class Stack
because otherwise Stack
cannot do anything with buffer
apart from having a pointer or a reference to it.
Topic archived. No new replies allowed.