5 Replies - 2220 Views - Last Post: 10 May 2009 - 09:34 PM Rate Topic: -----

#1 drunkenmonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-May 09

operator to copy an instance of the Stack class

Posted 09 May 2009 - 03:11 PM

I have been working on a program that tests a stack with the usual operators: Push, Pop, Initialize, etc. I am finished except for a new operator my instructor asked for for extra credit. He wants me to writ an operator called Copy that will enable an instance of the Stack class to make a copy of itself in another stack instance. example: StackA.Copy(StackB) will result in stackB holding the same elements as StackA. I have tried several things but none seem to be the right answer. Any pointers would be appreciated.


Project.cpp:

//=================
#include "stdafx.h"
#include <iostream>
#include <string>
//===============

//================
#include "Stack.h"
//================

//==================
using namespace std;
//==================

//  ============
//  Enumerations
//  ===========================================
	enum menuSelections {Push, Pop, Initialize,
						 Print, Purge, Sum, Average, Roll};
//  ===================================

//  ===================
//  Function Prototypes
//  ==========================
	void GetAnswer( string& );
	void Finished( string, bool& );
	void Menu( menuSelections& );
	void ProcessCommand( menuSelections,
						 Stack& );	
//	==============================

//  ===========
	int main( )
	{
//	=====================
//	Variable Declarations
//	=============================
	menuSelections menuSelection;


	bool   finished = false;
	string userAnswer;


	Stack integerValues;
//	====================

//	========================================
//	Welcome Banner and Instructions for User
//	=========================================================
	cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+" << endl;
	cout << "+							 +" << endl;
	cout << "= WELCOME TO THE STACK TESTER =" << endl;
	cout << "+							 +" << endl;
	cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=" << endl;
	cout << endl << endl;

	cout << "THIS PROGRAM ALLOWS THE USER TO CREATE" << endl;
	cout << "AND MANIPULATE A STACK WHOSE VALUES ARE" << endl;
	cout << "HELD BY A STATIC ARRAY." << endl;
	cout << "=+=+=+=+=+=+=+=+=+=+=+=" << endl;
	cout << endl << endl;
//	=====================

//		=========
//		User Loop
//		===================
		while ( !finished ) 
		{
			Menu( menuSelection );
			ProcessCommand( menuSelection,
							integerValues );			

//			===========================
//			User loop termination code.
//			========================
			GetAnswer( userAnswer );
			Finished( userAnswer, finished );
//			=================================

		} // while loop
//		===============


	return 0;
}//End Function Main
//==================


//	==================
//	Function GetAnswer
//	==============================
	void GetAnswer( string& answer )
	{
	bool validAnswer = false;
		while ( !validAnswer ) 
		{
			cout << "Would you like to modify the stack again? (y or n)" 
				 << endl;
			cin >> answer;

			if (( answer.length() == 1 ) && 
			  (( answer[0] == 'y' ) || ( answer[0] == 'n' )))
					validAnswer = true;
		
			else
				cout << "Invalide choice. Please enter (y or n)"
					 << endl;
		}			
	}//End Function GetAnswer
//	=========================

//  =================
//  Function Finished
//  =============================
	void Finished( string answer,
				   bool&  finished )
	{
		if (answer[0] == 'n')
			finished = true;
	}//End Function Finished
//	========================


//  =============
//  Function Menu
//  ==============================================
	void Menu( menuSelections& userMenuSelection )
	{
		int selectionNumber = 1;

		cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+" << endl;
		cout << "MENU FOR TESTING THE STACK" << endl;
		cout << endl;
		cout << "1: Push" << endl;
		cout << "2: Pop" << endl;
		cout << "3: Initialize Stack" << endl;
		cout << "4: Print Stack" << endl;
		cout << "5: Purge Stack" << endl;
		cout << "6: Sum of Stack" << endl;
		cout << "7: Average of Stack" << endl;
		cout << "8: Roll Top Two Elements" << endl;

		cout << endl;

		cout << "What is your selection?" << endl;
		cin >> selectionNumber;

		switch ( selectionNumber )
		{
			case 1: userMenuSelection = Push;
				break;
			case 2: userMenuSelection = Pop;
				break;
			case 3: userMenuSelection = Initialize;
				break;
			case 4: userMenuSelection = Print;
				break;
			case 5: userMenuSelection = Purge;
				break;
			case 6: userMenuSelection = Sum;
				break;
			case 7: userMenuSelection = Average;
				break;
			case 8: userMenuSelection = Roll;
				break;
			default: 
					cout << "Invalid selection." << endl;
					cout << "The selection is being set to 4: Print." << endl;
					userMenuSelection = Print;					
				break;
		}

	}//End Function Menu
//	====================


//  =======================
//  Function ProcessCommand
//  ======================================================
	void ProcessCommand( menuSelections userMenuSelection,
						 Stack& integerValues )
	{
		int integerValue;
		int sum;
		switch( userMenuSelection )
		{
			case Push:
					 cout << "Enter a value to add to the list." << endl;
					 cin >> integerValue;
				 integerValues.Push(integerValue);
				break;
			case Pop: integerValues.Pop(integerValue);
				break;
			case Initialize: integerValues.InitializeStack( );
				break;
			case Print: integerValues.PrintStack( );
				break;
			case Purge: integerValues.PurgeStack( );
				break;
			case Sum: integerValues.SumOfElements( sum );
				break;
			case Average: integerValues.Average( );
				break;
			case Roll: integerValues.Roll( );
				break;
			default: 
					cout << "Invalid selection." << endl;
					cout << "Print member-function invoked." << endl;
					integerValues.PrintStack();				
				break;
		}

	}//End Function ProcessCommand
//	==============================



Stack.h:

//=======
//Stack.h
//=======
//	|
//  \ |	/
//   \|/
//	  V
//  ===========
   #pragma once
//  ===========

//  ===========
	class Stack
	{
//	  =========
//	  Constants
//		======================================
		const static int MAX_STACK_SIZE = 200;
//	  ======================================

//		=======
		public:
//		=======

//			============
//			Constructors
//			=============
			Stack(void);
//			============

//			==========
//			Destructor
//			============
		   ~Stack(void);
//		  ============

//		  ================
//			Member Functions
//		  ==========================
			void Stack::PurgeStack( );
			void Stack::InitializeStack( );
			void Stack::Pop( int& );
			void Stack::PrintStack( );
			void Stack::Push( int );
			bool Stack::StackEmpty( );
			bool Stack::StackFull( );
			int Stack::SumOfElements(int& );
			void Stack::Average( );
			void Stack::Roll( );
//		  =========================

//		========
		private:
//		========

//		  ================
//		  Member variables
//		  ========
			int top;
			int elements[MAX_STACK_SIZE];
//		  =============================

	}; // Class Stack
//  =================

//===========
//End Stack.h
//===========



Stack.cpp:

//=========
//Stack.cpp
//=========
//	|
//  \ |	/
//   \|/
//	  V
//=================
#include "StdAfx.h"
#include <iostream>
#include <iomanip>
//=================

#include "Stack.h"
//================

//==================
using namespace std;
//==================

//  ============
//  Constructors
//  ============
		
//	  ===================
//	  Default Constructor
//		==================== 
		Stack::Stack( void )
		{
			PurgeStack();
		} //End Constructor
//		===================

//  ==========
//  Destructor
//	=====================
	Stack::~Stack( void )
	{

	} //End Destructor
//	==================

//  ================
//  Member-Functions
//  ================
		
//	  ===================
//		Function PurgeStack
//	  =========================
		void Stack::PurgeStack( )
		{
			for (int ii = 0; ii < MAX_STACK_SIZE; ii++)
			{
				elements[ii] = 0;
			} 
			top = 0;
		}//End Function PurgeStack
//	  ==========================

//	  ========================
//		Function InitializeStack
//		==============================
		void Stack::InitializeStack( )
		{
			top = 0;
		}//End Function InitializeStack
//	  ===============================

//	  ============
//		Function Pop
//	  ===================================
		void Stack::Pop( int& poppedValue )
		{
			if (!StackEmpty()){
			top--;
			poppedValue = elements[top];
			PrintStack();
		}
			else
			PrintStack();
		} // Member-Function Pop
//	  ========================

//	  ===================
//	  Function PrintStack
//	  =========================	  
		void Stack::PrintStack( )
		{
			cout << endl << endl;
			cout << "+ Top of Stack  +" << endl;
			cout << "=+=+=+=+=+=+=+=+=" << endl;
			cout << endl;
			if ( StackEmpty() )
				cout << "The stack is empty." << endl;
			else{
				for ( int ii = top - 1; ii >= 0; ii-- ){
					cout << "At index[" << ii << "] = "
						 << elements[ii] << endl;
				} // for
			} // else
			cout << endl;
			cout << "=+=+=+=+=+=+=+=+=" << endl;
			cout << "+Bottom of Stack+" << endl << endl;
		}//End Function PrintStack
//	  ==========================

//	  =============
//		Function Push
//		===================================
		void Stack::Push( int pushedValue ) 
		{
			elements[top] = pushedValue;
			top++;
			PrintStack();
		}//End Function Push
//	  =========================

//	  ===================
//		Function StackEmpty
//	  =========================
		bool Stack::StackEmpty( )
		{
			return (top == 0);
		}//End Function StackEmpty
//	  ==========================

//	  ==================
//		Function StackFull
//	  ========================
		bool Stack::StackFull( )
		{
			return (top == MAX_STACK_SIZE);
		}//End Function StackFull
//	  =========================

//	  ======================
//		Function SumOfElements
//	  ==================================
		int Stack::SumOfElements(int& sum)
		{
			sum = 0;
			for (int ii = 0; ii <= (top-1);ii++)
				sum = sum + elements[ii];
			cout << "Sum = " << sum << endl;
			return sum;
		}//End SumOfElements
//		====================

//	  ================
//		Function Average
//	  ======================
		void Stack::Average( )
		{
			cout << fixed << showpoint << setprecision(2);

			int sum;
			double result = 0;
			cout << endl;
			SumOfElements( sum );
			cout << endl;
			double total = sum;
			double divisor = top;

			if (sum == 0)
				cout << "Average = 0, the stack is empty" << endl << endl;
			else
			{
			result = total/ divisor;
			cout << "Average = " << result << endl << endl;
			}
		}

		void Stack::Roll( )
		{
			if (top == 0)
				cout << "Stack is empty, There are no elements to roll" << endl << endl;
			else if (top == 1)
				cout << "There is only one element in the stack, nothing to roll with" << endl << endl;
			else
			{
			int temp;
			temp = elements[top-1];
			elements[top-1] = elements[top-2];
			elements[top-2] = temp;
			PrintStack( );
			}
		}





//  ====================
//  End Member-Functions
//  =====================

//=============
//End Stack.cpp
//=============



Is This A Good Question/Topic? 0
  • +

Replies To: operator to copy an instance of the Stack class

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4353
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: operator to copy an instance of the Stack class

Posted 09 May 2009 - 07:33 PM

First off, where is your code showing the attempt at a copy method? You said you tried it, show us what you have tried for it.

Second, think of a method called "Copy" belonging to the Stack class and takes in a reference parameter. This reference is going to be the target of a member by member copy.


// Assume you passed in a reference parameter called "StackB"
// Copy elements from current stack to StackB...

for (int i = MAX_STACK_SIZE - 1; i >= 0; i--) {
	 // Read from current elements (from bottom up) and push onto StackB
	 StackB.Push(elements[i]);
}



Using the idea above we are taking the elements of the CURRENT STACK and pushing them onto the other stack that was passed into the Copy() method.

Hopefully you understand now.

Enjoy!

"At DIC we be stack pushing code ninjas... we also push other things including people off bridges, guns in faces, and consulted for the movie 'Push'" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 drunkenmonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-May 09

Re: operator to copy an instance of the Stack class

Posted 09 May 2009 - 09:13 PM

When you say method, do you mean function?. I am a beginner at any kind of programming, and have only studied c++. Secondly I did not include any code for my attempts because my attempts were dead wrong, nothing would compile, and my idea changed the fundemental structure of the program. I will post when I have attempted to write with your suggestion. Thanks so much
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: operator to copy an instance of the Stack class

Posted 10 May 2009 - 12:26 AM

View Postdrunkenmonk, on 9 May, 2009 - 08:13 PM, said:

When you say method, do you mean function?


Pretty much.

A method is the name given to what you might call a function, in other settings, when we are talking about classes.
Was This Post Helpful? 0
  • +
  • -

#5 drunkenmonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-May 09

Re: operator to copy an instance of the Stack class

Posted 10 May 2009 - 07:29 PM

Here is my new code, doesn't seem to be working correctly:

Project.cpp:

	 
//
//=================
#include "stdafx.h"
#include <iostream>
#include <string>
//===============

//================
#include "Stack.h"
//================

//==================
using namespace std;
//==================

//  ============
//  Enumerations
//  ===========================================
	enum menuSelections {Push, Pop, Initialize,
						 Print, Purge, Sum, Average, Roll, Copy};
//  ===================================

//  ===================
//  Function Prototypes
//  ==========================
	void GetAnswer( string& );
	void Finished( string, bool& );
	void Menu( menuSelections& );
	void ProcessCommandA( menuSelections,
						 Stack& );
	void ProcessCommandB( menuSelections,
						 Stack& );
//	==============================

//  ===========
	int main( )
	{
//	=====================
//	Variable Declarations
//	=============================
	menuSelections menuSelection;


	bool   finished = false;
	string userAnswer;
	char stackChoice;


	Stack stackA;
	Stack stackB;
//	====================

//	========================================
//	Welcome Banner and Instructions for User
//	=========================================================
	cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+" << endl;
	cout << "+							 +" << endl;
	cout << "= WELCOME TO THE STACK TESTER =" << endl;
	cout << "+							 +" << endl;
	cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=" << endl;
	cout << endl << endl;

	cout << "THIS PROGRAM ALLOWS THE USER TO CREATE" << endl;
	cout << "AND MANIPULATE A STACK WHOSE VALUES ARE" << endl;
	cout << "HELD BY A STATIC ARRAY." << endl;
	cout << "=+=+=+=+=+=+=+=+=+=+=+=" << endl;
	cout << endl << endl;
//	=====================

//		=========
//		User Loop
//		===================
		while ( !finished ) 
		{
			Menu( menuSelection );



			cout << "Which Stack?(a/b)" << endl;
			cin >> stackChoice;


			if (stackChoice == 'a')
				ProcessCommandA( menuSelection,
								stackA);
			else if (stackChoice == 'b')

			ProcessCommandB( menuSelection,
							stackB );
			else
			{
				cout << "Bad Choice, try again:" << endl;
			cin >> stackChoice;
			}


//			===========================
//			User loop termination code.
//			========================
			GetAnswer( userAnswer );
			Finished( userAnswer, finished );
//			=================================

		} // while loop
//		===============


	return 0;
}//End Function Main
//==================


//	==================
//	Function GetAnswer
//	==============================
	void GetAnswer( string& answer )
	{
	bool validAnswer = false;
		while ( !validAnswer ) 
		{
			cout << "Would you like to modify one of the stacks again? (y or n)" 
				 << endl;
			cin >> answer;

			if (( answer.length() == 1 ) && 
			  (( answer[0] == 'y' ) || ( answer[0] == 'n' )))
					validAnswer = true;
		
			else
				cout << "Invalide choice. Please enter (y or n)"
					 << endl;
		}			
	}//End Function GetAnswer
//	=========================

//  =================
//  Function Finished
//  =============================
	void Finished( string answer,
				   bool&  finished )
	{
		if (answer[0] == 'n')
			finished = true;
	}//End Function Finished
//	========================


//  =============
//  Function Menu
//  ==============================================
	void Menu( menuSelections& userMenuSelection )
	{
		int selectionNumber = 1;

		cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+" << endl;
		cout << "MENU FOR TESTING THE STACK" << endl;
		cout << endl;
		cout << "1: Push" << endl;
		cout << "2: Pop" << endl;
		cout << "3: Initialize Stack" << endl;
		cout << "4: Print Stack" << endl;
		cout << "5: Purge Stack" << endl;
		cout << "6: Sum of Stack" << endl;
		cout << "7: Average of Stack" << endl;
		cout << "8: Roll Top Two Elements" << endl;
		cout << "9: Copy stack to other instance" << endl;

		cout << endl;

		cout << "What is your selection?" << endl;
		cin >> selectionNumber;

		switch ( selectionNumber )
		{
			case 1: userMenuSelection = Push;
				break;
			case 2: userMenuSelection = Pop;
				break;
			case 3: userMenuSelection = Initialize;
				break;
			case 4: userMenuSelection = Print;
				break;
			case 5: userMenuSelection = Purge;
				break;
			case 6: userMenuSelection = Sum;
				break;
			case 7: userMenuSelection = Average;
				break;
			case 8: userMenuSelection = Roll;
				break;
			case 9: userMenuSelection = Copy;
				break;
			default: 
					cout << "Invalid selection." << endl;
					cout << "The selection is being set to 4: Print." << endl;
					userMenuSelection = Print;					
				break;
		}

	}//End Function Menu
//	====================


//  ========================
//  Function ProcessCommandA
//  ======================================================
	void ProcessCommandA( menuSelections userMenuSelection,
						 Stack& stackA )
	{
		Stack stackB;
		int integerValue;
		int sum;
		switch( userMenuSelection )
		{
			case Push:
					 cout << "Enter a value to add to the list." << endl;
					 cin >> integerValue;
				 stackA.Push(integerValue);
				break;
			case Pop: stackA.Pop(integerValue);
				break;
			case Initialize: stackA.InitializeStack( );
				break;
			case Print: stackA.PrintStack( );
				break;
			case Purge: stackA.PurgeStack( );
				break;
			case Sum: stackA.SumOfElements( sum );
				break;
			case Average: stackA.Average( );
				break;
			case Roll: stackA.Roll( );
				break;
			case Copy: stackA.Copy( stackB );
				break;
			default: 
					cout << "Invalid selection." << endl;
					cout << "Print member-function invoked." << endl;
					stackA.PrintStack();				
				break;
		}

	}//End Function ProcessCommandA
//	===============================


//  ========================
//  Function ProcessCommandA
//  ======================================================
	void ProcessCommandB( menuSelections userMenuSelection,
						 Stack& stackB )
	{
		Stack stackA;
		int integerValue;
		int sum;
		switch( userMenuSelection )
		{
			case Push:
					 cout << "Enter a value to add to the list." << endl;
					 cin >> integerValue;
				 stackB.Push(integerValue);
				break;
			case Pop: stackB.Pop(integerValue);
				break;
			case Initialize: stackB.InitializeStack( );
				break;
			case Print: stackB.PrintStack( );
				break;
			case Purge: stackB.PurgeStack( );
				break;
			case Sum: stackB.SumOfElements( sum );
				break;
			case Average: stackB.Average( );
				break;
			case Roll: stackB.Roll( );
				break;
			case Copy: stackB.Copy( stackA );
				break;
			default: 
					cout << "Invalid selection." << endl;
					cout << "Print member-function invoked." << endl;
					stackB.PrintStack();				
				break;
		}

	}//End Function ProcessCommandB
//	===============================



Stack.h:


//=======
//Stack.h
//=======
//	|
//  \ |	/
//   \|/
//	  V
//  ===========
   #pragma once
//  ===========

//  ===========
	class Stack
	{
//	  =========
//	  Constants
//		======================================
		const static int MAX_STACK_SIZE = 200;
//	  ======================================

//		=======
		public:
//		=======

//			============
//			Constructors
//			=============
			Stack(void);
//			============

//			==========
//			Destructor
//			============
		   ~Stack(void);
//		  ============

//		  ================
//			Member Functions
//		  ==========================
			void Stack::PurgeStack( );
			void Stack::InitializeStack( );
			void Stack::Pop( int& );
			void Stack::PrintStack( );
			void Stack::Push( int );
			bool Stack::StackEmpty( );
			bool Stack::StackFull( );
			int Stack::SumOfElements(int& );
			void Stack::Average( );
			void Stack::Roll( );
			void Stack::Copy( Stack& );
//		  =========================

//		========
		private:
//		========

//		  ================
//		  Member variables
//		  ========
			int top;
			int elements[MAX_STACK_SIZE];

//		  =============================

	}; // Class Stack
//  =================

//===========
//End Stack.h
//===========



Stack.cpp:

//=========
//Stack.cpp
//=========
//	|
//  \ |	/
//   \|/
//	  V
//=================
#include "StdAfx.h"
#include <iostream>
#include <iomanip>
//=================

#include "Stack.h"
//================

//==================
using namespace std;
//==================

//  ============
//  Constructors
//  ============
		
//	  ===================
//	  Default Constructor
//		==================== 
		Stack::Stack( void )
		{
			PurgeStack();
		} //End Constructor
//		===================

//  ==========
//  Destructor
//	=====================
	Stack::~Stack( void )
	{

	} //End Destructor
//	==================

//  ================
//  Member-Functions
//  ================
		
//	  ===================
//		Function PurgeStack
//	  =========================
		void Stack::PurgeStack( )
		{
			for (int ii = 0; ii < MAX_STACK_SIZE; ii++)
			{
				elements[ii] = 0;
			} 
			top = 0;
		}//End Function PurgeStack
//	  ==========================

//	  ========================
//		Function InitializeStack
//		==============================
		void Stack::InitializeStack( )
		{
			top = 0;
		}//End Function InitializeStack
//	  ===============================

//	  ============
//		Function Pop
//	  ===================================
		void Stack::Pop( int& poppedValue )
		{
			if (!StackEmpty()){
			top--;
			poppedValue = elements[top];
			PrintStack();
		}
			else
			PrintStack();
		} // Member-Function Pop
//	  ========================

//	  ===================
//	  Function PrintStack
//	  =========================	  
		void Stack::PrintStack( )
		{
			cout << endl << endl;
			cout << "+ Top of Stack  +" << endl;
			cout << "=+=+=+=+=+=+=+=+=" << endl;
			cout << endl;
			if ( StackEmpty() )
				cout << "The stack is empty." << endl;
			else{
				for ( int ii = top - 1; ii >= 0; ii-- ){
					cout << "At index[" << ii << "] = "
						 << elements[ii] << endl;
				} // for
			} // else
			cout << endl;
			cout << "=+=+=+=+=+=+=+=+=" << endl;
			cout << "+Bottom of Stack+" << endl << endl;
		}//End Function PrintStack
//	  ==========================

//	  =============
//		Function Push
//		===================================
		void Stack::Push( int pushedValue ) 
		{
			elements[top] = pushedValue;
			top++;
			PrintStack();
		}//End Function Push
//	  =========================

//	  ===================
//		Function StackEmpty
//	  =========================
		bool Stack::StackEmpty( )
		{
			return (top == 0);
		}//End Function StackEmpty
//	  ==========================

//	  ==================
//		Function StackFull
//	  ========================
		bool Stack::StackFull( )
		{
			return (top == MAX_STACK_SIZE);
		}//End Function StackFull
//	  =========================

//	  ======================
//		Function SumOfElements
//	  ==================================
		int Stack::SumOfElements(int& sum)
		{
			sum = 0;
			for (int ii = 0; ii <= (top-1);ii++)
				sum = sum + elements[ii];
			cout << "Sum = " << sum << endl;
			return sum;
		}//End SumOfElements
//		====================

//	  ================
//		Function Average
//	  ======================
		void Stack::Average( )
		{
			cout << fixed << showpoint << setprecision(2);

			int sum;
			double result = 0;
			cout << endl;
			SumOfElements( sum );
			cout << endl;
			double total = sum;
			double divisor = top;

			if (sum == 0)
				cout << "Average = 0, the stack is empty" << endl << endl;
			else
			{
			result = total/ divisor;
			cout << "Average = " << result << endl << endl;
			}
		}

		void Stack::Roll( )
		{
			if (top == 0)
				cout << "Stack is empty, There are no elements to roll" << endl << endl;
			else if (top == 1)
				cout << "There is only one element in the stack, nothing to roll with" << endl << endl;
			else
			{
			int temp;
			temp = elements[top-1];
			elements[top-1] = elements[top-2];
			elements[top-2] = temp;
			PrintStack( );
			}
		}

		 void Stack::Copy(Stack& stackB )
		{
			for (int ii = top-1; ii >= 0; ii--) {
				stackB.Push(elements[ii]);
		}
		}





//  ====================
//  End Member-Functions
//  =====================

//=============
//End Stack.cpp
//=============



Any nudge in the right direction would be appreciated.
Was This Post Helpful? 0
  • +
  • -

#6 drunkenmonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-May 09

Re: operator to copy an instance of the Stack class

Posted 10 May 2009 - 09:34 PM

Ive tried all these:

		  void Stack::Copy(Stack otherStack)
		{

			otherStack.top = top;
							
			for (int ii = 0; ii < top; ii++) {
				otherStack.elements[ii] = elements[ii];
		}
		
		}



		  void Stack::Copy(Stack otherStack)
		{

			otherStack.top = top;
							
			for (int ii = 0; ii < top; ii++) {
				otherStack.Push(elements[ii]);
		}
		
		}



I am stumped!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1