第十四章 编程练习答案
这章看的头有点晕,好多东西都是网上看别人的代码,学的有点模糊
1.
//头文件,类的声明
#ifndef WINE_H_
#define WINE_H_
#include <iostream>
#include <string>
#include <valarray>
typedef std::valarray<int> ArrayInt;
typedef std::pair<ArrayInt, ArrayInt> PairArray;
class Wine
{
private:
std::string label;
PairArray number;
int yearnumber;
public:
// Wine(){}
Wine(const char * l, int y, const int yr[], const int bot[]);
Wine(const char * l, int y);
// ~Wine(){}
void GetBottles();
void Show();
const std::string & Label();
int sum();
};
#endif
//类的实现
#include "U14p1wine.h"
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;
using std::endl;
Wine::Wine(const char * l, int y, const int yr[], const int bot[])
{
label = l;
yearnumber = y;
ArrayInt a(y);
ArrayInt b(y);
for (int i = 0; i < y; ++i)
{
a[i] = yr[i];
b[i] = bot[i];
}
number = std::make_pair(a, b);
}
Wine::Wine(const char * l, int y)
{
label = l;
yearnumber = y;
ArrayInt a(y);
ArrayInt b(y);
number = std::make_pair(a, b);
}
void Wine::GetBottles()
{
cout << "Enter " << label << " data for " << yearnumber << " year(s):\n";
number.first.resize(yearnumber);
number.second.resize(yearnumber);
for (int i = 0; i < yearnumber; ++i)
{
cout << "Enter year: ";
cin >> number.first[i];
cout << "Enter bottles for that year: ";
cin >> number.second[i];
}
}
void Wine::Show()
{
cout << "Wine: " << label << endl;
cout << " Year Bottles" << endl;
for (int i = 0; i < yearnumber; i++)
{
cout << " " << number.first[i]
<< " " << number.second[i] << endl;
}
}
const std::string & Wine::Label()
{
return label;
}
int Wine::sum()
{
int count = 0;
for (int i = 0; i < yearnumber; i++)
count += number.second[i];
return count;
}
//书中的测试程序
#include <iostream>
#include "U14p1wine.h"
int main(void)
{
using std::cout;
using std::cin;
using std::endl;
cout << "Enter name of wine: ";
char lab[50];
cin.getline(lab, 50);
cout << "Enter number of years: ";
int yrs;
cin >> yrs;
Wine holding (lab, yrs);
holding.GetBottles();
holding.Show();
const int YRS = 3;
int y[YRS] = {1993, 1995, 1998};
int b[YRS] = {48, 60, 72};
Wine more("Gushing Grape Red", YRS, y, b);
more.Show();
cout << "Total bottles for " << more.Label()
<< ": " << more.sum() << endl;
cout << "Bye\n";
return 0;
}
2.
//头文件,类的声明
#ifndef CD_H_
#define CD_H_
//Brass Cd 类
class Cd
{
private:
char * performers;
char * label;
int selections;
double playtime;
public:
Cd(char * s1, char * s2, int n, double x);
Cd(const Cd & d);
Cd();
virtual ~Cd();
virtual void Report() const;
Cd & operator=(const Cd & d);
};
//Brass Classic 类
class Classic : public Cd
{
private:
char * name;
public:
Classic(char * s1, char * s2, char * s3, int n, double x);
Classic(const Classic & d, char * c);
Classic(const Classic & d);
Classic();
~ Classic();
virtual void Report() const;
Classic & operator=(const Classic & d);
};
#endif
//类的实现
#include <iostream>
#include <cstring>
#include "classic10.h"
using std::cout;
using std::endl;
using std::strcpy;
using std::strlen;
//Cd 实现方法
Cd::Cd(char * s1, char * s2, int n, double x)
{
performers = new char[strlen(s1) + 1];
strcpy(performers, s1);
label = new char[strlen(s2) + 1];
strcpy(label, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd & d)
{
performers = new char[strlen(d.performers) + 1];
strcpy(performers, d.performers);
label = new char[strlen(d.label) + 1];
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
}
Cd::Cd()
{
performers = new char[1];
performers[0] = '\0';
label = new char[1];
label[0] = '\0';
selections = 0;
playtime = 0;
}
Cd::~Cd()
{
delete [] label;
delete [] performers;
}
void Cd::Report() const
{
cout << "performers: "<< performers << endl;
cout << "label: " << label << endl;
cout << "number of selections: " << selections << endl;
cout << "playing time in minutes: " << playtime << endl;
cout << endl;
}
Cd & Cd::operator=(const Cd & d)
{
if (this == &d)
return *this;
delete [] label;
delete [] performers;
performers = new char[strlen(d.performers) + 1];
strcpy(performers, d.performers);
label = new char[strlen(d.label) + 1];
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
//Classic 实现方法
//成员初始化列表
Classic::Classic(char * s1, char * s2, char * s3, int n, double x) : Cd(s1, s2, n, x)
{
name = new char[strlen(s3) + 1];
strcpy(name, s3);
}
Classic::Classic(const Classic & d, char * c) : Cd(d)
{
name = new char[strlen(d.name) + 1];
strcpy(name, c);
}
Classic::Classic()
{
name = new char[1];
name[0] = '\0';
}
Classic::~Classic()
{
delete [] name;
}
//重新定义report
void Classic::Report() const
{
cout << "name: " << name << endl;
Cd::Report();
}
Classic & Classic::operator=(const Classic & d)
{
if (this == &d)
return *this;
Cd::operator=(d);
delete [] name;
name = new char[strlen(d.name) + 1];
strcpy(name, d.name);
return *this;
}
//测试程序
#include <iostream>
using namespace std;
#include "classic10.h"
void Bravo(const Cd & disk);
int main()
{
Cd c1("Beatles", "Capitol", 14, 35.5);
Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C",
"Alfred Brendel", "Philips", 2, 57.17);
Cd *pcd = &c1;
cout << "Using object directly:\n";
c1.Report();
c2.Report();
cout << "Using type cd * pointer to objects:\n";
pcd->Report();
pcd = &c2;
pcd->Report();
cout << "Calling a function with a Cd reference argument:\n";
Bravo(c1);
Bravo(c2);
cout << "Testing assignment: \n";
Classic copy;
copy= c2;
copy.Report();
return 0;
}
void Bravo(const Cd & disk)
{
disk.Report();
}
3.
//头文件,类的声明
#ifndef WORKERMI_H_
#define WORKERMI_H_
#include <string>
class Worker
{
private:
std::string fullname;
long id;
protected:
virtual void Data() const;
virtual void Get();
public:
Worker() : fullname("no one"), id(0L) {}
Worker(const std::string & s, long n)
: fullname(s), id(n) {}
virtual ~Worker() = 0;
virtual void Set() = 0;
virtual void Show() const = 0;
};
class Waiter : virtual public Worker
{
private:
int panache;
protected:
void Data() const;
void Get();
public:
Waiter() : Worker(), panache(0) {}
Waiter(const std::string & s, long n, int p = 0)
: Worker(s, n), panache(p) {}
Waiter(const Worker & wk, int p = 0)
: Worker(wk), panache(p) {}
void Set();
void Show() const;
};
class Singer : virtual public Worker
{
protected:
enum {other, alto, contralto, soprano,
bass, baritone, tenor};
enum {Vtypes = 7};
void Data() const;
void Get();
private:
static char *pv[Vtypes];
int voice;
public:
Singer() : Worker(), voice(other) {}
Singer(const std::string & s, long n, int v = other)
: Worker(s, n), voice(v) {}
Singer(const Worker & wk, int v = other)
: Worker(wk), voice(v) {}
void Set();
void Show() const;
};
class SingingWaiter : public Singer, public Waiter
{
protected:
void Data() const;
void Get();
public:
SingingWaiter() {}
SingingWaiter(const std::string & s, long n, int p = 0, int v = other)
: Worker(s, n), Waiter(s, n, p), Singer(s, n, v) {}
SingingWaiter(const Worker & wk, int p = 0, int v = other)
: Worker(wk), Waiter(wk, p), Singer(wk, v) {}
SingingWaiter(const Worker & wt, int v = other)
: Worker(wt), Waiter(wt), Singer(wt, v) {}
SingingWaiter(const Singer & wt, int p = 0)
: Worker(wt), Waiter(wt, p), Singer(wt) {}
void Set();
void Show() const;
};
#endif
//另外一个头文件,包含了类的声明和实现
#ifndef QUEUETP_H_
#define QUEUETP_H_
template <class T>
class QueueTp
{
private:
const int Q_size = 10;
T * data;
int top;
public:
QueueTp(){data= new T[Q_size]; top = 0;}
QueueTp(int q){data = new T[2 * q]; top = 0;}
~QueueTp(){ delete[] data; }
bool isempty() { return top == 0; }
bool isfull() { return top == Q_size; }
bool Push(T item);
bool Pop();
T &front() const;
T &rear() const;
};
template <class T>
bool QueueTp<T>::Push(T item)
{
if (isfull())
return false;
for (int i = top; i > 0; --i)
data[i] = data[i - 1];
data[0] = item;
++top;
return true;
}
template <class T>
bool QueueTp<T>::Pop()
{
if (isempty())
return false;
--top;
return true;
}
template <class T>
T &QueueTp<T>::front() const
{
return data[top - 1];
}
template <class T>
T &QueueTp<T>::rear() const
{
return data[0];
}
#endif
//第一个类的实现
#include "14-10workermi.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
//worker类方法实现
//必须实现虚析构函数,即使是纯虚函数
Worker::~Worker() {}
void Worker::Data() const
{
cout << "Name: " << fullname << endl;
cout << "Employee ID: " << id << endl;
}
void Worker::Get()
{
getline(cin, fullname);
cout << "Enter worker's ID: ";
cin >> id;
while (cin.get() != '\n')
continue;
}
//waiter类方法实现
void Waiter::Set()
{
cout << "Enter waiter's name: ";
Worker::Get();
Get();
}
void Waiter::Show() const
{
cout << "Category: waiter\n";
Worker::Data();
Data();
}
void Waiter::Get()
{
cout << "Enter worker's panche rating: ";
cin >> panache;
while (cin.get() != '\n')
continue;
}
void Waiter::Data() const
{
cout << "Panache rating: " << panache << endl;
}
//singer类方法实现
char * Singer::pv[Singer::Vtypes] = {"other", "alto", "contralto",
"soprano", "bass", "baritone", "tensor"};
void Singer::Set()
{
cout << "Enter singer's name: ";
Worker::Get();
Get();
}
void Singer::Show() const
{
cout << "Category: singer\n";
Worker::Data();
Data();
}
void Singer::Data() const
{
cout << "Vocal range: " << pv[voice] << endl;
}
void Singer::Get()
{
cout << "Enter number for singer's vocal range:\n";
int i;
for (i = 0; i < Vtypes; i++)
{
cout << i << ": " << pv[i] << " ";
if (i % 4 == 3)
cout << endl;
}
if (i % 4 != 0)
cout << endl;
cin >> voice;
while (cin.get() != '\n')
continue;
}
//singingwaiter类方法实现
void SingingWaiter::Data() const
{
Singer::Data();
Waiter::Data();
}
void SingingWaiter::Get()
{
Waiter::Get();
Singer::Get();
}
void SingingWaiter::Set()
{
cout << "Enter singing waiter's name: ";
Worker::Get();
Get();
}
void SingingWaiter::Show() const
{
cout << "Categroy: singing waiter\n";
Worker::Data();
Data();
}
//测试程序
#include <iostream>
#include <cstring>
#include "14-10workermi.h"
#include "U14p3queuetp.h"
const int SIZE = 5;
int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::strchr;
QueueTp<Worker *> lolas(SIZE);
//原来是Worker * lolas[SIZE]
int ct;
for (ct = 0; ct < SIZE; ct++)
{
char choice;
cout << "Enter the employee category:\n"
<< "w: waiter s: singer "
<< "t: singing waiter q: quit\n";
cin >> choice;
while (strchr("wstq", choice) == NULL)
{
cout << "Please enter a w, s, t or q: ";
cin >> choice;
}
if (choice == 'q')
break;
switch(choice)
{
case 'w':lolas.Push(new Waiter);
break;
case 's':lolas.Push(new Singer);
break;
case 't':lolas.Push(new SingingWaiter);
break;
}
cin.get();
lolas.rear()->Set();
}
cout << "\nHere is your staff:\n";
int i;
for (i = 0; i < ct; i++)
{
cout << endl;
lolas.front()->Show();
lolas.Push(lolas.front());
lolas.Pop();
}
for (i = 0; i < ct; ++i)
{
delete lolas.front();
lolas.Pop();
}
cout << "Bye.\n";
return 0;
}
4.
//头文件,类的声明
#ifndef PERSON_H_
#define PERSON_H_
#include<iostream>
#include <string>
using std::string;
//这章有点晕,很多都是参考网上的答案的
class Person
{
private:
string firstname;
string lastname;
protected:
virtual void Data() const;
virtual void Get();
public:
Person(): firstname("null"), lastname("null") {};
Person(string f, string l)
: firstname(f), lastname(l) {};
virtual ~Person() = 0;
virtual void Set() = 0;
virtual void Show() const = 0;
};
class Gunslinger : virtual public Person
{
private:
double time;//拔枪时间
int num;//刻痕数
protected:
void Data() const;//派生类的虚函数virtual可以不写
void Get();
public:
Gunslinger() : Person(), time(0.0), num(0){}
Gunslinger(string f, string l, double t, int n)
: Person(f, l), time(t), num(n) {}
Gunslinger(Person & p, double t, int n)
:Person(p), time(t), num(n) {};
double Drawtime(){return time;}
void Set();
void Show() const;
};
class PokerPlayer : virtual public Person
{
private:
int card;
//这里简化了扑克牌,直接用1-52表示
protected:
void Data() const;
void Get();
public:
PokerPlayer() : Person(), card(1){};
PokerPlayer(string f, string l, int c)
: Person(f, l), card(c) {};
PokerPlayer(Person & p, int c)
:Person(p), card(c) {};
int Drawcard(){return card;}
void Set();
void Show() const;
};
class BadDude : public Gunslinger, public PokerPlayer
{
protected:
void Data() const;
void Get();
public:
BadDude() {};
BadDude(string f, string l, double t = 0.0, int n = 0, int c = 1)
: Person(f, l), Gunslinger(f, l, t, n), PokerPlayer(f, l, c){};
BadDude(Person & p, double t, int n, int c)
: Person(p), Gunslinger(p, t, n), PokerPlayer(p, c){};
BadDude(Gunslinger & gs, int c)
: Person(gs), Gunslinger(gs), PokerPlayer(gs, c){};
BadDude(PokerPlayer & pp, double t , int n)
: Person(pp), Gunslinger(pp, t, n), PokerPlayer(pp){};
double GDraw(){return Drawtime();}
int CDraw(){return Drawcard();}
void Set();
void Show() const;
};
#endif
//类的实现
#include "U14p4person.h"
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
Person::~Person(){}
void Person::Data() const
{
cout << "First name: " << firstname << endl;
cout << "Last name: " << lastname << endl;
}
void Person::Get()
{
cout << "Enter perosn's firstname: ";
getline(cin, firstname);
cout << "Enter perosn's lastname: ";
getline(cin, lastname);
}
void Gunslinger::Set()
{
Person::Get();
Get();
cout << endl;
}
void Gunslinger::Show() const
{
cout << "Category: Gunslinger\n";
Person::Data();
Data();
}
void Gunslinger::Data() const
{
cout << "The time: " << time << endl;
cout << "The number of scotch: " << num << endl;
}
void Gunslinger::Get()
{
cout << "Enter gunslinger's time: ";
cin >> time;
cout << "Enter gunslinger's number of scotch: ";
cin >> num;
while (cin.get() != '\n')
continue;
}
void PokerPlayer::Set()
{
Person::Get();
Get();
cout << endl;
}
void PokerPlayer::Show() const
{
cout << "Gategory: PokerPlayer\n";
Person::Data();
Data();
}
void PokerPlayer::Data() const
{
cout << "The card number: " << card << endl;
}
void PokerPlayer::Get()
{
cout << "Enter PokerPlayer's card: ";
cin >> card;
while (cin.get() != '\n')
continue;
}
void BadDude::Data() const
{
Gunslinger::Data();
PokerPlayer::Data();
}
void BadDude::Get()
{
Gunslinger::Get();
PokerPlayer::Get();
}
void BadDude::Set()
{
Person::Get();
Get();
cout << endl;
}
void BadDude::Show() const
{
Person::Data();
Data();
}
//测试程序
#include <iostream>
#include <cstring>
#include "U14p4person.h"
const int SIZE = 5;
int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::strchr;
Person * lolas[SIZE];
int ct;
for (ct = 0; ct < SIZE; ct++)
{
char choice;
cout << "Enter the employee category:\n"
<< "g: Gunslinger p: PokerPlayer "
<< "b: BadDude q: Quit\n";
cin >> choice;
while (strchr("gpbq", choice) == NULL)
{
cout << "Please enter a g, p, b or q: ";
cin >> choice;
}
if (choice == 'q')
break;
switch(choice)
{
case 'g': lolas[ct] = new Gunslinger;
break;
case 'p': lolas[ct] = new PokerPlayer;
break;
case 'b': lolas[ct] = new BadDude;
break;
}
cin.get();
lolas[ct]->Set();
}
cout << "\nHere is your person:\n";
int i;
for (i = 0; i < ct; i++)
{
cout << endl;
lolas[i]->Show();
}
for (i = 0; i < ct; i++)
delete lolas[i];
cout << "Bye.\n";
return 0;
}
5.
//书中的头文件程序
#include <iostream>
#include <string>
using std::string;
class abstr_emp
{
private:
string fname;
string lname;
string job;
public:
abstr_emp();
abstr_emp(const string & fn, const string & ln,
const string & j);
virtual void ShowAll() const;
virtual void SetAll();
friend std::ostream & operator<<(
std::ostream & os, const abstr_emp & e);
virtual ~abstr_emp() = 0;
};
class employee : public abstr_emp
{
public:
employee(const string & fn, const string & ln,
const string & j);
virtual void ShowAll() const;
virtual void SetAll();
};
class manager : virtual public abstr_emp
{
private:
int inchargeof;
protected:
int InChargeOf() const {return inchargeof; }
int & InChargeOf(){return inchargeof; }
public:
manager();
manager(const string & fn, const string & ln,
const string & j, int ico = 0);
manager(const abstr_emp & e, int ico);
manager(const manager & m);
virtual void ShowAll() const;
virtual void SetAll();
};
class fink : virtual public abstr_emp
{
private:
string reportsto;
protected:
const string ReportsTo() const {return reportsto; }
string & ReportsTo(){return reportsto; }
public:
fink();
fink(const string & fn, const string & ln,
const string & j, const string & rpo);
fink(const abstr_emp & e, const string & rpo);
fink(const fink & f);
virtual void ShowAll() const;
virtual void SetAll();
};
class highfink : public manager, public fink
{
public:
highfink();
highfink(const string & fn, const string & ln,
const string & j, const string & rpo, int ico);
highfink(const abstr_emp & e, const string & rpo, int ico);
highfink(const fink & f, int ico);
highfink(const manager & m, const string & rpo);
highfink(const highfink & h);
virtual void ShowAll() const;
virtual void SetAll();
};
//类的实现
#include <iostream>
#include <string>
#include "U14p5emp.h"
using namespace std;
abstr_emp::abstr_emp()
{
fname = "no name";
lname = "no name";
job = "no job";
}
abstr_emp::abstr_emp(const string & fn,
const string & ln, const string & j)
{
fname = fn;
lname = ln;
job = j;
}
abstr_emp::~abstr_emp() {}
void abstr_emp::ShowAll() const
{
cout << "First name: " << fname << endl;
cout << "Last name: " << lname << endl;
cout << "Job:" << job << endl;
}
void abstr_emp::SetAll()
{
cout << "Enter the first name: " << endl;
getline(cin, fname);
cout << "Enter the last name: " << endl;
getline(cin, lname);
cout << "Enter the job: " << endl;
getline(cin, job);
}
std::ostream & operator<<(ostream & os, const abstr_emp & e)
{
os << e.fname << " " << e.lname;
return os;
}
employee::employee(const string & fn, const string & ln,
const string & j) : abstr_emp(fn, ln, j)
{
}
void employee::ShowAll() const
{
abstr_emp::ShowAll();
cout << endl;
}
void employee::SetAll()
{
abstr_emp::SetAll();
}
manager::manager()
{
inchargeof = 0;
}
manager::manager(const string & fn, const string & ln, const string & j,
int ico) : abstr_emp(fn, ln, j), inchargeof(ico)
{
}
manager::manager(const abstr_emp & e, int ico)
: abstr_emp(e), inchargeof(ico)
{
}
manager::manager(const manager & m) : abstr_emp(m)
{
inchargeof = m.inchargeof;
}
void manager::ShowAll() const
{
abstr_emp::ShowAll();
cout << "Inchargeof: " << inchargeof << endl;
cout << endl;
}
void manager::SetAll()
{
abstr_emp::SetAll();
cout << "Enter the inchargeof: " << endl;
cin >> inchargeof;
while (cin.get() != '\n')
continue;//数字和字符混合输入时注意
}
fink::fink()
{
reportsto = "no name";
}
fink::fink(const string & fn, const string & ln, const string & j,
const string & rpo) : abstr_emp(fn, ln, j), reportsto(rpo)
{
}
fink::fink(const abstr_emp & e, const string & rpo)
: abstr_emp(e), reportsto(rpo)
{
}
fink::fink(const fink & f) : abstr_emp(f)
{
}
void fink::ShowAll() const
{
abstr_emp::ShowAll();
cout << "Reportsto: " << reportsto << endl;
cout << endl;
}
void fink::SetAll()
{
abstr_emp::SetAll();
cout << "Enter the reportsto: " << endl;
getline(cin, reportsto);
}
highfink::highfink()
{
}
highfink::highfink(const string & fn, const string & ln, const string & j,
const string & rpo, int ico) : abstr_emp(fn, ln, j), fink(fn, ln, j, rpo), manager(fn, ln, j, ico)
{
}
highfink::highfink(const abstr_emp & e, const string & rpo, int ico)
: abstr_emp(e), fink(e, rpo), manager(e, ico)
{
}
highfink::highfink(const fink & f, int ico)
: abstr_emp(f), fink(f), manager(f, ico)
{
}
highfink::highfink(const manager & m, const string & rpo)
: abstr_emp(m), fink(m, rpo), manager(m)
{
}
highfink::highfink(const highfink & h) : abstr_emp(h), fink(h), manager(h)
{
}
void highfink::ShowAll() const
{
manager::ShowAll();
cout << "Reportsto: " << ReportsTo() << endl;
cout << endl;
}
void highfink::SetAll()
{
manager::SetAll();
cout << "Enter the reportsto: " << endl;
getline(cin, fink::ReportsTo());
}
//书中的测试程序
#include <iostream>
using namespace std;
#include "U14p5emp.h"
int main(void)
{
employee em("Trip", "Harris", "Thumper");
cout << em << endl;
em.ShowAll();
manager ma("Amorphia", "Spindragon", "Nuancer", 5);
cout << ma << endl;
ma.ShowAll();
fink fi("Matt", "Oggs", "Oiler", "Juno Barr");
cout << fi << endl;
fi.ShowAll();
highfink hf(ma, "Curly Kew");
hf.ShowAll();
cout << "Press a key for next phase:\n";
cin.get();
highfink hf2;
hf2.SetAll();
cout << "Using an abstr_emp * pointer:\n";
abstr_emp * tri[4] = {&em, &fi, &hf, &hf2};
for (int i = 0; i < 4; i++)
tri[i]->ShowAll();
return 0;
}