第12章 类和动态内存分配

12.1 动态内存和类

12.2改进后的新String类

12.3在构造函数中使用new时应注意的事项

  • 构造函数中使用new来初始化指针成员,则应在析构函数中使用delete。
  • new对应delete,new[] 对应delete[]
  • 如果有多个构造函数,则必须以相同方式使用new。只有一个析构函数,所有构造函数都必须与他兼容。
  • 应定义一个赋值构造函数,通过深度复制将一个对象初始化为另一个对象。
String::String(const String &st)
{
    num_string++;
    len = st.len;
    str = new char[len + 1];
    std::strcpy(str,st.str);
}
  • 应当定义一个复制运算符。通过深度复制将一个对象复制给另一个对象。
String &String::operation=(const String &st)
{
    if(this == &st)
        return *this;
    delete[] str;
    len = st.len;
    str = new char[len +1];
    std::strcpy(str,st.str);
    return *this;
}

12.4有关返回对象的说明

  • 返回对象的引用
  • 指向对象的const引用   const Vector &Max(const Vector &v1,const Vector &v2)
  • const对象

1、返回指向const对象的引用

const Vector &Max(const Vector &v1,const Vector &v2)

2、返回指向非const对象的引用

operator重载函数

std::ostream &operator<< (std::ostream &os,const Vector &v)

3、返回对象

Vector Vector::operator+(const Vector &b) const

4、返回const对象

12.5使用指向对象的指针

12.6复习各种技术

  • 重载<<运算符
  • 转换函数
  • 构造函数使用new的类

12.7队列模拟

  • 栈:在统一端进行添加和删除,后进先出(LIFO)
  • 队列:先进先出(FIFO)

队列类特征

  • 储存有序的项目序列
  • 容纳项目有数据限制
  • 能够创建空队列
  • 可检查队列为空或者已满
  • 队尾添加、对首删除
  • 能确定队列中数目

对于const类成员,必须使用成员初始化列表语法。对于被声明为引用的类成员也是如此。

class Agency {...}

class Agent
{
    private:
        Agency &belong;
        const int cnt;
}

Agent::Agent(Agency &a,int num):belong(a),cnt(num)
{
    ...
}

示例代码

queue.h

#ifndef QUEUE_H_
#define QUEUE_H_

class Customer
{
private:
    long arrive;
    int processtime;
public:
    Customer() {arrive = processtime = 0;};
    void set(long when);
    long when() const {return arrive;};
    int ptime() const {return processtime;};
};

typedef Customer Item;

class Queue
{
    enum {Q_SIZE = 10};
private:
    struct Node {Item item;struct Node* next;};
    Node* front;
    Node* rear;
    int items;
    const int qsize;

    Queue(const Queue &q):qsize(0) {};
    Queue &operator=(const Queue &q) {return *this;};
public:
    Queue(int qs = Q_SIZE);
    ~Queue();
    bool isempty() const;
    bool isfull() const;
    int queuecount() const;
    bool enqueue(const Item &item);
    bool dequeue(Item &item);

};

#endif

queue.cpp

#include "queue.h"
#include <cstdlib>
#include <iostream>

Queue::Queue(int qs):qsize(qs)
{
    front = rear = NULL;
    items = 0;
}

Queue::~Queue()
{
    Node* temp;
    while(front != NULL)
    {
        temp = front;
        front = front->next;
        delete temp;
    }
}

bool Queue::isempty() const
{
    return items == 0;
}

bool Queue::isfull() const
{
    return items == qsize;
}

int Queue::queuecount() const
{
    return items;
}

bool Queue::enqueue(const Item &item)
{
    //std::cout << "enqueue B" <<std::endl;
    if(isfull())
    {
        return false;
    }
    Node* add = new Node;
    add->item = item;
    add->next = NULL;
    
    items++;
    if(front == NULL)
        front =add;
    else
    {
        rear->next = add;
    }
        
    rear = add;
    //std::cout << "front:" << front <<"rear :" << rear <<std::endl;
    return true;
}

bool Queue::dequeue(Item &item)
{
    //std::cout << "dequeue B" <<std::endl;
    
    if(isempty())
    {
        return false;
    }
    //std::cout << "items :" << items <<std::endl;
    
    item = front->item;
    items--;
    Node* temp = front;
    front = front->next;
    delete temp;
    if(items == 0)
        rear = NULL;
    //std::cout << "front:" << front <<"rear :" << rear <<std::endl;
    //std::cout << "dequeue A"<<std::endl;
    return true;
}

void Customer::set(long when)
{
    processtime = std::rand() %3 +1;
    arrive = when;
}

bank.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "queue.h"

using namespace std;
const int MIN_PER_HR = 60;

bool newcustomer(double x);

int main()
{
    srand(time(0));
    cout << "enter maximun size of queue:";
    int qs;
    cin >> qs;
    Queue line(qs);

    cout << "Enter the number of simulation hours:";
    int hours;
    cin >> hours;
    long cyclelimit = MIN_PER_HR * hours;

    cout << "enter the average number of customers per hour:";
    double perhour;
    cin >> perhour;
    double min_per_cust;
    min_per_cust = MIN_PER_HR/perhour;

    Item temp;
    long turnaways = 0;
    long customers = 0;
    long served = 0,sum_line = 0,wait_time = 0,line_wait = 0; 
    for(int cycle = 0;cycle < cyclelimit;cycle++)
    {
        if(newcustomer(min_per_cust))
            turnaways++;
        else
        {
            customers++;
            temp.set(cycle);
            line.enqueue(temp);
        }
        if(wait_time <= 0 && !line.isempty())
        {
            line.dequeue(temp);
            wait_time = temp.ptime();
            line_wait += cycle - temp.when();
            served++;
        }
        if(wait_time > 0)
            wait_time--;
        sum_line += line.queuecount();
    }
    if(customers > 0)
    {
        cout << "customers accepted: " << customers << endl;
        cout << " customers served: " << served << endl;
        cout << "  turnaways: " << turnaways << endl;
        cout << "average queue size: ";
        cout.precision(2);
        cout.setf(ios_base::fixed,ios_base::floatfield);
        cout << (double)sum_line/cyclelimit << endl;
        cout << " average wait time:" << (double)line_wait/served << " minutes\n";
    }
    else
        cout <<"No customers!\n";
    cout << "Done\n";

    return 0;
    
}

bool newcustomer(double x)
{
    return (rand()*x/RAND_MAX < 1);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值