Question

overload the relational operator == for the class stacktype

How do overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also write thedefintion of the function template to overload this operator.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Header file : myStack.h

#ifndef H_StackType

#define H_StackType

#include <iostream>

#include<cassert>

using namespace std;

//Definition of the template class stackType

template <class Type>

class stackType

{ private :

int maxStackSize;

int stackTop;

Type *list;

public :

void initializeStack();

bool isFullStack() const;

bool isEmptyStack() const;

void push( const Type&);

void pop();

Type top() const;

stackType( int = 20 );

~stackType();

bool operator==( conststackType<Type>&);

};

template <class Type>

void stackType<Type>::initializeStack()

{

stackTop = 0;

}

template <class Type>

bool stackType<Type>::isFullStack() const

{

return ( stackTop == maxStackSize );

}

template <class Type>

bool stackType<Type>::isEmptyStack() const

{

return ( stackTop == 0 );

}

template <class Type>

void stackType<Type>::push( const Type&newItem )

{

if ( !isFullStack() )

{

list[ stackTop ] = newItem;

stackTop++;

}else

cout << "ntCan not add to a full stack";

}

template <class Type>

void stackType<Type>::pop()

{

if ( !isEmptyStack() )

stackTop--;

else

cout << "ntCan not remove from an empty stack";

}

template <class Type>

Type stackType<Type>::top() const

{

assert( stackTop != 0 );

return list[ stackTop - 1 ];

}

template <class Type>

stackType<Type>::stackType( int stackSize )

{

if ( stackSize <= 0 )

{

cout << "Invalid size";

stackSize = 10;

}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>&right )

{

if (this->stackTop != right.stackTop )

return false;

for ( int i = 0; i < stackTop; i++)

if ( this->list[ i ] != right.list[ i ] )

return false;

return true;

}

#endif

intmain()

{

cout << "nntProgram to overload the realtional" << "operator == for the class stackType.";

stackType<int> s1( 12 );

stackType<int> s2( 15 );

cout << "nntInserting elements 5, 10, 15 ... "<< "to both the stacks.";

for ( int i = 5; i < 50; i+=5 )

{

s1.push( i );

s2.push( i );

}

if ( s1 == s2 )

cout << "ntBoth the stacks are equal";

else

cout << "ntBoth the stacks are not equal";

cout<<"ntInserting element 11 to the secondstack.";

s2.push( 11 );

if ( s1 == s2 )

cout << "ntBoth the stacks are equal";

else

cout << "ntBoth the stacks are not equal";

cout << "nnt";

system("pause");

return 0;

}I hope this will helps to You!
answered by: Sheshe
Add a comment
Know the answer?
Add Answer to:
overload the relational operator == for the class stacktype
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT