
The template class starts with this code: template
template
class is regarded as the type name of the variable. The variable accepts the type as its value. Type is regarded as the name of the variable.
Put the template information in a header file and create stacktp.h
#ifndef STACKTP_H_
#define STACKTP_H_
// 建立模板
template<class Type>
class Stack
{
private:
enum {MAX=10};
Type items[MAX];
int top;
public:
Stack();
bool isempty();
bool isfull();
bool push(const Type & item);
bool pop(Type & item);
};
template<class Type>
Stack<Type>::Stack()
{
top=10;
}
template<class Type>
bool Stack<Type>::isempty()
{
return top==0;
}
template<class Type>
bool Stack<Type>::isfull()
{
return top==MAX;
}
template<class Type>
bool Stack<Type>::push(const Type &item)
{
if(top<MAX)
{
items[top++]=item;
return true;
}
else
return false;
}
template<class Type>
bool Stack<Type>::pop(Type & item)
{
if(top>0)
{
item=items[--top];
return true;
}
else
return false;
}
#endifRelated recommendations: "FAQ"
Create the source file stacktem .cpp;
#include<iostream>
#include<string>
#include<cctype>
#include"stacktp.h"
using namespace std;
int main()
{
Stack<string> st;// 创建一个空的stack,和前面的模板联系起来
char ch;
string po;
cout<<"Please enter A to add a purchase order.\n"
<<"P to precess a PO,or Q to quit."<<endl;
while(cin>>ch && toupper(ch)!='Q' )
{
while(cin.get()!='\n')
{
continue;
}
if(!isalpha(ch))
{
cout<<'\a';
continue;
}
switch(ch)
{
case 'A':
case 'a':cout<<"Enter a PO number to add:"<<endl;
cin>>po;
if(st.isfull())
{
cout<<"stack already full"<<endl;
}
else
{
st.push(po);
}
break;
case 'P':
case 'p':
if(st.isempty())
{
cout<<"stack already empty"<<endl;
}
else
{
st.pop(po);
cout<<"PO #"<<po<<" popped\n";
break;
}
}
cout<<"Please enter A to add a purchase order,\n"
<<"P to process a PO,or Q to quit.\n";
}
cout<<"Bye!"<<endl;
return 0;
}
The above is the detailed content of What does template mean?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.





