• C++模板嵌套类


    12.14 C++模板嵌套类

    模板类中中嵌套类:模板类为QueueTP,嵌套类为Node

    代码:

    queuetp.h

    #pragma once
    // 这是队列---先入先出的思想
    #ifndef QUEUETP_H_
    #define QUEUETP_H_
    template 
    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 
    QueueTP::QueueTP(int qs) : qsize(qs)
    {
        front = rear = 0;
        items = 0;
    }
    template 
    QueueTP::~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 
    bool QueueTP::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 
    bool QueueTP::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
    #include"queuetp.h"
    #include"queue.h"
    #include
    ​
    int main()
    {
        using std::string;
        using std::cin;
        using std::cout;
        /*
        模板类中的嵌套类
        相关的文件:queuetp.h
        */
        QueueTP 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。
    要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
    按任意键关闭此窗口. . .
  • 相关阅读:
    tcp,udp和计算机协议分层
    Matlab:对函数调用结果进行索引
    在Excel中使用SQL
    springcloud微服务(一)(了解微服务、eureka注册中心、Ribbon负载均衡)
    【计算机组成 课程笔记】7.3 高速缓存 Cache
    低度酒赛道进入洗牌期,新品牌如何破局三大难题?
    智能技术上的“是”并不代表具体领域的“应该”
    Vue - 实现点击按钮(笔图标)可编辑 input 输入框(点击文字内容后变成 <input> 输入框同时能修改和取消、删除)
    Sprites and textures
    js异步任务的简单总结
  • 原文地址:https://blog.csdn.net/weixin_44410704/article/details/127994230