empty stacks are equal s1: 20 10 s2: 10 s1 is not equal to s2 s2: 20 10 s1 is equal to s2 s1: 20 30 s1 is not equal to s2
#include <iostream> #include <stdlib.h> #include "stack.h" using namespace std; int main() { stackType<int> s1; stackType<int> s2; if (s1 == s2) { cout << "empty stacks are equal" << endl; } s1.push(10); s1.push(20); s2.push(10); cout << "s1: "; cout << "s2: "; cout << "s1 "; if (s1 != s2) cout << "!"; cout << "= s2" << endl; s2.push(20); cout << "s2: "; cout << "s1 "; if (s1 == s2) { cout << "is equal "; } else { cout << "is not equal " << endl; } cout << "s2" << endl; s1.pop(); s1.pop(); s1.push(30); s1.push(20); cout << "s1: "; cout << "s1 "; if (s1 != s2) cout << " is"; cout << " not equal to s2" << endl; system ("pause"); return 0; }
#ifndef H_StackType #define H_StackType #include <iostream> #include <cassert> using namespace std; template<class Type> class stackType { public: const stackType<Type>& operator=(const stackType<Type>&); void initializeStack(); bool isEmptyStack(); bool isFullStack(); void destroyStack(); void push(const Type& newItem); Type top(); void pop(); stackType(int stackSize = 100); stackType(const stackType<Type>& otherStack); ~stackType(); private: int maxStackSize; int stackTop; Type *list; void copyStack(const stackType<Type>& otherStack); }; template<class Type> void stackType<Type>::copyStack(const stackType<Type>& otherStack) { delete [] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize]; assert(list != NULL); for(int j = 0; j < stackTop; j++) list[j] = otherStack.list[j]; } template<class Type> stackType<Type>::stackType(const stackType<Type>& otherStack) { list = NULL; copyStack(otherStack); } template<class Type> const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack) { if (this != &otherStack) copyStack(otherStack); return *this; } template<class Type> void stackType<Type>::initializeStack() { stackTop = 0; } template<class Type> void stackType<Type>::destroyStack() { stackTop = 0; } template<class Type> bool stackType<Type>::isEmptyStack() { return(stackTop == maxStackSize); } template<class Type> bool stackType<Type>::isFullStack() { return(stackTop == maxStackSize); } template<class Type> void stackType<Type>::push(const Type& newItem) { if(!isFullStack()) { list[stackTop] = newItem; stackTop++; } else cerr<<"Cannot add to a full stack."<<endl; } template<class Type> Type stackType<Type>::top() { assert(stackTop != 0); return list[stackTop - 1]; } template<class Type> void stackType<Type>::pop() { if(!isEmptyStack()) { stackTop--; } else cerr<<"Cannot remove from an empty stack."<<endl; } template<class Type> stackType<Type>::stackType(int stackSize) { if(stackSize <= 0) { cerr<<"The size of the array to hold the stack must " <<"be positive."<<endl; cerr<<"Creating an array of size 100."<<endl; maxStackSize = 100; } else maxStackSize = stackSize; stackTop = 0; list = new Type[maxStackSize]; } template<class Type> stackType<Type>::~stackType() { delete [] list; } #endif
#ifndef H_StackType #define H_StackType #include <iostream> #include <cassert> using namespace std; template<class Type> class stackType { public: const stackType<Type>& operator=(const stackType<Type>&); void initializeStack(); bool isEmptyStack(); bool isFullStack(); void destroyStack(); void push(const Type& newItem); Type top(); void pop(); stackType(int stackSize = 100); stackType(const stackType<Type>& otherStack); ~stackType(); bool operator==(const stackType<Type>& otherStack); bool operator!=(const stackType<Type>& otherStack); private: int maxStackSize; int stackTop; Type *list; void copyStack(const stackType<Type>& otherStack); }; template<class Type> void stackType<Type>::copyStack(const stackType<Type>& otherStack) { delete [] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize]; assert(list != NULL); for(int j = 0; j < stackTop; j++) list[j] = otherStack.list[j]; } template<class Type> stackType<Type>::stackType(const stackType<Type>& otherStack) { list = NULL; copyStack(otherStack); } template<class Type> const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack) { if (this != &otherStack) copyStack(otherStack); return *this; } template<class Type> void stackType<Type>::initializeStack() { stackTop = 0; } template<class Type> void stackType<Type>::destroyStack() { stackTop = 0; } template<class Type> bool stackType<Type>::isEmptyStack() { return(stackTop == maxStackSize); } template<class Type> bool stackType<Type>::isFullStack() { return(stackTop == maxStackSize); } template<class Type> void stackType<Type>::push(const Type& newItem) { if(!isFullStack()) { list[stackTop] = newItem; stackTop++; } else cerr<<"Cannot add to a full stack."<<endl; } template<class Type> Type stackType<Type>::top() { assert(stackTop != 0); return list[stackTop - 1]; } template<class Type> void stackType<Type>::pop() { if(!isEmptyStack()) { stackTop--; } else cerr<<"Cannot remove from an empty stack."<<endl; } template<class Type> stackType<Type>::stackType(int stackSize) { if(stackSize <= 0) { cerr<<"The size of the array to hold the stack must " <<"be positive."<<endl; cerr<<"Creating an array of size 100."<<endl; maxStackSize = 100; } else maxStackSize = stackSize; stackTop = 0; list = new Type[maxStackSize]; } template<class Type> stackType<Type>::~stackType() { delete [] list; } template<class Type> bool operator== (const stackType<Type>& otherStack) { bool eq=true; } template<class Type> bool operator!= (const stackType<Type>& otherStack) { return !( this == otherStack ); } #endif
Thank you for replying Vernon Dozier.
Is it still necessary to use bool for operator== and operator!= or should i declare the function exactly the same as how I did operator=?
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
bool operator== (const stackType<Type>& otherStack)
bool stackType<Type>& operator==(bool stackType<Type>&); bool stackType<Type>& operator!= (bool stackType<Type>&); template<class Type> bool stackType<Type>::operator==(bool stackType<Type>& otherStack) { bool eq=true; } template<class Type> bool stackType<Type>::operator!=(bool stackType<Type>& otherStack) { return !(this == &otherStack); }
#ifndef H_StackType #define H_StackType #include <iostream> #include <cassert> using namespace std; template<class Type> class stackType { public: const stackType<Type>& operator=(const stackType<Type>&); void initializeStack(); bool isEmptyStack(); bool isFullStack(); void destroyStack(); void push(const Type& newItem); Type top(); void pop(); stackType(int stackSize = 100); stackType(const stackType<Type>& otherStack); ~stackType(); bool stackType<Type>& operator==(bool stackType<Type>&); bool stackType<Type>& operator!= (bool stackType<Type>&); private: int maxStackSize; int stackTop; Type *list; void copyStack(const stackType<Type>& otherStack); }; template<class Type> void stackType<Type>::copyStack(const stackType<Type>& otherStack) { delete [] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize]; assert(list != NULL); for(int j = 0; j < stackTop; j++) list[j] = otherStack.list[j]; } template<class Type> stackType<Type>::stackType(const stackType<Type>& otherStack) { list = NULL; copyStack(otherStack); } template<class Type> const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack) { if (this != &otherStack) copyStack(otherStack); return *this; } template<class Type> void stackType<Type>::initializeStack() { stackTop = 0; } template<class Type> void stackType<Type>::destroyStack() { stackTop = 0; } template<class Type> bool stackType<Type>::isEmptyStack() { return(stackTop == maxStackSize); } template<class Type> bool stackType<Type>::isFullStack() { return(stackTop == maxStackSize); } template<class Type> void stackType<Type>::push(const Type& newItem) { if(!isFullStack()) { list[stackTop] = newItem; stackTop++; } else cerr<<"Cannot add to a full stack."<<endl; } template<class Type> Type stackType<Type>::top() { assert(stackTop != 0); return list[stackTop - 1]; } template<class Type> void stackType<Type>::pop() { if(!isEmptyStack()) { stackTop--; } else cerr<<"Cannot remove from an empty stack."<<endl; } template<class Type> stackType<Type>::stackType(int stackSize) { if(stackSize <= 0) { cerr<<"The size of the array to hold the stack must " <<"be positive."<<endl; cerr<<"Creating an array of size 100."<<endl; maxStackSize = 100; } else maxStackSize = stackSize; stackTop = 0; list = new Type[maxStackSize]; } template<class Type> stackType<Type>::~stackType() { delete [] list; } template<class Type> bool stackType<Type>::operator==(bool stackType<Type>& otherStack) { bool eq=true; } template<class Type> bool stackType<Type>::operator!=(bool stackType<Type>& otherStack) { return !(this == &otherStack); } #endif
template<class Type> bool stackType<Type>::operator== (const stackType<Type>& otherStack) { bool eq=true; } template<class Type> bool stackType<Type>::operator!= (const stackType<Type>& otherStack) { return !( this == otherStack ); }
stackType<Type>:: before the word operator . Everything else stays the same except you do need to change the contents of your == function too since lines 156 - 158 don't do anything and you have no return statement. But your compiler problem goes away if you make the changes above.
#ifndef H_StackType #define H_StackType #include <iostream> #include <cassert> using namespace std; template<class Type> class stackType { public: const stackType<Type>& operator=(const stackType<Type>&); void initializeStack(); bool isEmptyStack(); bool isFullStack(); void destroyStack(); void push(const Type& newItem); Type top(); void pop(); stackType(int stackSize = 100); stackType(const stackType<Type>& otherStack); ~stackType(); bool operator== (const stackType<Type>&); bool operator!= (const stackType<Type>&); private: int maxStackSize; int stackTop; Type *list; void copyStack(const stackType<Type>& otherStack); }; template<class Type> void stackType<Type>::copyStack(const stackType<Type>& otherStack) { delete [] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize]; assert(list != NULL); for(int j = 0; j < stackTop; j++) list[j] = otherStack.list[j]; } template<class Type> stackType<Type>::stackType(const stackType<Type>& otherStack) { list = NULL; copyStack(otherStack); } template<class Type> const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack) { if (this != &otherStack) copyStack(otherStack); return *this; } template<class Type> void stackType<Type>::initializeStack() { stackTop = 0; } template<class Type> void stackType<Type>::destroyStack() { stackTop = 0; } template<class Type> bool stackType<Type>::isEmptyStack() { return(stackTop == maxStackSize); } template<class Type> bool stackType<Type>::isFullStack() { return(stackTop == maxStackSize); } template<class Type> void stackType<Type>::push(const Type& newItem) { if(!isFullStack()) { list[stackTop] = newItem; stackTop++; } else cerr<<"Cannot add to a full stack."<<endl; } template<class Type> Type stackType<Type>::top() { assert(stackTop != 0); return list[stackTop - 1]; } template<class Type> void stackType<Type>::pop() { if(!isEmptyStack()) { stackTop--; } else cerr<<"Cannot remove from an empty stack."<<endl; } template<class Type> stackType<Type>::stackType(int stackSize) { if(stackSize <= 0) { cerr<<"The size of the array to hold the stack must " <<"be positive."<<endl; cerr<<"Creating an array of size 100."<<endl; maxStackSize = 100; } else maxStackSize = stackSize; stackTop = 0; list = new Type[maxStackSize]; } template<class Type> stackType<Type>::~stackType() { delete [] list; } template<class Type> bool stackType<Type>::operator== (const stackType<Type>& otherStack) { if (otherStack.isFullStack() != isFullStack()) cerr<<"They are not the same size"<<endl; for(int j = 0 ; j < isFullStack(); ++j) { if (otherStack.list[j] != list[j]) return false; else return true; } } template<class Type> bool stackType<Type>::operator!= (const stackType<Type>& otherStack) { return !(*this == otherStack); } #endif
#include <iostream> #include <stdlib.h> #include "stack.h" using namespace std; int main() { stackType<int> s1; stackType<int> s2; if (s1 == s2) { cout << "empty stacks are equal" << endl; } s1.push(10); s1.push(20); s2.push(10); cout << "s1: "; cout << "s2: "; cout << "s1 "; if (s1 != s2) cout << "!"; cout << "= s2" << endl; s2.push(20); cout << "s2: "; cout << "s1 "; if (s1 == s2) { cout << "is equal "; } else { cout << "is not equal " << endl; } cout << "s2" << endl; s1.pop(); s1.pop(); s1.push(30); s1.push(20); cout << "s1: "; cout << "s1 "; if (s1 != s2) cout << " is"; cout << " not equal to s2" << endl; system ("pause"); return 0; }
| DaniWeb Message | |
| Cancel Changes | |