copy an instance of a Stack class?

Hello, I am trying to write a program that manipulates a stack containing integers. I have written al the usual: Push, Pop, Print, etc.. My last operator is called Copy. When called (stackA.Copy( stackB ) whatever was in stackA is deep copied into stackB. here is my code:


Stack.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

//=======
//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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//=========
//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& otherStack)
		{
			 top = otherStack.top;


			for ( int ii = top-1; ii >=0 ; ii--){

			elements[ii] = otherStack.elements[ii];
			}
	

		
		}





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

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


Any help would be appreciated!!!

Your stack is a stack of ints held in a fixed array. You can do a bitwise copy.
1
2
3
4
5
6
7
void Stack::Copy(const Stack& otherStack)
{
    if (this != &otherStack)
        *this = otherStack;

    return *this;
}
Topic archived. No new replies allowed.