Need Help (Stack)

Hi . I am new in c++ programming and I need some help to complete my assignment. I have no idea how to begin and what to do . Please somebody explain it to me and guide me . I am not asking for complete solution just some hint. Thanks for your help in advanced.

the head file stack.h describes the data type STACK as follows:

class STACK{
int *const elems;
const int max;
int top;
public:
STACK(int m);
STACK(const STACK&s);
virtual int size ( ) const;
virtual operator int ( ) const;
virtual int operator[ ] (int x) const;
virtual STACK& operator<<(int e);
virtual STACK& operator>>(int &e);
virtual STACK& operator=(const STACK&s);
virtual void print( ) const;
virtual ~STACK( );
};
You can start with:
1) use code tag
2) describe each line
3) try to write stack.cpp

In your STACK class declaration, I would expect a private member containing the size of the stack. Is it int top; ?
Thanks for your answer.
Yes it is .
The thing that confusing me is virtual function .And where should I initialize my stack?
Last edited on
Yes it is .
So, const int max; would be the maximum number of elements your stack class will support?

The thing that confusing me is virtual function
If you don't derive from STACK, you could ignore the word virtual for the moment.

where should I initialize my stack?
In the constructor STACK(int m);

Thanks for your answer. I will post the full assignment to see.Maybe I didnt explain it well.

1.Problem description
An integer stack is a LIFO (last in, first out) data type that serves as a collection of integer elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the last element that was added. The data type STACK can be implemented with a linear data structure by using an integer pointer which is allocated a piece of memory that can store at most max elements. The head file stack.h describes the data type STACK as follows:

class STACK{
int *const elems; //memory allocated to store elements
const int max; //maximum number of elements the stack can contain
int top; //top indicator or number of elements in current stack

public:

STACK(int m); //create a stack which can store at most m elements

STACK(const STACK &s); //deep-copy constructor of stack s

virtual int size ( ) const; //get maximum number max

virtual operator int ( ) const; //get top indicator top

virtual int operator[ ] (int x) const; //get element at location x in the stack

virtual STACK& operator<<(int e); //push e into the stack

virtual STACK& operator>>(int &e); //pop an element to e from the stack

virtual STACK& operator=(const STACK&s); //deep-copy assignment of stacks

virtual void print( ) const; //print out the stack

virtual ~STACK( ); //destroy the stack

};


Please create a project named st using “console wizard” of C++ builder 6.0 that includes three files: stack.cpp which is used to implement the functions defined in stack.h and stest.cpp which contains the main function which is used to test the operations on stack .
Last edited on
You can start with:
1) use code tag
2) describe each line
3) try to write stack.cpp

1) please use code tag!!!
2) ok
3) I think nobody will do a full assignment for you.
I will suggest you constructor and destructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
STACK::STACK(int m)
	: max(100), elems(new int(max)), top(m)
{
	// what if m > max???
}

STACK::~STACK()
{
	if (elems != nullptr)
	{
		delete[] elems;
	}
}

Now, you should try to implement the other functions and maybe we will help you.
Topic archived. No new replies allowed.