模板提供了泛型,就是将数据类型作为一个参数传递给用于构建类或函数
格式:
template // newer choice
当一个模板被触发后,Type将会被一个具体的数据类型or类代替(这个过程成为模板类的实现)。
成员函数与模板声明:在定义模板类时,每个成员函数之前都必须有同样的模板声明:
template
内联函数不需要,因为内联函数定义在类声明中。
stacktp.h
#pragma once
// stacktp.h -- a stack template
#ifndef STACKTP_H_
#define STACKTP_H_
template
class Stack
{
private:enum { SIZE = 10 }; // constant specific to classint stacksize;Type * items; // holds stack itemsint 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 stackbool pop(Type& item); // pop top into item~Stack() { delete[] items; }//析构函数};
//默认构造函数
template
Stack::Stack(int ss) : stacksize(ss), top(0)
{items = new Type[stacksize];
}
template
Stack::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
Stack& Stack::operator=(const Stack& 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
bool Stack::isempty()
{return top == 0;
}
template
bool Stack::isfull()
{return top == stacksize;
}
template
bool Stack::push(const Type& item)
{if (top < stacksize){items[top++] = item;return true;}elsereturn false;
}
template
bool Stack::pop(Type& item)
{if (top > 0){item = items[--top];return true;}elsereturn 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 st = Stack(); // 创建一个栈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";elsest.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。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .