C++Primer Plus 第十四章代碼重用:編程練習,第一題
提示:這里可以添加系列文章的所有文章的目錄,目錄需要自己手動添加
C++Primer Plus 第十四章代碼重用:編程練習,第一題
提示:寫完文章后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
文章目錄
- C++Primer Plus 第十四章代碼重用:編程練習,第一題
- 14.7 編程練習
- 題.
- 答
- 頭文件定義
- 方法的實現
14.7 編程練習
題.
Wine 類有一個 sting類對象成員(參見第4章)和一個Pair 對象(參見本章);其中前者用于存儲葡萄酒的名稱,而后者有2個valamray<inp對象(參見本章),這兩個valanay對象分別保存了葡萄酒的釀造年份和該年生產的瓶數。例如,Pair的第1個valarray對象可能為1988、1992和1996年,第2個valarray對象可能為 24、48和144瓶。Wine 最好有1個imnt 成員用于存儲年數。另外,一些typedef可能有助于簡化編程工作:
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt,ArrayInt> PairArray;
這樣,PairAmray 表示的是類型 Pair<std::valarray,std:valarray>。使用包含來實現 Wine 類,并用一個簡單的程序對其進行測試。Wine 類應該有一個默認構造函數以及如下構造函數:
//initialize label to l,number of years to y,
//vintage years to yrl],bottles to bot[]
wine(const char*l,int y,const int yr[],const int bot[]);
//initialize label to l,number of years to y.
//create array objects of length y
Wine(const char*l int y);
Wimne 類應該有一個 GetBotles()方法,它根據 Wine 對象能夠存儲兒種年份(y),提示用戶輸入年份和瓶數。方法 Label()返回一個指向葡萄酒名稱的引用。sum()方法返回 Pair 對象中第二個 valamay對象中的瓶數總和。測試程序應提示用戶輸入葡萄酒名稱、元素個數以及每個元素存儲的年份和瓶數等信息。程序將使用這些數據來構造一個 Wine 對象,然后顯示對象中保存的信息。
下面是一個簡單的測試程序:
// pe14-1.cpp--using Wine class with containment
#include <iostream>
#include "winec.h"
int main (void )
{using std::cin;using std::cout;using std::endl;cout <<"Enter name of wine:";char lab[50];cin.getline(lab,50);cout <<"Enter numberofyears:";int yrs;cin >>yrs;Wine holding(lab,yrs);//store label,years,give arrays yrs elementsholding.GetBottles();//solicit input for year,bottle countholding.show();//display object contentsconst int YRs =3;int y[YRs]={1993,1995,1998};int b[YRs]={48,60,72};
//create new object,initialize using data in arrays y and b
Wine more("Gushing Grape Red",YRS,y,b);
more.Show();
cout << "Total bottles for "<< more.Label()//use Label()method<<":"<< more.sum()<< endl;//use sum()method
cout << "Bye\n";return 0;
}
答
僅供參考
頭文件定義
#pragma once#include <iostream>
#include <valarray>namespace n_wine
{using std::string;using std::pair;using std::valarray;class Wine{private:typedef valarray<unsigned> UnsignedArray;public:Wine (const string& name, unsigned years_total, unsigned vintage_years[], unsigned bottles_total[]);Wine (const string& name, unsigned years_total);const string& getLabel (void) const;void getBottles (void);void show (void) const;unsigned sum (void) const;private:const string name_;const unsigned years_total_; // 年份數量pair<UnsignedArray, UnsignedArray> vintage_year_and_bottles_total_; // 釀造年份與對應瓶數};
}
方法的實現
#include "Wine.h"namespace n_wine
{using std::cin;using std::cout;using std::endl;using std::cerr;// ====================================// 公有成員函數// ====================================Wine::Wine (const string& name, unsigned years_total, unsigned vintage_years[], unsigned bottles_total[]): name_(name),years_total_(years_total), vintage_year_and_bottles_total_(UnsignedArray(vintage_years, years_total), UnsignedArray(bottles_total, years_total)){;}Wine::Wine (const string& name, unsigned years_total): name_(name),years_total_(years_total), vintage_year_and_bottles_total_(UnsignedArray(years_total), UnsignedArray(years_total)){;}const string&Wine::getLabel (void) const{return (name_);}voidWine::getBottles (void){cout << "Enter "<< name_ << " data for " << years_total_ << " year(s) ---- \n";for (unsigned i = 0; i < years_total_; ++i) {unsigned vintage_year;cout << "Enter year: ";cin >> vintage_year;if (!cin) {cerr << "ERREOR! " << __FILE__ << ", " << __LINE__ << endl;exit(EXIT_FAILURE);}vintage_year_and_bottles_total_.first[i] = vintage_year;unsigned bottles_total;cout << "Enter bottles for that year: ";cin >> bottles_total;if (!cin) {cerr << "ERREOR! " << __FILE__ << ", " << __LINE__ << endl;exit(EXIT_FAILURE);}vintage_year_and_bottles_total_.second[i] = bottles_total;}}voidWine::show (void) const{cout << "Wine: " << name_ << '\n';cout << '\t' << "Year" << '\t' << "Bottles" << '\n';for (unsigned i = 0; i < years_total_; ++i) {cout << '\t' << vintage_year_and_bottles_total_.first[i] << '\t' << vintage_year_and_bottles_total_.second[i] << endl;}}unsignedWine::sum (void) const{return (vintage_year_and_bottles_total_.second.sum());}
}