C++模板嵌套类

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jasmine-Lily

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

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

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

打赏作者

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

抵扣说明:

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

余额充值