模板提供了泛型,就是将数据类型作为一个参数传递给用于构建类或函数
格式:
template <typename Type> // newer choice
当一个模板被触发后,Type将会被一个具体的数据类型or类代替(这个过程成为模板类的实现)。
成员函数与模板声明:在定义模板类时,每个成员函数之前都必须有同样的模板声明:
template <typename Type>
内联函数不需要,因为内联函数定义在类声明中。
stacktp.h
#pragma once
// stacktp.h -- a stack template
#ifndef STACKTP_H_
#define STACKTP_H_
template <class Type>
class Stack
{
private:
enum { SIZE = 10 }; // constant specific to class
int stacksize;
Type * items; // holds stack items
int top; // index for top stack item
public:
explicit Stack(int ss = SIZE);
Stack(const Stack& st);//复制构造函数
Stack& operator=(const Stack& st);//赋值运算符
bool isempty();
bool isfull();
bool push(const Type& item); // add item to stack
bool pop(Type& item); // pop top into item
~Stack() { delete[] items; }//析构函数
};
//默认构造函数
template <class Type>
Stack<Type>::Stack(int ss) : stacksize(ss), top(0)
{
items = new Type[stacksize];
}
template <class Type>
Stack<Type>::Stack(const Stack& st)
{
stacksize = st.stacksize;
top = st.top;
items = new Type[stacksize];
for (int i = 0; i < top; i++)
items[i] = st.items[i];
}
template <class Type>
Stack<Type>& Stack<Type>::operator=(const Stack<Type>& st)
{
if (this == &st)
return *this;
delete[] items;
stacksize = st.stacksize;
top = st.top;
items = new Type[stacksize];
for (int i = 0; i < top; i++)
items[i] = st.items[i];
return *this;
}
template <class Type>
bool Stack<Type>::isempty()
{
return top == 0;
}
template <class Type>
bool Stack<Type>::isfull()
{
return top == stacksize;
}
template <class Type>
bool Stack<Type>::push(const Type& item)
{
if (top < stacksize)
{
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;
}
#endif
main.h中的 simple_template()函数
#pragma once
#ifndef MAIN_H_
#define MAIN_H_
#include //输入输出
#include //simple_template
#include //simple_template
#include "stacktp.h" //simple_template
using namespace std;
void simple_template(void)
{
cout << "simple_template Hello*****************************************************\n";
Stack<std::string> st = Stack<std::string>(); // 创建一个栈
char ch;
std::string po;
cout << "Please enter A to add a purchase order,\n"
<< "P to process a PO, or Q to quit.\n";
while (cin >> ch && std::toupper(ch) != 'Q')
{
while (cin.get() != '\n')
continue;
if (!std::isalpha(ch))
{
cout << '\a';
continue;
}
switch (ch)
{
case 'A':
case 'a': cout << "Enter a PO number to add: ";
cin >> po;
if (st.isfull())
cout << "stack already full\n";
else
st.push(po);
break;
case 'P':
case 'p': if (st.isempty())
cout << "stack already empty\n";
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 << "simple_template Bye*****************************************************\n";
}
#endif
main.cpp
/*
Project name : _12template
Last modified Date: 2022年5月6日11点33分
Last Version: V1.0
Descriptions: 模板的简单测试
*/
#include "main.h"
int main()
{
cout << "模板的简单测试***************************************************************" << endl;
simple_template();
return 0;
}
模板的简单测试***************************************************************
simple_template Hello*****************************************************
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
A
Enter a PO number to add: 1
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
A
Enter a PO number to add: 2
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
P0
PO #2 popped
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
P0
PO #1 popped
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
Q
simple_template Bye*****************************************************
D:\Prj\_C++Self\_12template\Debug\_12template.exe (进程 3560)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .