C++ Primer Plus学习:第十二章 类和动态内存(3)
2011-09-19 21:44
405 查看
成员 初始化列表的句法
初始化成员列表
mem1:n
mem2:0
mem3:n*m+2
注意
此格式仅可用于构造函数
必须用此格式各初始化非静态const数据成员
必须用这种格式来初始化引用数据成员
eg
queue.h
queue.cpp
bank.cpp
Class::Classy(int n,int m):mem1(n),mem2(0),mem3(n*m+2) { //..... }
初始化成员列表
mem1:n
mem2:0
mem3:n*m+2
注意
此格式仅可用于构造函数
必须用此格式各初始化非静态const数据成员
必须用这种格式来初始化引用数据成员
eg
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 { private: struct Node{Item item;struct Node *next;}; enum{Q_SIZE = 10}; 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 <cstdlib> #include "queue.h" 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; } //Add item to queue bool Queue::enqueue(const Item & item) { if(isfull()) return false; Node * add = new Node; if(add == NULL) return false; add->item = item; add->next = NULL; items++; if(front == NULL) front = add; else rear->next = add; return true; } //Place front item into item variable and remove from queue bool Queue::dequeue(Item & item) { if(front == NULL) return false; item = front->item; items--; Node * temp = front; front = front->next; delete temp; if(items == 0) rear = NULL; 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" const int MIN_PER_HR = 60; bool newcustomer(double x); int main() { using std::cin; using std::cout; using std::endl; using std::ios_base; std::srand(std::time(0)); cout<<"Case Study:Bank of Heather Automatic Teller\n"; cout<<"Enter maximum 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; Item temp; long trunaways = 0; long customers = 0; long served = 0; long sum_line = 0; int wait_time = 0; long line_wait = 0; for(int cycle = 0;cycle<cyclelimit;cycle++) { if(newcustomer(min_per_cust)) { if(line.isfull()) trunaways++; 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<<" trunaways: "<<trunaways<<endl; cout<<"average queue size: "; cout.precision(2); cout.setf(ios_base::fixed,ios_base::floatfield); cout.setf(ios_base::showpoint); cout<<(double)sum_line/cyclelimit<<endl; cout<<" average wait time: "<<(double)line_wait/served<<" minutes\n"; } else cout<<"No customers!\n"; cout<<"Done!\n"; system("pause"); return 0; } bool newcustomer(double x) { return (std::rand()*x/RAND_MAX<1); }
相关文章推荐
- C++ Primer Plus学习:第十二章 类和动态内存(2)
- 2012/1/27 《C++ Primer Plus》第十二章:类和动态内存分配 学习笔记
- C++ Primer Plus学习:第十二章 类和动态内存(1)
- 2012/1/27 《C++ Primer Plus》第十二章:类和动态内存分配 学习笔记
- 《C++ Primer Plus(第六版)》(24)(第十二章 类和动态内存分配 编程题和答案)
- c++ primer plus第十二章-动态内存和类、复制构造函数
- 《C++ Primer Plus(第六版)》(23)(第十二章 类和动态内存分配 复习题和答案)
- 【学习C++】C++ Primer Plus (第六版)第十二章编程练习1-6
- 《C++ Primer Plus》第12章 类和动态内存分配 学习笔记
- 《C++ Primer Plus(第六版)》(22)(第十二章 类和动态内存分配 笔记)
- 【C++】学习笔记十四——C++ primer plus第六版第4章编程练习
- C++ Primer Plus学习:第四章 复合类型(5)
- 学习C++.Primer.Plus 4 复合类型
- C++ Primer Plus学习:第七章
- C++ Primer Plus(第六版)--学习杂记(第六章)
- 【学习笔记】C++primer plus 8.函数探幽
- 【今日学习】C++ Primer Plus 第六版——RTTI、代码重用(上)
- c++primer plus第十二章-再谈new定位运算符和返回对象const的引用
- [C++ Primer Plus]学习笔记--关于C++ string和c类型字符数组的对比
- C++ primer plus学习记录