【C++ Primer Plus】编程练习答案——第14章

1 // chapter14_1_wine.h23 #ifndef LEARN_CPP_CHAPTER14_1_WINE_H4 #define LEARN_CPP_CHAPTER14_1_WINE_H56 #include <valarray>7 #include <string>89 typedef std::valarray<int> ArrayInt; 10 typedef std::pair<ArrayInt, ArrayInt> PairArray; 1112 class Wine { 13 private: 14std::string label; 15int numofyears; 16PairArray year_bots; 17 public: 18Wine(); 19Wine(const char * l, int y, const int ye[], const int bot[]); 20Wine(const char * l, int y); 21~Wine(); 22void GetBottles(); 23std::string & Label(); 24int sum() const; 25void show() const; 26 }; 2728 #endif //LEARN_CPP_CHAPTER14_1_WINE_H 293031 // chapter14_1_wine.cpp 3233 #include "chapter14_1_wine.h" 34 #include <iostream> 3536 Wine::Wine() { 37label = "none"; 38numofyears = 0; 39year_bots.first = ArrayInt(numofyears); 40year_bots.second = ArrayInt(numofyears); 41 } 4243 Wine::Wine(const char *l, int y, const int *ye, const int *bot) { 44label = l; 45numofyears = y; 46year_bots.first = ArrayInt(numofyears); 47year_bots.second = ArrayInt(numofyears); 48for (int i = 0; i < numofyears; ++ i) { 49year_bots.first[i] = ye[i]; 50year_bots.second[i] = bot[i]; 51} 52 } 5354 Wine::Wine(const char *l, int y) { 55label = l; 56numofyears = y; 57year_bots.first = ArrayInt(numofyears); 58year_bots.second = ArrayInt(numofyears); 59 } 6061 Wine::~Wine() { 6263 } 6465 void Wine::GetBottles() { 66using namespace std; 67for (int i = 0; i < numofyears; ++ i) { 68cout << "#" << i << " year: "; 69cin >> year_bots.first[i]; 70cout << "#" << i << " bottles: "; 71cin >> year_bots.second[i]; 72} 73cout << "finished" << endl; 74 } 7576 std::string &Wine::Label() { 77return label; 78 } 7980 int Wine::sum() const { 81return year_bots.second.sum(); 82 } 8384 void Wine::show() const { 85using namespace std; 86cout << "year\tbottles" << endl; 87for (int i = 0; i < numofyears; ++ i) 88cout << year_bots.first[i] << "\t" << year_bots.second[i] << endl; 89 } 909192 // run 9394 void ch14_1() { 95using namespace std; 96cout << "enter name of wine: "; 97char lab[50]; 98cin.getline(lab, 50); 99cout << "enter number of years: ";100int yrs;101cin >> yrs;102 103Wine holding(lab, yrs);104holding.GetBottles();105holding.show();106 107const int YRS = 3;108int y[YRS] = {1993, 1995, 1998};109int b[YRS] = {48, 60, 72};110Wine more("Gushing Grape Red", YRS, y, b);111more.show();112cout << "total bottles for " << more.Label() << ": " << more.sum() << endl;113cout << "bye" << endl;114return;115 }1 // chatper14_2_wine2.h23 #ifndef LEARN_CPP_CHAPTER14_2_WINE2_H4 #define LEARN_CPP_CHAPTER14_2_WINE2_H56 #include <valarray>7 #include <string>89 typedef std::valarray<int> ArrayInt; 10 typedef std::pair<ArrayInt, ArrayInt> PairArray; 1112 class Wine2 : private std::string, private PairArray { 13 private: 14int numofyears; 15 public: 16Wine2(); 17Wine2(const char * l, int y, const int ye[], const int bot[]); 18Wine2(const char * l, int y); 19~Wine2(); 20void GetBottles(); 21std::string & Label(); 22int sum() const; 23void show() const; 24 }; 2526 #endif //LEARN_CPP_CHAPTER14_2_WINE2_H 272829 // chapter14_2_wine2.cpp 3031 #include "chapter14_2_wine2.h" 32 #include <iostream> 3334 Wine2::Wine2() : std::string("none"), PairArray(ArrayInt(0),ArrayInt(0)){ 35numofyears = 0; 36 } 3738 Wine2::Wine2(const char *l, int y, const int *ye, const int *bot) 39: std::string(l), PairArray(ArrayInt(y),ArrayInt(y)){ 40numofyears = y; 41for (int i = 0; i < numofyears; ++ i) { 42PairArray::first[i] = ye[i]; 43PairArray::second[i] = bot[i]; 44} 45 } 4647 Wine2::Wine2(const char *l, int y) 48: std::string(l), PairArray(ArrayInt(y),ArrayInt(y)){ 49numofyears = y; 50 } 5152 Wine2::~Wine2() { 5354 } 5556 void Wine2::GetBottles() { 57using namespace std; 58for (int i = 0; i < numofyears; ++ i) { 59cout << "#" << i << " year: "; 60cin >> PairArray::first[i]; 61cout << "#" << i << " bottles: "; 62cin >> PairArray::second[i]; 63} 64cout << "finished" << endl; 65 } 6667 std::string &Wine2::Label() { 68return (std::string &)*this; 69 } 7071 int Wine2::sum() const { 72return PairArray::second.sum(); 73 } 7475 void Wine2::show() const { 76using namespace std; 77cout << "year\tbottles" << endl; 78for (int i = 0; i < numofyears; ++ i) 79cout << PairArray::first[i] << "\t" << PairArray::second[i] << endl; 80 } 818283 // run 8485 void ch14_2() { 86using namespace std; 87cout << "enter name of wine: "; 88char lab[50]; 89cin.getline(lab, 50); 90cout << "enter number of years: "; 91int yrs; 92cin >> yrs; 9394Wine2 holding(lab, yrs); 95holding.GetBottles(); 96holding.show(); 9798const int YRS = 3; 99int y[YRS] = {1993, 1995, 1998};100int b[YRS] = {48, 60, 72};101Wine2 more("Gushing Grape Red", YRS, y, b);102more.show();103cout << "total bottles for " << more.Label() << ": " << more.sum() << endl;104cout << "bye" << endl;105return;106 }1 //chatper14_3_queuetp.h23 #ifndef LEARN_CPP_CHAPTER14_3_QUEUETP_H4 #define LEARN_CPP_CHAPTER14_3_QUEUETP_H56 #include <string>78 class Worker {9 private: 10std::string fullname; 11long id; 12 public: 13Worker(); 14Worker(const std::string & s, long n); 15~Worker(); 16Worker & operator=(const Worker & w); 17void show() const; 18 }; 1920 template <typename Type> 21 class QueueTp { 22 private: 23enum {MAX = 100}; 24Type items[MAX]; 25int front; 26int rear; 27 public: 28QueueTp(); 29~QueueTp(); 30bool isEmpty(); 31bool isFull(); 32bool enQueue(const Type &t); 33bool deQueue(Type &t); 34int num() const {return (rear - front + MAX) % MAX;} 35 }; 3637 template<typename Type> 38 QueueTp<Type>::QueueTp() { 39front = rear = 0; 40 } 4142 template<typename Type> 43 QueueTp<Type>::~QueueTp() { 4445 } 4647 template<typename Type> 48 bool QueueTp<Type>::isEmpty() { 49if (front == rear && front == 0) 50return true; 51return false; 52 } 5354 template<typename Type> 55 bool QueueTp<Type>::isFull() { 56if ((rear + 1) % MAX == front) 57return true; 58return false; 59 } 6061 template<typename Type> 62 bool QueueTp<Type>::enQueue(const Type &t) { 63if (isFull()) 64return false; 65items[rear] = t; 66rear = (rear + 1) % MAX; 67return true; 68 } 6970 template<typename Type> 71 bool QueueTp<Type>::deQueue(Type &t) { 72if (isEmpty()) 73return false; 74t = items[front]; 75front = (front + 1) % MAX; 76return true; 77 } 7879 #endif //LEARN_CPP_CHAPTER14_3_QUEUETP_H 80818283 // chapter14_3_queuetp.cpp 8485 #include "chapter14_3_queuetp.h" 86 #include <iostream> 8788 Worker::Worker() 89: fullname("none"){ 90id = 0; 91 } 9293 Worker::Worker(const std::string &s, long n) 94: fullname(s){ 95id = n; 96 } 9798 Worker::~Worker() { 99 100 }101 102 Worker & Worker::operator=(const Worker & w) {103fullname = w.fullname;104id = w.id;105return *this;106 }107 108 void Worker::show() const {109using namespace std;110cout << "name: " << fullname << endl;111cout << "id: " << id << endl;112 }113 114 115 // run116 117 void ch14_3() {118using namespace std;119Worker * lolas[5];120Worker * t;121for (int i = 0; i < 5; ++ i)122lolas[i] = new Worker("worker" + i, i);123QueueTp<Worker *> q;124q.enQueue(lolas[0]);125q.enQueue(lolas[1]);126q.enQueue(lolas[2]);127cout << q.num() << "items in q" << endl;128q.enQueue(lolas[3]);129q.enQueue(lolas[4]);130cout << q.num() << "items in q" << endl;131q.deQueue(t);132cout << q.num() << "items in q" << endl;133 }1 // chapter14_4_person.h23 #ifndef LEARN_CPP_CHAPTER14_4_PERSON_H4 #define LEARN_CPP_CHAPTER14_4_PERSON_H56 #include <iostream>78 class Personn {9 private: 10std::string firstname; 11std::string lastname; 12 public: 13Personn(); 14Personn(std::string f, std::string l); 15~Personn(); 16virtual void show() const; 17 }; 1819 class Gunslmger : virtual public Personn { 20 private: 21int num; 22 public: 23Gunslmger(); 24Gunslmger(std::string f, std::string l, int n); 25~Gunslmger(); 26double draw() const; 27virtual void show() const; 28 }; 2930 class PokerPlayer : virtual public Personn { 31 private: 32int poke; 33 public: 34PokerPlayer(); 35PokerPlayer(std::string f, std::string l, int p); 36~PokerPlayer(); 37int draw() const; 38virtual void show() const; 39 }; 4041 class BadDude : public PokerPlayer, public Gunslmger { 42 public: 43BadDude(); 44BadDude(std::string f, std::string l, int n, int p); 45~BadDude(); 46double Gdraw() const; 47int Cdraw() const; 48virtual void show() const; 49 }; 50515253 #endif //LEARN_CPP_CHAPTER14_4_PERSON_H 5455 // chapter14_4_person.cpp 5657 #include "chapter14_4_person.h" 58 #include <iostream> 5960 Personn::Personn() 61: firstname("none"), lastname("none"){ 6263 } 6465 Personn::Personn(std::string f, std::string l) 66: firstname(f), lastname(l){ 6768 } 6970 Personn::~Personn() { 7172 } 7374 void Personn::show() const { 75using namespace std; 76cout << firstname << " " << lastname << endl; 77 } 7879 Gunslmger::Gunslmger() 80: Personn("none", "none"){ 81num = 0; 82 } 8384 Gunslmger::Gunslmger(std::string f, std::string l, int n) 85: Personn(f, l){ 86num = n; 87 } 8889 Gunslmger::~Gunslmger() { 9091 } 9293 double Gunslmger::draw() const { 94return num; 95 } 9697 void Gunslmger::show() const { 98using namespace std; 99Personn::show();100cout << "num: " << num << endl;101 }102 103 PokerPlayer::PokerPlayer()104: Personn("none", "none"){105poke = 0;106 }107 108 PokerPlayer::PokerPlayer(std::string f, std::string l, int p)109: Personn(f, l){110poke = p;111 }112 113 PokerPlayer::~PokerPlayer() {114 115 }116 117 int PokerPlayer::draw() const {118return poke;119 }120 121 void PokerPlayer::show() const {122using namespace std;123Personn::show();124cout << "poke: " << poke << endl;125 }126 127 BadDude::BadDude()128: Personn("none", "none"), Gunslmger("none", "none", 0), PokerPlayer("none", "none", 1){129 130 }131 132 BadDude::BadDude(std::string f, std::string l, int n, int p)133: Personn(f, l), Gunslmger(f, l, n), PokerPlayer(f, l, p){134 135 }136 137 BadDude::~BadDude() {138 139 }140 141 double BadDude::Gdraw() const {142return Gunslmger::draw();143 }144 145 int BadDude::Cdraw() const {146return PokerPlayer::draw();147 }148 149 void BadDude::show() const {150Gunslmger::show();151PokerPlayer::show();152 }153 154 // run155 156 157 void ch14_4() {158using namespace std;159Gunslmger g("f1","l1",1);160PokerPlayer p("f2", "l2", 52);161BadDude b("f3", "l3", 2, 51);162cout << g.draw() << endl;163g.show();164cout << p.draw() << endl;165p.show();166b.show();167b.Gdraw();168b.Cdraw();169 }1 // chapter14_5_abstremp.h234 #ifndef LEARN_CPP_CHAPTER14_5_ABSTREMP_H5 #define LEARN_CPP_CHAPTER14_5_ABSTREMP_H67 #include <iostream>8 #include <string>910 class abstr_emp { 11 private: 12std::string fname; 13std::string lname; 14std::string job; 15 public: 16abstr_emp(); 17abstr_emp(const std::string & fn, const std::string & ln, const std::string & j); 18abstr_emp(const abstr_emp & e); 19virtual ~abstr_emp() = 0; 20virtual void showAll() const; 21virtual void setAll(); 22friend std::ostream& operator<<(std::ostream & os, const abstr_emp & e); 23 }; 2425 class employee : public abstr_emp { 26 public: 27employee(); 28employee(const std::string & fn, const std::string & ln, const std::string & j); 29~employee(); 30virtual void showAll() const; 31virtual void setAll(); 32 }; 3334 class manager : virtual public abstr_emp { 35 private: 36int inchargeof; 37 protected: 38int InChargeOf() const {return inchargeof;} 39int & InChargeOf() {return inchargeof;} 40 public: 41manager(); 42manager(const std::string & fn, const std::string & ln, const std::string & j, int ico = 0); 43manager(const abstr_emp & e, int ico); 44manager(const manager & m); 45virtual void showAll() const; 46virtual void setAll(); 47 }; 4849 class fink : virtual public abstr_emp { 50 private: 51std::string reportsto; 52 protected: 53const std::string ReportsTo() const {return reportsto;} 54std::string & ReportsTo() {return reportsto;} 55 public: 56fink(); 57fink(const std::string & fn, const std::string & ln, const std::string & j, const std::string & rpo); 58fink(const abstr_emp & e, const std::string & rpo); 59fink(const fink & e); 60virtual void showAll() const; 61virtual void setAll(); 62 }; 6364 class highfink : public manager, public fink { 65 public: 66highfink(); 67highfink(const std::string & fn, const std::string & ln, const std::string & j, const std::string & rpo, int ico); 68highfink(const abstr_emp & e, const std::string & rpo, int ico); 69highfink(const fink & f, int ico); 70highfink(manager & m, const std::string & rpo); 71highfink(const highfink & h); 72virtual void showAll() const; 73virtual void setAll(); 74 }; 7576 #endif //LEARN_CPP_CHAPTER14_5_ABSTREMP_H 7778 // chapter14_5_abstremp.cpp 798081 #include "chapter14_5_abstremp.h" 8283 abstr_emp::abstr_emp() 84: fname("none"), lname("none"), job("none"){ 8586 } 8788 abstr_emp::abstr_emp(const std::string &fn, const std::string &ln, const std::string &j) 89: fname(fn), lname(ln), job(j){ 9091 } 9293 abstr_emp::abstr_emp(const abstr_emp & e) { 94fname = e.fname; 95lname = e.lname; 96job = e.job; 97 } 9899 abstr_emp::~abstr_emp() {100 101 }102 103 void abstr_emp::showAll() const {104using namespace std;105cout << "fname: " << fname << endl;106cout << "lname: " << lname << endl;107cout << "job: " << job << endl;108 }109 110 void abstr_emp::setAll() {111using namespace std;112cout << "reset All" << endl;113cout << "fname: "; cin >> fname;114cout << "lname: "; cin >> lname;115cout << "job: "; cin >> job;116 }117 118 std::ostream& operator<<(std::ostream & os, const abstr_emp & e) {119os << "fname: " << e.fname << std::endl;120os << "lname: " << e.lname << std::endl;121os << "job: " << e.job << std::endl;122return os;123 }124 125 employee::employee()126: abstr_emp("none", "none", "none"){127 128 }129 130 employee::employee(const std::string &fn, const std::string &ln, const std::string &j)131: abstr_emp(fn, ln, j) {132 133 }134 135 employee::~employee() {136 137 }138 139 void employee::showAll() const {140abstr_emp::showAll();141 }142 143 void employee::setAll() {144abstr_emp::setAll();145 }146 147 manager::manager()148: abstr_emp("none", "none", "none"){149inchargeof = 0;150 }151 152 manager::manager(const std::string &fn, const std::string &ln, const std::string &j, int ico)153: abstr_emp(fn, ln, j){154inchargeof = ico;155 }156 157 manager::manager(const abstr_emp &e, int ico)158: abstr_emp(e){159inchargeof = ico;160 }161 162 manager::manager(const manager &m)163: abstr_emp(m){164inchargeof = m.inchargeof;165 }166 167 void manager::showAll() const {168using namespace std;169abstr_emp::showAll();170cout << "ico: " << inchargeof << endl;171 }172 173 void manager::setAll() {174using namespace std;175abstr_emp::setAll();176cout << "ico: "; cin >> inchargeof;177 }178 179 fink::fink()180: abstr_emp("none", "none", "none"), reportsto("none"){181 182 }183 184 fink::fink(const std::string &fn, const std::string &ln, const std::string &j, const std::string &rpo)185: abstr_emp(fn, ln, j), reportsto(rpo){186 187 }188 189 fink::fink(const abstr_emp &e, const std::string &rpo)190: abstr_emp(e), reportsto(rpo){191 192 }193 194 fink::fink(const fink &e)195: abstr_emp(e){196reportsto = e.reportsto;197 }198 199 void fink::showAll() const {200using namespace std;201abstr_emp::showAll();202cout << "reportsto: " << reportsto << endl;203 }204 205 void fink::setAll() {206using namespace std;207abstr_emp::setAll();208cout << "reportsto: "; cin >> reportsto;209 }210 211 highfink::highfink()212: abstr_emp("none", "none", "none"), manager("none", "none", "none", 0),213fink("none", "none", "none", "none"){214 215 }216 217 highfink::highfink(const std::string &fn, const std::string &ln, const std::string &j, const std::string &rpo, int ico)218: abstr_emp(fn, ln, j), manager(fn, ln, j, ico), fink(fn, ln, j, rpo){219 220 }221 222 highfink::highfink(const abstr_emp &e, const std::string &rpo, int ico)223: abstr_emp(e), manager(e, ico), fink(e, rpo){224 225 }226 227 highfink::highfink(const fink &f, int ico)228: abstr_emp(f), manager(f, ico), fink(f){229 }230 231 highfink::highfink(manager &m, const std::string &rpo)232: abstr_emp(m), manager(m), fink(m, rpo){233 234 }235 236 highfink::highfink(const highfink &h)237: abstr_emp(h), manager(h), fink(h){238 239 }240 241 void highfink::showAll() const {242fink::showAll();243manager::showAll();244 }245 246 void highfink::setAll() {247manager::setAll();248fink::setAll();249 }250 251 // run252 253 void ch14_5() {254using namespace std;255employee em("trip", "harris", "thumper");256cout << em << endl;257em.showAll();258manager ma("amorphia", "spindragon", "nuancer", 5);259cout << ma << endl;260 261fink fi("matt", "oggs", "oiler", "junobarr");262cout << fi << endl;263fi.showAll();264highfink hf(ma, "curly kew");265hf.showAll();266 267highfink hf2;268hf2.setAll();269 270cout << "using an abstr_emp * pointer: " << endl;271abstr_emp * tri[4] = {&em, &fi, &hf, &hf2};272for (int i = 0; i < 4; ++ i)273tri[i] -> showAll();274return;275 }【【C++ Primer Plus】编程练习答案——第14章】 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数