c++类模板的简单举例

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

12.2.1 模板类简介

12.2.1.1 泛型定义及格式

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

template <typename Type> // newer choice
12.2.1.2 模板是如何工作的?

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

template <typename Type>

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

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 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 <iostream> //输入输出 
#include <string> //simple_template
#include <cctype> //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;
}

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。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jasmine-Lily

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值