12.14 C++模板嵌套类
在模板类中中嵌套类:模板类为QueueTP,嵌套类为Node
代码:
queuetp.h
#pragma once // 这是队列---先入先出的思想 #ifndef QUEUETP_H_ #define QUEUETP_H_ template <class Item> class QueueTP { private: enum { Q_SIZE = 10 }; // Node is a nested class definition class Node { public: Item item; Node* next; Node(const Item& i) :item(i), next(0) { } }; Node* front; // pointer to front of Queue Node* rear; // pointer to rear of Queue int items; // current number of items in Queue const int qsize; // maximum number of items in Queue QueueTP(const QueueTP& q) : qsize(0) {} //复制构造函数 QueueTP& operator=(const QueueTP& q) { return *this; } //赋值运算符重载 public: QueueTP(int qs = Q_SIZE);//默认构造函数 ~QueueTP();//析构函数 bool isempty() const { return items == 0; } bool isfull() const { return items == qsize; } int queuecount() const { return items; } bool enqueue(const Item& item); // add item to end bool dequeue(Item& item); // remove item from front }; // QueueTP methods template <class Item> QueueTP<Item>::QueueTP(int qs) : qsize(qs) { front = rear = 0; items = 0; } template <class Item> QueueTP<Item>::~QueueTP() { Node* temp; while (front != 0) // while queue is not yet empty { temp = front; // save address of front item front = front->next;// reset pointer to next item delete temp; // delete former front } } // Add item to queue template <class Item> bool QueueTP<Item>::enqueue(const Item& item) { if (isfull()) return false; Node* add = new Node(item); // create node // on failure, new throws std::bad_alloc exception items++; if (front == 0) // if queue is empty, front = add; // place item at front else rear->next = add; // else place at rear rear = add; // have rear point to new node return true; } // Place front item into item variable and remove from queue template <class Item> bool QueueTP<Item>::dequeue(Item& item) { if (front == 0) return false; item = front->item; // set item to first item in queue items--; Node* temp = front; // save location of first item front = front->next; // reset front to next item delete temp; // delete former first item if (items == 0) rear = 0; return true; } #endif
main.cpp
/* Project name : _16Nested_class Last modified Date: 2022年3月28日09点39分 Last Version: V1.0 Descriptions: 嵌套类 */ #include<iostream> #include"queuetp.h" #include"queue.h" #include<string> int main() { using std::string; using std::cin; using std::cout; /* 模板类中的嵌套类 相关的文件:queuetp.h */ QueueTP<string> cs(2); while (!cs.isfull()) { cout << "Please enter your name. You will be " "served in the order of arrival.\n" "name: "; getline(cin, temp); cs.enqueue(temp); } cout << "The queue is full. Processing begins!\n"; while (!cs.isempty()) { cs.dequeue(temp); cout << "Now processing " << temp << "...\n"; } return 0; }
运行结果:
Please enter your name. You will be served in the order of arrival. name: Jasmine Please enter your name. You will be served in the order of arrival. name: lili The queue is full. Processing begins! Now processing Jasmine... Now processing lili... D:\Prj\_C++Self\_16Nested_class\Debug\_16Nested_class.exe (进程 15116)已退出,代码为 0。 要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。 按任意键关闭此窗口. . .