The C++ class template program example which is the basic of standard template library (STL) construct

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ class template to push and pop up data and printing them on the standard output in C++ programming

To show: How to use the C++ class template in C++ programming

 

// the C++ class template example

// C++ templates enable you to define a family of functions or classes that can operate on different types of information.

// Can be used as in situations that result in duplication of the same code for multiple types e.g., using function templates

// to create a set of functions that apply the same algorithm to different data types. Using class templates to develop a set

// of typesafe classes. Templates are sometimes a better solution than C macros and void pointers, and they are especially

// useful when working with collections (one of the main uses for templates in MFC) and smart pointers.

#include <iostream>

using namespace std;

 

const int MAXSIZE = 128;

 

// class template

template <class ANY_TYPE> class stack

{

ANY_TYPE array[MAXSIZE];

int stack_pointer;

public:

stack(void)

{

stack_pointer = 0;

};

void push(ANY_TYPE input_data)

{

array[stack_pointer++] = input_data;

};

ANY_TYPE pop(void)

{

return array[--stack_pointer];

};

int empty(void)

{

return (stack_pointer == 0);

};

};

 

// main program

int main(void)

{

int x = 30, y = -10;

double real = 4.2425;

 

stack<int> int_stack;

stack<double> double_stack;

stack<char *> string_stack;

char * name = "Testing, this is a pointer to a string";

 

// storing/pushing data

int_stack.push(x);

int_stack.push(y);

int_stack.push(67);

double_stack.push(real);

double_stack.push(-20.473);

double_stack.push(107.03);

string_stack.push("This is the first line of string");

string_stack.push("This is the second line of string");

string_stack.push("This is the third line of string");

string_stack.push(name);

 

// displaying/popping up data, FILO

cout<<"\n==== C++ class template, displaying data ====\n";

cout<<"\nInteger stack, FILO\n";

cout<<"-------------------\n";

 

cout<<"Access using int_stack.pop(), first time : "<<int_stack.pop()<<"\n";

cout<<"Access using int_stack.pop(), second time: "<<int_stack.pop()<<"\n";

cout<<"Access using int_stack.pop(), third time : "<<int_stack.pop()<<"\n";

cout<<"\ndouble stack, FILO\n";

cout<<"-------------------\n";

 

cout<<"Access using double_stack.pop(), first time : "<<double_stack.pop()<<"\n";

cout<<"Access using double_stack.pop(), second time: "<<double_stack.pop()<<"\n";

cout<<"Access using double_stack.pop(), third time : "<<double_stack.pop()<<"\n";

cout<<"\nString stack, FILO\n";

cout<<"--------------------\n";

 

do

{

cout<<"Access using string_stack.pop(): "<<string_stack.pop()<<"\n";

} while (!string_stack.empty());

 

return 0;

}

 

Output example:

 

==== C++ class template, displaying data ====

Integer stack, FILO

--------------------

Access using int_stack.pop(), first time : 67

Access using int_stack.pop(), second time: -10

Access using int_stack.pop(), third time : 30

 

double stack, FILO

-------------------

Access using double_stack.pop(), first time : 107.03

Access using double_stack.pop(), second time: -20.473

Access using double_stack.pop(), third time : 4.2425

 

String stack, FILO

-------------------

Access using string_stack.pop(): Testing, this is a pointer to a string

Access using string_stack.pop(): This is the third line of string

Access using string_stack.pop(): This is the second line of string

Access using string_stack.pop(): This is the first line of string

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index