• 第12章 类和动态内存分配


    12.1 动态内存和类

    12.2改进后的新String类

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

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

    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类成员,必须使用成员初始化列表语法。对于被声明为引用的类成员也是如此。

    1. class Agency {...}
    2. class Agent
    3. {
    4. private:
    5. Agency &belong;
    6. const int cnt;
    7. }
    8. Agent::Agent(Agency &a,int num):belong(a),cnt(num)
    9. {
    10. ...
    11. }

    示例代码

    queue.h

    1. #ifndef QUEUE_H_
    2. #define QUEUE_H_
    3. class Customer
    4. {
    5. private:
    6. long arrive;
    7. int processtime;
    8. public:
    9. Customer() {arrive = processtime = 0;};
    10. void set(long when);
    11. long when() const {return arrive;};
    12. int ptime() const {return processtime;};
    13. };
    14. typedef Customer Item;
    15. class Queue
    16. {
    17. enum {Q_SIZE = 10};
    18. private:
    19. struct Node {Item item;struct Node* next;};
    20. Node* front;
    21. Node* rear;
    22. int items;
    23. const int qsize;
    24. Queue(const Queue &q):qsize(0) {};
    25. Queue &operator=(const Queue &q) {return *this;};
    26. public:
    27. Queue(int qs = Q_SIZE);
    28. ~Queue();
    29. bool isempty() const;
    30. bool isfull() const;
    31. int queuecount() const;
    32. bool enqueue(const Item &item);
    33. bool dequeue(Item &item);
    34. };
    35. #endif

    queue.cpp

    1. #include "queue.h"
    2. #include <cstdlib>
    3. #include <iostream>
    4. Queue::Queue(int qs):qsize(qs)
    5. {
    6. front = rear = NULL;
    7. items = 0;
    8. }
    9. Queue::~Queue()
    10. {
    11. Node* temp;
    12. while(front != NULL)
    13. {
    14. temp = front;
    15. front = front->next;
    16. delete temp;
    17. }
    18. }
    19. bool Queue::isempty() const
    20. {
    21. return items == 0;
    22. }
    23. bool Queue::isfull() const
    24. {
    25. return items == qsize;
    26. }
    27. int Queue::queuecount() const
    28. {
    29. return items;
    30. }
    31. bool Queue::enqueue(const Item &item)
    32. {
    33. //std::cout << "enqueue B" <<std::endl;
    34. if(isfull())
    35. {
    36. return false;
    37. }
    38. Node* add = new Node;
    39. add->item = item;
    40. add->next = NULL;
    41. items++;
    42. if(front == NULL)
    43. front =add;
    44. else
    45. {
    46. rear->next = add;
    47. }
    48. rear = add;
    49. //std::cout << "front:" << front <<"rear :" << rear <<std::endl;
    50. return true;
    51. }
    52. bool Queue::dequeue(Item &item)
    53. {
    54. //std::cout << "dequeue B" <<std::endl;
    55. if(isempty())
    56. {
    57. return false;
    58. }
    59. //std::cout << "items :" << items <<std::endl;
    60. item = front->item;
    61. items--;
    62. Node* temp = front;
    63. front = front->next;
    64. delete temp;
    65. if(items == 0)
    66. rear = NULL;
    67. //std::cout << "front:" << front <<"rear :" << rear <<std::endl;
    68. //std::cout << "dequeue A"<<std::endl;
    69. return true;
    70. }
    71. void Customer::set(long when)
    72. {
    73. processtime = std::rand() %3 +1;
    74. arrive = when;
    75. }

    bank.cpp

    1. #include <iostream>
    2. #include <cstdlib>
    3. #include <ctime>
    4. #include "queue.h"
    5. using namespace std;
    6. const int MIN_PER_HR = 60;
    7. bool newcustomer(double x);
    8. int main()
    9. {
    10. srand(time(0));
    11. cout << "enter maximun size of queue:";
    12. int qs;
    13. cin >> qs;
    14. Queue line(qs);
    15. cout << "Enter the number of simulation hours:";
    16. int hours;
    17. cin >> hours;
    18. long cyclelimit = MIN_PER_HR * hours;
    19. cout << "enter the average number of customers per hour:";
    20. double perhour;
    21. cin >> perhour;
    22. double min_per_cust;
    23. min_per_cust = MIN_PER_HR/perhour;
    24. Item temp;
    25. long turnaways = 0;
    26. long customers = 0;
    27. long served = 0,sum_line = 0,wait_time = 0,line_wait = 0;
    28. for(int cycle = 0;cycle < cyclelimit;cycle++)
    29. {
    30. if(newcustomer(min_per_cust))
    31. turnaways++;
    32. else
    33. {
    34. customers++;
    35. temp.set(cycle);
    36. line.enqueue(temp);
    37. }
    38. if(wait_time <= 0 && !line.isempty())
    39. {
    40. line.dequeue(temp);
    41. wait_time = temp.ptime();
    42. line_wait += cycle - temp.when();
    43. served++;
    44. }
    45. if(wait_time > 0)
    46. wait_time--;
    47. sum_line += line.queuecount();
    48. }
    49. if(customers > 0)
    50. {
    51. cout << "customers accepted: " << customers << endl;
    52. cout << " customers served: " << served << endl;
    53. cout << " turnaways: " << turnaways << endl;
    54. cout << "average queue size: ";
    55. cout.precision(2);
    56. cout.setf(ios_base::fixed,ios_base::floatfield);
    57. cout << (double)sum_line/cyclelimit << endl;
    58. cout << " average wait time:" << (double)line_wait/served << " minutes\n";
    59. }
    60. else
    61. cout <<"No customers!\n";
    62. cout << "Done\n";
    63. return 0;
    64. }
    65. bool newcustomer(double x)
    66. {
    67. return (rand()*x/RAND_MAX < 1);
    68. }

  • 相关阅读:
    中级深入--day15
    如何让照片动起来?几个制作方法和注意事项分享
    【Java 进阶篇】Java Servlet 执行原理详解
    STM32F1与STM32CubeIDE综合实例-MPU6050数据3D可视化(基于Python)
    ROS2支持技术:DDS简述
    微信每日早安推送 Windows版
    你在前端有做过哪些性能优化
    openpyxl修改excel列数据
    APP逆向案例之(二)对加固APP进行分析和破解
    Golang学习记录:基础知识篇(一)
  • 原文地址:https://blog.csdn.net/m0_59666413/article/details/125464405