First make the methods
Push
,
Pop
and
Display
static; then make the variable
ptrTop a
private static field of the class and the field
ptr
a
private field:
class CStack
{
public:
int data;
private:
static CStack* ptrTop;
CStack* ptr;
public:
static void Push();
static void Pop();
static void Display();
};
CStack* CStack::ptrTop = NULL;
This way you'll have the advantages below:
- you don't need to instantiate a
CStack
object to call the Push
, Pop
and Display
methods
- the
ptr
and ptrTop
variables are protected from being accessed and modified outside the CStack
class code