Page 1 of 1

C++ BEGINNER STL STACK CLASS TUTORIAL C++ BEGINNER STL STACK CLASS TUTORIAL Rate Topic: -----

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 29 July 2009 - 12:53 AM

C++ BEGINNER STL STACK CLASS TUTORIAL


CONTENTS:
I. ASSUMPTIONS
II. INTRODUCTION
III. VOCABULARY
IV. CLASS TEMPLATE STACK<T>
V. MEMBER FUNCTIONS
VI. CONSTRUCTOR

WHAT YOU WILL LEARN IN THIS TUTORIAL:

1. You will learn stack class template vocabulary.

2. You will learn how to declare and use a stack class template constructor.

3. You will learn how to instantiate objects of type stack.

4. You will learn the stack class member functions and how to use them.

I. ASSUMPTIONS

I assume you have a C++ compiler and you want to learn about the C++ STL stack class.

II. INTRODUCTION

Hello; nice to meet you! Welcome to the C++ Beginner STL Stack Class Tutorial.

Please copy and paste the attached example program into a file your compiler can read. Then build, compile, and debug the example program. Following along, back and forth, between the tutorial and your copy of the example program will help you understand the example program functionality.

//******************************************************
//C++ Beginner STL Stack Class Tutorial
//Featuring a gold coin counting class template stack<T>
//******************************************************
#include <deque>
#include <exception>
#include <iostream>
#include <stack>

using namespace std;

template< typename T>
class CStack
{
	public:
		
		CStack( int = 5 );  //Default constructor with stack size 5.
		~CStack()  //Destructor.
		{
			delete [] CStackPtr;
		}

		bool push( const T & );
		bool pop ( T & );

		bool isEmpty( ) const
		{
			return top == -1;
		}

		bool isFull( ) const
		{
			return top == max -1;
		}
			  
	private:

		int max;
		int top;
		T *CStackPtr;
};

template< typename T >  //Template constructor with stack size 5.
CStack< T >::CStack( int cs )
: max( cs > 0 ? cs : 5 ),
	top( -1 ),
	CStackPtr( new T[ max ])
{

}

template< typename T > 
bool CStack< T >::push( const T &pushValue )
{
	if ( !isFull() )
	{
		CStackPtr[ ++top ] = pushValue;
		return true;
	}

	return false;
}

template< typename T > 
bool CStack< T >::pop( T &popValue )
{
	if ( !isEmpty() )
	{
		popValue = CStackPtr[ top-- ];
		return true;
	}

	return false;
}

//************************************
// MAIN() FUNCTION.
//************************************
int main(int argc, char* argv[])
{
	int	 choice   =	0;
	bool	exit	=	false;
	//************************************
	
	do
	{
		CStack< double > doubleCStack( 10 );  //Instantiate object doubleCStack of type CStack.
		double doubleValue = 1.0;

		system("CLS");
		cout << endl << endl << endl;
		cout << "  This is a rich person's gold stacking program." << endl << endl;
		cout << "  The program stacks and unstacks gold coins." << endl << endl;
		cout << "  The first stack is a double and holds 10 gold coins." << endl << endl;
		cout << "  The second stack is an int and holds 5 gold coins." << endl << endl;
		cout << endl << endl;
		cout << "  Pushing gold coins onto the double stack." << endl;
		cout << endl << endl;
		cout << "  ";
		
		while ( doubleCStack.push( doubleValue ))
		{
			cout << doubleValue << "  ";
			doubleValue += 1.0;
		}
		
		cout << endl << endl;
		cout << "  Double stack is now full." << endl << endl;
		cout << "  Cannot push any more gold coins onto double stack!" << endl << endl;
		cout << endl << endl;
		cout << "  ";
		system("PAUSE");

		system("CLS");
		cout << endl << endl << endl;
		cout << "  Popping gold coins from double stack." << endl;
		cout << endl << endl;
		cout << "  ";
		
		while ( doubleCStack.pop( doubleValue ))
		{
			cout << doubleValue << "  ";
		}

		cout << endl;
		cout << "  ";
		
		cout << endl;
		cout << "  Double stack is now empty." << endl << endl;
		cout << "  Cannot pop any more gold coins from double stack!" << endl << endl;
		cout << endl << endl;
		cout << "  ";
		system("PAUSE");
		cout << endl << endl << endl;
		cout << "  ";

		CStack< int > intCStack;  //Instantiate object intCStack of type CStack.
		int intValue = 1;
		
		system("CLS");
		cout << endl << endl << endl;
		cout << "  Pushing gold coins onto int stack." << endl;
		
		cout << endl << endl;
		cout << "  ";

		while ( intCStack.push( intValue ))
		{
			cout << intValue++ << "  ";
		}

		cout << endl << endl;
		cout << "  Int stack is now full."  << endl << endl << endl;
		cout << "  Cannot push any more gold coins onto int stack!" << endl << endl;

		cout << endl << endl;
		cout << "  ";
		system("PAUSE");

		system("CLS");
		cout << endl << endl << endl;
		cout << "  Popping gold coins from int stack." << endl;
		
		cout << endl << endl;
		cout << "  ";

		while ( intCStack.pop( intValue ))
		{
			cout << intValue << "  ";
		}
		
		cout << endl << endl;
		cout << "  Int stack is now empty." << endl << endl;
		cout << "  Cannot pop any more gold coins from int stack!" << endl << endl;
		cout << endl << endl;
		cout << "  ";
		system("PAUSE");
		cout << endl << endl << endl;
		cout << "  ";

		
		//************************************
		system( "CLS" );
		cout << endl << endl << endl;
		cout << "  Type the number 1 and press return ";
		cout << "to run the program again.  " << endl << endl;
		cout << "  Type 0 and press return to exit. ==> ";
		cin >> choice;
		if ( choice == 0 )
		{
			EXIT_SUCCESS;
		}
		if ( choice == 1 )
			exit = false;
		else
		{
			exit = true;
		}
	}while( exit == false );

	return EXIT_SUCCESS;
}



III. VOCABULARY

1. What is LIFO?

LIFO is last-in-first-out. A stack enforces LIFO order.
A stack of plates is a common visualization of a STL stack. The last plate placed on a stack of plates is normally the first plate removed from the stack of plates.

2. What is a generic function or function template?

A generic function or function template is a function definition preceded by a template statement.

template< typename T > 
bool CStack< T >::push( const T &pushValue )
{
	if ( !isFull() )
	{
		CStackPtr[ ++top ] = pushValue;
		return true;
	}

	return false;
}



A compiler generated function specialization is an instantiation of a function template.

A function template declaration consists of the keyword template, followed by a list of template arguments enclosed in angle brackets, followed by a function declaration.

All versions of a generic function must perform the same actions, only the data type can be different. For example, a sorting algorithm is the same no mater what type of data is being sorted. You can use a generic function any time you see a situation where the logic of a function is independent of the data type. Defining a single function template defines an entire family of overloaded functions.

3. What is an explicit specialization?

A generic function overloads itself. However, the programmer can code an explicit specialization to overload a generic function and override the compiler generated function to accommodate unique situations.

4. What is a generic class?

Class is the generic term for type. A generic class is a class that uses logic that can be generalized. When objects of a generic class are created the type of data is specified as a parameter. Upon instantiation the compiler generates the correct type of object based on the parameter type specified. A generic class allows you to define the general form of an object that can then be used with any data type.

By making stack a generic class the compiler can create a stack for any type of data.

5. What is typename?

The keyword typename can be substituted for the keyword class in template declarations. The keyword typename also informs the compiler a type name is being used in a template declaration instead of an object name.

template< typename T > 
bool CStack< T >::pop( T &popValue )
{
	if ( !isEmpty() )
	{
		popValue = CStackPtr[ top-- ];
		return true;
	}

	return false;
}



The keyword class connotes a user defined type while the keyword typename is more neutral and refers to fundamental types and user-defined types.

6. What is a class template?

A class template is a template for producing classes. A class produced from a template is said to be a class template. A template class is an instance of a class template.

Member function definitions for class templates are preceded by the template keyword with parameters in angle brackets.

7. What are container adapters?

The STL provides three container adapters: stack, queue, and priority queue. Container adapters do not support iterators. Container adapters allow you to choose an underlying data structure.

IV. CLASS TEMPLATE STACK<T>

To instantiate a stack a data type must be specified.
The template specifications for a stack are as follows:
template <class T, class Container = deque<T>> class stack

template< typename T >  //Template constructor with stack size 5.
CStack< T >::CStack( int cs )
: max( cs > 0 ? cs : 5 ),
	top( -1 ),
	CStackPtr( new T[ max ])
{

}



V. MEMBER FUNCTIONS

The main behavior of a stack is that objects go on and come off of the top of the stack. The following are STL stack class public member functions. Other underlying deque, vector, or list container member functions such as iterators, push_back, and insert been suppressed to maintain stack behavior complance.

1. void push(const value_type &val);

Inserts an element at the top of a stack. This element becomes the last in the stack.

template< typename T > 
bool CStack< T >::push( const T &pushValue )
{
	if ( !isFull() )
	{
		CStackPtr[ ++top ] = pushValue;
		return true;
	}

	return false;
}



2. void pop();

Removes, deletes, the element at the top of a stack, the last element in the container.

template< typename T > 
bool CStack< T >::pop( T &popValue )
{
	if ( !isEmpty() )
	{
		popValue = CStackPtr[ top-- ];
		return true;
	}

	return false;
}



3. bool empty() const;

Tests whether the stack is empty. Returns true if the stack is empty and false otherwise.

bool isEmpty( ) const
{
	return top == -1;
}




4. size_type size() const;

Returns the number of elements currently in the stack.

5. value_type &top(); const value_type &top() const;

Returns a reference to the top most element in the stack. An undefined value is returned if the stack is empty.

if ( !isFull() )
{
	CStackPtr[ ++top ] = pushValue;
	return true;
}



VI. CONSTRUCTOR

The stack constructor uses the following syntax to create an empty stack using deque as the default container:

explicit stack(const Container &cnt = Container());

template< typename T >  //Template constructor with stack size 5.
CStack< T >::CStack( int cs )
: max( cs > 0 ? cs : 5 ),
	top( -1 ),
	CStackPtr( new T[ max ])
{

}



The container is held in a protected member called c of type Container.
The ==, <, <=, !=, >, and >= comparison operators are defined for stack.

WHAT YOU HAVE LEARNED IN THIS TUTORIAL:

1. You have learned stack class template vocabulary.

2. You have learned how to declare and use a stack class template constructor.

3. You have learned how to instantiate objects of type stack.

4. You have learned the stack class member functions and how to use them.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1