c++类模板的简单举例
创始人
2024-02-08 06:44:47
0

12.2 c++类模板的简单举例

12.2.1 模板类简介

12.2.1.1 泛型定义及格式

模板提供了泛型,就是将数据类型作为一个参数传递给用于构建类或函数
格式:

template  // newer choice

12.2.1.2 模板是如何工作的?

当一个模板被触发后,Type将会被一个具体的数据类型or类代替(这个过程成为模板类的实现)。
成员函数与模板声明:在定义模板类时,每个成员函数之前都必须有同样的模板声明:

template 

内联函数不需要,因为内联函数定义在类声明中。

12.2.1.3 注意事项

  • 因为模板不是函数,所以它们不能单独编译。所以模板成员函数要与声明在一个文件,实现这种目标可以使用内联函数或者将模板成员函数与声明定义在同一个头文件。
  • 注意到在类声明或模板函数里面是可以直接使用 Stack 的,但是在外面必须使用 Stack的格式

12.2.2 代码

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;
}

12.2.3 运行结果

模板的简单测试***************************************************************
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。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...