1 // chapter14_1_wine.h 2 3 #ifndef LEARN_CPP_CHAPTER14_1_WINE_H 4 #define LEARN_CPP_CHAPTER14_1_WINE_H 5 6 #include <valarray> 7 #include <string> 8 9 typedef std::valarray<int> ArrayInt; 10 typedef std::pair<ArrayInt, ArrayInt> PairArray; 11 12 class Wine { 13 private: 14 std::string label; 15 int numofyears; 16 PairArray year_bots; 17 public: 18 Wine(); 19 Wine(const char * l, int y, const int ye[], const int bot[]); 20 Wine(const char * l, int y); 21 ~Wine(); 22 void GetBottles(); 23 std::string & Label(); 24 int sum() const; 25 void show() const; 26 }; 27 28 #endif //LEARN_CPP_CHAPTER14_1_WINE_H 29 30 31 // chapter14_1_wine.cpp 32 33 #include "chapter14_1_wine.h" 34 #include <iostream> 35 36 Wine::Wine() { 37 label = "none"; 38 numofyears = 0; 39 year_bots.first = ArrayInt(numofyears); 40 year_bots.second = ArrayInt(numofyears); 41 } 42 43 Wine::Wine(const char *l, int y, const int *ye, const int *bot) { 44 label = l; 45 numofyears = y; 46 year_bots.first = ArrayInt(numofyears); 47 year_bots.second = ArrayInt(numofyears); 48 for (int i = 0; i < numofyears; ++ i) { 49 year_bots.first[i] = ye[i]; 50 year_bots.second[i] = bot[i]; 51 } 52 } 53 54 Wine::Wine(const char *l, int y) { 55 label = l; 56 numofyears = y; 57 year_bots.first = ArrayInt(numofyears); 58 year_bots.second = ArrayInt(numofyears); 59 } 60 61 Wine::~Wine() { 62 63 } 64 65 void Wine::GetBottles() { 66 using namespace std; 67 for (int i = 0; i < numofyears; ++ i) { 68 cout << "#" << i << " year: "; 69 cin >> year_bots.first[i]; 70 cout << "#" << i << " bottles: "; 71 cin >> year_bots.second[i]; 72 } 73 cout << "finished" << endl; 74 } 75 76 std::string &Wine::Label() { 77 return label; 78 } 79 80 int Wine::sum() const { 81 return year_bots.second.sum(); 82 } 83 84 void Wine::show() const { 85 using namespace std; 86 cout << "yeartbottles" << endl; 87 for (int i = 0; i < numofyears; ++ i) 88 cout << year_bots.first[i] << "t" << year_bots.second[i] << endl; 89 } 90 91 92 // run 93 94 void ch14_1() { 95 using namespace std; 96 cout << "enter name of wine: "; 97 char lab[50]; 98 cin.getline(lab, 50); 99 cout << "enter number of years: "; 100 int yrs; 101 cin >> yrs; 102 103 Wine holding(lab, yrs); 104 holding.GetBottles(); 105 holding.show(); 106 107 const int YRS = 3; 108 int y[YRS] = {1993, 1995, 1998}; 109 int b[YRS] = {48, 60, 72}; 110 Wine more("Gushing Grape Red", YRS, y, b); 111 more.show(); 112 cout << "total bottles for " << more.Label() << ": " << more.sum() << endl; 113 cout << "bye" << endl; 114 return; 115 }
1 // chatper14_2_wine2.h 2 3 #ifndef LEARN_CPP_CHAPTER14_2_WINE2_H 4 #define LEARN_CPP_CHAPTER14_2_WINE2_H 5 6 #include <valarray> 7 #include <string> 8 9 typedef std::valarray<int> ArrayInt; 10 typedef std::pair<ArrayInt, ArrayInt> PairArray; 11 12 class Wine2 : private std::string, private PairArray { 13 private: 14 int numofyears; 15 public: 16 Wine2(); 17 Wine2(const char * l, int y, const int ye[], const int bot[]); 18 Wine2(const char * l, int y); 19 ~Wine2(); 20 void GetBottles(); 21 std::string & Label(); 22 int sum() const; 23 void show() const; 24 }; 25 26 #endif //LEARN_CPP_CHAPTER14_2_WINE2_H 27 28 29 // chapter14_2_wine2.cpp 30 31 #include "chapter14_2_wine2.h" 32 #include <iostream> 33 34 Wine2::Wine2() : std::string("none"), PairArray(ArrayInt(0),ArrayInt(0)){ 35 numofyears = 0; 36 } 37 38 Wine2::Wine2(const char *l, int y, const int *ye, const int *bot) 39 : std::string(l), PairArray(ArrayInt(y),ArrayInt(y)){ 40 numofyears = y; 41 for (int i = 0; i < numofyears; ++ i) { 42 PairArray::first[i] = ye[i]; 43 PairArray::second[i] = bot[i]; 44 } 45 } 46 47 Wine2::Wine2(const char *l, int y) 48 : std::string(l), PairArray(ArrayInt(y),ArrayInt(y)){ 49 numofyears = y; 50 } 51 52 Wine2::~Wine2() { 53 54 } 55 56 void Wine2::GetBottles() { 57 using namespace std; 58 for (int i = 0; i < numofyears; ++ i) { 59 cout << "#" << i << " year: "; 60 cin >> PairArray::first[i]; 61 cout << "#" << i << " bottles: "; 62 cin >> PairArray::second[i]; 63 } 64 cout << "finished" << endl; 65 } 66 67 std::string &Wine2::Label() { 68 return (std::string &)*this; 69 } 70 71 int Wine2::sum() const { 72 return PairArray::second.sum(); 73 } 74 75 void Wine2::show() const { 76 using namespace std; 77 cout << "yeartbottles" << endl; 78 for (int i = 0; i < numofyears; ++ i) 79 cout << PairArray::first[i] << "t" << PairArray::second[i] << endl; 80 } 81 82 83 // run 84 85 void ch14_2() { 86 using namespace std; 87 cout << "enter name of wine: "; 88 char lab[50]; 89 cin.getline(lab, 50); 90 cout << "enter number of years: "; 91 int yrs; 92 cin >> yrs; 93 94 Wine2 holding(lab, yrs); 95 holding.GetBottles(); 96 holding.show(); 97 98 const int YRS = 3; 99 int y[YRS] = {1993, 1995, 1998}; 100 int b[YRS] = {48, 60, 72}; 101 Wine2 more("Gushing Grape Red", YRS, y, b); 102 more.show(); 103 cout << "total bottles for " << more.Label() << ": " << more.sum() << endl; 104 cout << "bye" << endl; 105 return; 106 }
1 //chatper14_3_queuetp.h 2 3 #ifndef LEARN_CPP_CHAPTER14_3_QUEUETP_H 4 #define LEARN_CPP_CHAPTER14_3_QUEUETP_H 5 6 #include <string> 7 8 class Worker { 9 private: 10 std::string fullname; 11 long id; 12 public: 13 Worker(); 14 Worker(const std::string & s, long n); 15 ~Worker(); 16 Worker & operator=(const Worker & w); 17 void show() const; 18 }; 19 20 template <typename Type> 21 class QueueTp { 22 private: 23 enum {MAX = 100}; 24 Type items[MAX]; 25 int front; 26 int rear; 27 public: 28 QueueTp(); 29 ~QueueTp(); 30 bool isEmpty(); 31 bool isFull(); 32 bool enQueue(const Type &t); 33 bool deQueue(Type &t); 34 int num() const {return (rear - front + MAX) % MAX;} 35 }; 36 37 template<typename Type> 38 QueueTp<Type>::QueueTp() { 39 front = rear = 0; 40 } 41 42 template<typename Type> 43 QueueTp<Type>::~QueueTp() { 44 45 } 46 47 template<typename Type> 48 bool QueueTp<Type>::isEmpty() { 49 if (front == rear && front == 0) 50 return true; 51 return false; 52 } 53 54 template<typename Type> 55 bool QueueTp<Type>::isFull() { 56 if ((rear + 1) % MAX == front) 57 return true; 58 return false; 59 } 60 61 template<typename Type> 62 bool QueueTp<Type>::enQueue(const Type &t) { 63 if (isFull()) 64 return false; 65 items[rear] = t; 66 rear = (rear + 1) % MAX; 67 return true; 68 } 69 70 template<typename Type> 71 bool QueueTp<Type>::deQueue(Type &t) { 72 if (isEmpty()) 73 return false; 74 t = items[front]; 75 front = (front + 1) % MAX; 76 return true; 77 } 78 79 #endif //LEARN_CPP_CHAPTER14_3_QUEUETP_H 80 81 82 83 // chapter14_3_queuetp.cpp 84 85 #include "chapter14_3_queuetp.h" 86 #include <iostream> 87 88 Worker::Worker() 89 : fullname("none"){ 90 id = 0; 91 } 92 93 Worker::Worker(const std::string &s, long n) 94 : fullname(s){ 95 id = n; 96 } 97 98 Worker::~Worker() { 99 100 } 101 102 Worker & Worker::operator=(const Worker & w) { 103 fullname = w.fullname; 104 id = w.id; 105 return *this; 106 } 107 108 void Worker::show() const { 109 using namespace std; 110 cout << "name: " << fullname << endl; 111 cout << "id: " << id << endl; 112 } 113 114 115 // run 116 117 void ch14_3() { 118 using namespace std; 119 Worker * lolas[5]; 120 Worker * t; 121 for (int i = 0; i < 5; ++ i) 122 lolas[i] = new Worker("worker" + i, i); 123 QueueTp<Worker *> q; 124 q.enQueue(lolas[0]); 125 q.enQueue(lolas[1]); 126 q.enQueue(lolas[2]); 127 cout << q.num() << "items in q" << endl; 128 q.enQueue(lolas[3]); 129 q.enQueue(lolas[4]); 130 cout << q.num() << "items in q" << endl; 131 q.deQueue(t); 132 cout << q.num() << "items in q" << endl; 133 }
1 // chapter14_4_person.h 2 3 #ifndef LEARN_CPP_CHAPTER14_4_PERSON_H 4 #define LEARN_CPP_CHAPTER14_4_PERSON_H 5 6 #include <iostream> 7 8 class Personn { 9 private: 10 std::string firstname; 11 std::string lastname; 12 public: 13 Personn(); 14 Personn(std::string f, std::string l); 15 ~Personn(); 16 virtual void show() const; 17 }; 18 19 class Gunslmger : virtual public Personn { 20 private: 21 int num; 22 public: 23 Gunslmger(); 24 Gunslmger(std::string f, std::string l, int n); 25 ~Gunslmger(); 26 double draw() const; 27 virtual void show() const; 28 }; 29 30 class PokerPlayer : virtual public Personn { 31 private: 32 int poke; 33 public: 34 PokerPlayer(); 35 PokerPlayer(std::string f, std::string l, int p); 36 ~PokerPlayer(); 37 int draw() const; 38 virtual void show() const; 39 }; 40 41 class BadDude : public PokerPlayer, public Gunslmger { 42 public: 43 BadDude(); 44 BadDude(std::string f, std::string l, int n, int p); 45 ~BadDude(); 46 double Gdraw() const; 47 int Cdraw() const; 48 virtual void show() const; 49 }; 50 51 52 53 #endif //LEARN_CPP_CHAPTER14_4_PERSON_H 54 55 // chapter14_4_person.cpp 56 57 #include "chapter14_4_person.h" 58 #include <iostream> 59 60 Personn::Personn() 61 : firstname("none"), lastname("none"){ 62 63 } 64 65 Personn::Personn(std::string f, std::string l) 66 : firstname(f), lastname(l){ 67 68 } 69 70 Personn::~Personn() { 71 72 } 73 74 void Personn::show() const { 75 using namespace std; 76 cout << firstname << " " << lastname << endl; 77 } 78 79 Gunslmger::Gunslmger() 80 : Personn("none", "none"){ 81 num = 0; 82 } 83 84 Gunslmger::Gunslmger(std::string f, std::string l, int n) 85 : Personn(f, l){ 86 num = n; 87 } 88 89 Gunslmger::~Gunslmger() { 90 91 } 92 93 double Gunslmger::draw() const { 94 return num; 95 } 96 97 void Gunslmger::show() const { 98 using namespace std; 99 Personn::show(); 100 cout << "num: " << num << endl; 101 } 102 103 PokerPlayer::PokerPlayer() 104 : Personn("none", "none"){ 105 poke = 0; 106 } 107 108 PokerPlayer::PokerPlayer(std::string f, std::string l, int p) 109 : Personn(f, l){ 110 poke = p; 111 } 112 113 PokerPlayer::~PokerPlayer() { 114 115 } 116 117 int PokerPlayer::draw() const { 118 return poke; 119 } 120 121 void PokerPlayer::show() const { 122 using namespace std; 123 Personn::show(); 124 cout << "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 { 142 return Gunslmger::draw(); 143 } 144 145 int BadDude::Cdraw() const { 146 return PokerPlayer::draw(); 147 } 148 149 void BadDude::show() const { 150 Gunslmger::show(); 151 PokerPlayer::show(); 152 } 153 154 // run 155 156 157 void ch14_4() { 158 using namespace std; 159 Gunslmger g("f1","l1",1); 160 PokerPlayer p("f2", "l2", 52); 161 BadDude b("f3", "l3", 2, 51); 162 cout << g.draw() << endl; 163 g.show(); 164 cout << p.draw() << endl; 165 p.show(); 166 b.show(); 167 b.Gdraw(); 168 b.Cdraw(); 169 }
1 // chapter14_5_abstremp.h 2 3 4 #ifndef LEARN_CPP_CHAPTER14_5_ABSTREMP_H 5 #define LEARN_CPP_CHAPTER14_5_ABSTREMP_H 6 7 #include <iostream> 8 #include <string> 9 10 class abstr_emp { 11 private: 12 std::string fname; 13 std::string lname; 14 std::string job; 15 public: 16 abstr_emp(); 17 abstr_emp(const std::string & fn, const std::string & ln, const std::string & j); 18 abstr_emp(const abstr_emp & e); 19 virtual ~abstr_emp() = 0; 20 virtual void showAll() const; 21 virtual void setAll(); 22 friend std::ostream & operator<<(std::ostream & os, const abstr_emp & e); 23 }; 24 25 class employee : public abstr_emp { 26 public: 27 employee(); 28 employee(const std::string & fn, const std::string & ln, const std::string & j); 29 ~employee(); 30 virtual void showAll() const; 31 virtual void setAll(); 32 }; 33 34 class manager : virtual public abstr_emp { 35 private: 36 int inchargeof; 37 protected: 38 int InChargeOf() const {return inchargeof;} 39 int & InChargeOf() {return inchargeof;} 40 public: 41 manager(); 42 manager(const std::string & fn, const std::string & ln, const std::string & j, int ico = 0); 43 manager(const abstr_emp & e, int ico); 44 manager(const manager & m); 45 virtual void showAll() const; 46 virtual void setAll(); 47 }; 48 49 class fink : virtual public abstr_emp { 50 private: 51 std::string reportsto; 52 protected: 53 const std::string ReportsTo() const {return reportsto;} 54 std::string & ReportsTo() {return reportsto;} 55 public: 56 fink(); 57 fink(const std::string & fn, const std::string & ln, const std::string & j, const std::string & rpo); 58 fink(const abstr_emp & e, const std::string & rpo); 59 fink(const fink & e); 60 virtual void showAll() const; 61 virtual void setAll(); 62 }; 63 64 class highfink : public manager, public fink { 65 public: 66 highfink(); 67 highfink(const std::string & fn, const std::string & ln, const std::string & j, const std::string & rpo, int ico); 68 highfink(const abstr_emp & e, const std::string & rpo, int ico); 69 highfink(const fink & f, int ico); 70 highfink(manager & m, const std::string & rpo); 71 highfink(const highfink & h); 72 virtual void showAll() const; 73 virtual void setAll(); 74 }; 75 76 #endif //LEARN_CPP_CHAPTER14_5_ABSTREMP_H 77 78 // chapter14_5_abstremp.cpp 79 80 81 #include "chapter14_5_abstremp.h" 82 83 abstr_emp::abstr_emp() 84 : fname("none"), lname("none"), job("none"){ 85 86 } 87 88 abstr_emp::abstr_emp(const std::string &fn, const std::string &ln, const std::string &j) 89 : fname(fn), lname(ln), job(j){ 90 91 } 92 93 abstr_emp::abstr_emp(const abstr_emp & e) { 94 fname = e.fname; 95 lname = e.lname; 96 job = e.job; 97 } 98 99 abstr_emp::~abstr_emp() { 100 101 } 102 103 void abstr_emp::showAll() const { 104 using namespace std; 105 cout << "fname: " << fname << endl; 106 cout << "lname: " << lname << endl; 107 cout << "job: " << job << endl; 108 } 109 110 void abstr_emp::setAll() { 111 using namespace std; 112 cout << "reset All" << endl; 113 cout << "fname: "; cin >> fname; 114 cout << "lname: "; cin >> lname; 115 cout << "job: "; cin >> job; 116 } 117 118 std::ostream & operator<<(std::ostream & os, const abstr_emp & e) { 119 os << "fname: " << e.fname << std::endl; 120 os << "lname: " << e.lname << std::endl; 121 os << "job: " << e.job << std::endl; 122 return 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 { 140 abstr_emp::showAll(); 141 } 142 143 void employee::setAll() { 144 abstr_emp::setAll(); 145 } 146 147 manager::manager() 148 : abstr_emp("none", "none", "none"){ 149 inchargeof = 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){ 154 inchargeof = ico; 155 } 156 157 manager::manager(const abstr_emp &e, int ico) 158 : abstr_emp(e){ 159 inchargeof = ico; 160 } 161 162 manager::manager(const manager &m) 163 : abstr_emp(m){ 164 inchargeof = m.inchargeof; 165 } 166 167 void manager::showAll() const { 168 using namespace std; 169 abstr_emp::showAll(); 170 cout << "ico: " << inchargeof << endl; 171 } 172 173 void manager::setAll() { 174 using namespace std; 175 abstr_emp::setAll(); 176 cout << "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){ 196 reportsto = e.reportsto; 197 } 198 199 void fink::showAll() const { 200 using namespace std; 201 abstr_emp::showAll(); 202 cout << "reportsto: " << reportsto << endl; 203 } 204 205 void fink::setAll() { 206 using namespace std; 207 abstr_emp::setAll(); 208 cout << "reportsto: "; cin >> reportsto; 209 } 210 211 highfink::highfink() 212 : abstr_emp("none", "none", "none"), manager("none", "none", "none", 0), 213 fink("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 { 242 fink::showAll(); 243 manager::showAll(); 244 } 245 246 void highfink::setAll() { 247 manager::setAll(); 248 fink::setAll(); 249 } 250 251 // run 252 253 void ch14_5() { 254 using namespace std; 255 employee em("trip", "harris", "thumper"); 256 cout << em << endl; 257 em.showAll(); 258 manager ma("amorphia", "spindragon", "nuancer", 5); 259 cout << ma << endl; 260 261 fink fi("matt", "oggs", "oiler", "junobarr"); 262 cout << fi << endl; 263 fi.showAll(); 264 highfink hf(ma, "curly kew"); 265 hf.showAll(); 266 267 highfink hf2; 268 hf2.setAll(); 269 270 cout << "using an abstr_emp * pointer: " << endl; 271 abstr_emp * tri[4] = {&em, &fi, &hf, &hf2}; 272 for (int i = 0; i < 4; ++ i) 273 tri[i] -> showAll(); 274 return; 275 }
字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数 字数
文章来源: 博客园
- 还没有人评论,欢迎说说您的想法!