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?
************************************************************************************************************************************************
#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?