Here is the interface
template <class Type>
class stackADT
{
public:
virtual void initializeStack() = 0;
virtual bool isEmptyStack() const = 0;
virtual bool isFullStack() const = 0;
virtual void push(const Type& newItem) = 0;
virtual Type top() const = 0;
virtual void pop() = 0;
virtual void reverseStack(stackType<Type>& otherStack) = 0;
private:
int maxStackSize;
int stackTop;
Type *list;
};
Here is the reverse stack method which is part of class stackType which extends stackADT
template <class Type>
class stackType : public stackADT<Type>
{
private:
int maxStackSize;
int stackTop;
Type *list;
public:
/***
Other methods ...
**/
void reverseStack(stackType<Type>& otherStack)
{
int count = 0;
otherStack.list = new Type[maxStackSize]; // why does this WORK!!! its private
otherStack.stackTop = 0; // why does this WORK!!! its private
//copy otherStack into this stack.
for (int j = stackTop - 1; j >= 0; j--)
{
otherStack.push(list[j]);
count++;
}
}
Here is the main loop with the calls.
stackType<int> stack1(50);
stackType<int> stack2(50);
stack1.initializeStack();
stack1.push(1);
stack1.push(2);
stack1.push(3);
stack1.push(4);
stack1.push(5);
stack1.reverseStack(stack2);
So what's going on with this in C++ cause in Java, PHP, Python(mangled naming) and other OOD would not allow this.
I guess you are confused what private actually does because this would also work in Java.
Private means that instances of other classes (or no classes, meaning functions) can't change/call/invoke/... that member/method. The important part here is that it says other classes. Instances of the same class can change/call/invoke/... private members/methods.