• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Help with stacks!

imported_vr6

Platinum Member
I am having trouble figuring out a few things for my cmsc202 class. I has to do with a templated stack class. Here is my Class declaration.
************************************************************************************************************************************************
#ifndef STACK_H
#define STACK_H
#include <iostream.h>
template< class T>
class STACK
{
public:
STACK( int = 10 );
~STACK(){delete [] stackPtr;}
bool push( const T& pushValue );
bool pop( T& popValue);
void Display( void );
void peek( void );
//T& PopLast( void );
//const T& operator=( const T& rhs );

private:
int size;
int top;
T *stackPtr;

bool isEmpty() const{return top == -1; }
bool isFull() const {return top == size - 1; }
};

#include "STACK.CPP"
#endif
*************************************************************************************************************************
//sourcefile
#ifndef STACK_C
#define STACK_C
#include <iostream.h>
#include "STACK.H"
**************************************8
template<class T>
STACK<T>::STACK( int s )
{
size = s > 0 ? s: 10;
top = -1;
stackPtr = new T[size];
}
*******************************************
template<class T>
bool STACK<T>:😛ush( const T &pushValue )
{
if( !isFull())
{
stackPtr[++top] = pushValue;
return true;
}
return false;
}
******************************************
template<class T>
bool STACK<T>:😛op( T& popValue )
{

if(!isEmpty())
{
popValue = stackPtr[top--];
return true;
}
return false;
}
******************************************
template<class T>
void STACK<T>:😀isplay( void )
{
int i;
if( !isEmpty() )
{
for( i = 0; i <= top ; i++ )
{
cout << stackPtr << " ";
}
cout << endl;
}
if( isEmpty() )
{
cout << "The stack is empty" << endl;
}

cout <<"Size = " << size << endl;
cout <<"Top = " << top << endl;
}
****************************************************8
template<class T>
void STACK<T>:😛eek( void )
{
cout << "This was the last item entered " << stackPtr[top] << endl;
}
***************************************************************************************************************************************

here is what i was asked to do.

2)Next, create a function, which will pop off the last element of the array, but with a twist. The function will delete the last element in the array as a stack would, but it RETURNS the actual value that was popped off. You will have to created an OVERLOAD of the pop function.

so how would u exactly over load the pop function? Does he mean an overloaded assigment operator? so i can assign it to a temp variable like this?
/*template<class T>
T STACK<T>:😛opLast( void )
{
T popValue;
popValue = stackPtr[top--];
return popValue;
}

template<class T>
const T& STACK<T>:😱perator=( const T& rhs )
{
//rhs stands for right hand side or =
*this = rhs;
return *this;
}

I am pretty sure i did not write the operator= correctly, so can someone correct it if they know what is wrong?
 
When he says overload, he basically means rewrite the pop() function to support the new functionality. For example...

class Base
{
public:
virtual bool Blah(){ return true; }
};

class New : public Base
{
public:
virtual bool Blah(){ return false; }
};

int main()
{
New test;

test.Blah();
return 0;
}

New::Blah() basically rewrites or "overloads" the functionality of Base::Blah(). Instead of returning true, test.Blah() will now return false. Just make your new stack do the same thing. Just call it NewStack or something.
 
If that's the case, he's just asking you rewrite the function. So, either rewrite it or put a wrapper function around pop() that adds the new functionality...

class stack
{
public:
bool PopNew(...etc...)
{
Pop(...etc...);

// your new code somewhere in here
return true;
}
};

About your assignment operator (=), usually it's best to test if &rhs == this. That way, your copy constructor code doesn't have to waste time rebuilding something it doesn't need to copy.
 
my professor just e-mailed me back, and you were right oogle, i do overload the pop function it self. i am gonna try and c what i can come up with, gimme a few...
 
Back
Top