Book example on classes/stacks.

How accessing private data works.

Page 1 of 1

2 Replies - 536 Views - Last Post: 25 October 2010 - 02:20 PM Rate Topic: -----

#1 Riskinit  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 157
  • Joined: 14-January 10

Book example on classes/stacks.

Posted 25 October 2010 - 11:11 AM

Below is an example from a book we are reading for class.
template<class TYPE>
struct NODE
{
	TYPE data;
};

template <class TYPE>
class myClassStack
{
public:
	myClassStack(int size = 100);
	~myClassStack (void);
	bool pushStack (TYPE dataIn);
	bool popStack (TYPE& dataOut);
	bool stackTop (TYPE& dataOut);
	bool emptyStack (void);
	bool fullStack (void);
	int stackCount (void);
private:
	NODE<TYPE> *stackAry;
	int count;
	int stackMax;
	int top;
};


I get how class templates work or at least I thought I did. But with the lines
bool pushStack (TYPE dataIn);
bool popStack (TYPE& dataOut);
bool stackTop (TYPE& dataOut);


exactly how do they work? I know they return a true or false value and they access a function. And I even get why we say TYPE. But I don't get where the dataIn, dataOut is coming from.

In my "int main()" what would I enter for a parameter exactly?
For example if I have an instance called myObject.
int main(void)
{

	myClassStack<int> myObject;            //instance of myClassStack
	myObject.pushStack(5);                 //I am putting the first value of my stack in.
	myObject.stackTop(WHAT_VALUE_GOES_IN_HERE);   //Now I want to access my stack but I can't.
	std::cout << myObject;                 //And finally I am trying to display that top value.

	return 0;
}


Thanks in advance for tips/ help.

Is This A Good Question/Topic? 0
  • +

Replies To: Book example on classes/stacks.

#2 oscode  Icon User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: Book example on classes/stacks.

Posted 25 October 2010 - 11:19 AM

In this case, popStack and stackTop accept an object as an out parameter, this means it is passed in to be given a value. It is passed by reference, so it is modifying the actual object passed in.

Take this as an example:

#include <iostream>

void a(int num)
{
	num++;
}

void b(int& num)
{
	num++;
}

int main()
{
	int i = 0;

	a(i);
	std::cout << i << "\n";
	b(i);
	std::cout << i << "\n";

	return 0;
}


Was This Post Helpful? 1
  • +
  • -

#3 Riskinit  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 157
  • Joined: 14-January 10

Re: Book example on classes/stacks.

Posted 25 October 2010 - 02:20 PM

View Postoscode, on 25 October 2010 - 10:19 AM, said:

In this case, popStack and stackTop accept an object as an out parameter, this means it is passed in to be given a value. It is passed by reference, so it is modifying the actual object passed in.


Ok wait. Let me confirm something.


#include <iostream>

void a(int num)
{
	num++;
}

void b(int& num)
{
	num++;
}

int main()
{
	int i = 0;

	a(i);
	std::cout << i << "\n";
	b(i);
	std::cout << i << "\n";

	return 0;
}


In main when you say
a(i);

That means send the int '0' to the function and the function returns 0. Correct?
Then when you say b(i), because the function accepts the address of i, the function modifies it directly???

I must say that is amazing!

However I am still not putting everything together...
int main(void)
{

	myClassStack<int> myObject;            //instance of myClassStack
	myObject.pushStack(5);                 //I am putting the first value of my stack in.
	myObject.stackTop(WHAT_VALUE_GOES_IN_HERE);   //Now I want to access my stack but I can't.
	std::cout << myObject;                 //And finally I am trying to display that top value.

	return 0;
}


Does this mean I would need to declare a variable dataOut in main? Like this...
int main(void)
{
        int dataOut = 0;
	myClassStack<int> myObject;            //instance of myClassStack
	myObject.pushStack(5);                 //I am putting the first value of my stack in.
	myObject.stackTop(dataOut);   //Now I want to access my stack but I can't.
	std::cout << dataOut;                 //And finally I am trying to display that top value.

	return 0;
}

OMG... the program works... I have actually learned something today. Thank you oscode.


This post has been edited by Riskinit: 25 October 2010 - 02:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1