《C++ Primer Plus》14章编程练习1、2
2012-03-25 14:55
363 查看
就当练下手吧。后面的题不是太长就是太烦,不做了。
//Create a class by using the technique of composition //Charpter 14,Programming Exercise 01 //File name: ch14-1.h #ifndef _WINE_ #define _WINE_ #include <string> #include <valarray> using std::string; using std::valarray; template <typename T1, typename T2> class Pair { private: T1 a; T2 b; public: Pair() {} Pair(const T1& aV, const T2& bV): a(aV), b(bV) {} const T1& getFirst() const {return a; } const T2& getSecond() const {return b; } T1& setFirst() {return a; } T2& setSecond(){return b; } }; class Wine { private: typedef valarray<int> intArray; typedef Pair<intArray,intArray> PairArray; string label; //reference to wine name PairArray WineInfo; //producted year and number of bottles int years; //number of years public: Wine() {} Wine(const char* l, int y, const int yr[], const int bot[]); Wine(const char* l, int y); void GetBottles(); const string& Label() const {return label; } int sum () const; void Show() const; }; #endif
#include <iostream> #include "ch14-1.h" using std::cout; using std::endl; using std::cin; using std::left; Wine::Wine(const char* l, int y, const int yr[], const int bot[]) : label(l),years(y) { WineInfo.setFirst().resize(y); WineInfo.setSecond().resize(y); for(int i = 0; i< y; i++) { WineInfo.setFirst()[i] = yr[i]; WineInfo.setSecond()[i] = bot[i]; } } Wine::Wine(const char* l, int y): label(l),years(y) { WineInfo.setFirst().resize(y); WineInfo.setSecond().resize(y); } void Wine::GetBottles() { cout << "Enter " << label << " data for " << years << " year(s)\n"; for(int i = 0; i< years; i++) { cout << "Enter year: "; cin >> WineInfo.setFirst()[i]; cout << "Enter Bottles for that year: "; cin >> WineInfo.setSecond()[i]; } } int Wine::sum() const { return WineInfo.getSecond().sum(); } void Wine::Show() const { cout << "Wine: " << label << endl; cout << " Year Bottles\n"; for(int i = 0; i< years; i++) { cout << " "; cout.width(8); cout << left << WineInfo.getFirst()[i]; cout << left << WineInfo.getSecond()[i] << endl; } }
-------------------------Programming-Exercise-2----------------------------
//Create a class by using the technique of private inheritance //Charpter 14,Programming Exercise 02 //File name: ch14-2.h #ifndef _WINE_ #define _WINE_ #include <string> #include <valarray> using std::string; using std::valarray; template <typename T1, typename T2> class Pair { private: T1 a; T2 b; public: Pair() {} Pair(const T1& aV, const T2& bV): a(aV), b(bV) {} const T1& getFirst() const {return a; } const T2& getSecond() const {return b; } T1& setFirst() {return a; } T2& setSecond(){return b; } }; typedef valarray<int> intArray; typedef Pair<intArray,intArray> PairArray; class Wine: private PairArray,private string { public: Wine() {} Wine(const char* l, int y, const int yr[], const int bot[]); Wine(const char* l, int y); void GetBottles(); const string& Label() const {return (const string&)*this; } int sum () const; void Show() const; }; #endif
#include <iostream> #include "ch14-2.h" using std::cout; using std::endl; using std::cin; using std::left; Wine::Wine(const char* l, int y, const int yr[], const int bot[]) : string(l) { PairArray::setFirst() = intArray(yr,y); PairArray::setSecond() = intArray(bot,y); } Wine::Wine(const char* l, int y) :string(l) { PairArray::setFirst() = intArray(y); PairArray::setSecond() = intArray(y); } void Wine::GetBottles() { int years = PairArray::getFirst().size(); cout << "Enter " << (const string&)*this << " data for " << years << " year(s)\n"; for(int i = 0; i< years; i++) { cout << "Enter year: "; cin >> ((PairArray*)this)->setFirst()[i]; cout << "Enter Bottles for that year: "; cin >> ((PairArray*)this)->setSecond()[i]; } } int Wine::sum() const { return ((PairArray*)this)->getSecond().sum(); } void Wine::Show() const { int years = PairArray::getFirst().size(); cout << "Wine: " << (const string&)*this << endl; cout << " Year Bottles\n"; for(int i = 0; i< years; i++) { cout << " "; cout.width(8); cout << left << ((PairArray const*)this)->getFirst()[i]; cout << left << ((PairArray const*)this)->getSecond()[i] << endl; } }
相关文章推荐
- C++primer plus第六版课后编程练习答案5.1
- C++primer plus第六版课后编程练习答案6.3
- C++primer plus第六版课后编程练习答案7.1
- C++ Primer Plus 第五版中文版 第六章 编程练习6
- C++ Primer Plus 第五版 第七章 编程练习 5 6
- C++primer plus第六版课后编程练习答案9.4
- C++primer plus第六版课后编程练习答案10.2
- 【学习C++】C++ Primer Plus (第六版)第八章编程练习1-7
- C++primer plus第六版课后编程练习答案12.1
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习4
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习9
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第七章(chapter 7) 6-10
- C++ Primer Plus第六版 第七章 编程练习答案
- C++ Primer Plus 第6版 中文版 第5章编程练习
- C++ primer plus第六版课后编程练习答案:7.3
- C++ Primer Plus (第6版)编程练习 代码-----第五章
- C++ Primer Plus编程练习 7.13-1
- C++ Primer Plus课后编程练习——第七章 函数-C++的编程模块(部分)
- C++ Primer Plus 第六版_编程练习(3)(Chapter_four 1-10)
- C++ Primer Plus 第六版_编程练习(5)(Chapter_five 6-10)