C++ Primer Plus(第六版) 第十四章课后编程答案
程序员文章站
2023-12-23 11:10:46
...
参考链接:https://blog.csdn.net/weixin_41882882/article/details/82118401
https://blog.csdn.net/u012175089/article/details/53914092
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 "Wine.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"Wine.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 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, private PairArray
{
private:
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 "Wine.h"
#include<iostream>
#include<string>
#include<cstdlib>
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[])
{
(string &)(*this) = l;
yearnumber = y;
ArrayInt a(y);
ArrayInt b(y);
for (int i = 0; i < y; i++)
{
a[i] = yr[i];
b[i] = bot[i];
}
(PairArray &)(*this) = std::make_pair(a, b);
}
Wine::Wine(const char * l, int y)
{
(string &)(*this) = l;
yearnumber = y;
}
void Wine::GetBottles()
{
cout << "Enter " << (string &)(*this) << " data for " << yearnumber << " year(s):\n";
PairArray& number = (PairArray&)(*this);
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:" << (string&)(*this) << endl;
cout << " Year Bottles" << endl;
PairArray & number = (PairArray&)(*this);
for (int i = 0; i < yearnumber; i++)
{
cout << " " << number.first[i]
<< " " << number.second[i] << endl;
}
}
const std::string & Wine::Label()
{
return (string &)(*this);
}
int Wine::sum()
{
return ((PairArray&)(*this)).second.sum();
}
//书中的测试程序
#include<iostream>
#include"Wine.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;
}
3.
// workermi.h -- working classes with MI
#ifndef WORKERMI_H_
#define WORKERMI_H_
#include <string>
class Worker // an abstract base class
{
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; // pure virtual function
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]; // string equivs of voice types
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;
};
// multiple inheritance
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 Waiter & 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
// workermi.cpp -- working class methods with MI
#include "workermi.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
// Worker methods
Worker::~Worker() { }
// protected methods
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 methods
void Waiter::Set()
{
cout << "Enter waiter's name: ";
Worker::Get();
Get();
}
void Waiter::Show() const
{
cout << "Category: waiter\n";
Worker::Data();
Data();
}
// protected methods
void Waiter::Data() const
{
cout << "Panache rating: " << panache << endl;
}
void Waiter::Get()
{
cout << "Enter waiter's panache rating: ";
cin >> panache;
while (cin.get() != '\n')
continue;
}
// Singer methods
char * Singer::pv[Singer::Vtypes] = {"other", "alto", "contralto",
"soprano", "bass", "baritone", "tenor"};
void Singer::Set()
{
cout << "Enter singer's name: ";
Worker::Get();
Get();
}
void Singer::Show() const
{
cout << "Category: singer\n";
Worker::Data();
Data();
}
// protected methods
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 << '\n';
while (cin >> voice && (voice < 0 || voice >= Vtypes) )
cout << "Please enter a value >= 0 and < " << Vtypes << endl;
while (cin.get() != '\n')
continue;
}
// SingingWaiter methods
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 << "Category: singing waiter\n";
Worker::Data();
Data();
}
// workmi.cpp -- multiple inheritance
// compile with workermi.cpp
#include <iostream>
#include <cstring>
#include "queuetp.h"
#include "workermi.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";
// cin.get();
// cin.get();
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"person.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 person's firstname:";
getline(cin, firstname);
cout << "Enter person'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::Set()
{
Person::Get();
Get();
cout << endl;
}
void BadDude::Get()
{
Gunslinger::Get();
PokerPlayer::Get();
}
void BadDude::Show() const
{
Person::Data();
Data();
}
#include <iostream>
#include <cstring>
#include "person.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 staff:\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";
// cin.get();
// cin.get();
return 0;
}
5.
//书中的头文件程序
#ifndef EMP_H_
#define EMP_H_
#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();
};
#endif
//类的实现
#include<iostream>
#include<string>
#include"emp.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<<(
std::ostream & os, const abstr_emp & e)
{
os << e.fname << " " << e.lname;
return os;
}
employee::employee(const string & fn, const string & ln,
const string & 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 "emp.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;
}
推荐阅读
-
C++ Primer Plus(第六版) 第十四章课后编程答案
-
《C++ Primer Plus 第六版》学习笔记:第十一章 使用类
-
C++ Primer Plus 第十三章答案 类继承
-
C++ Primer Plus(第六版) 第三章 编程练习
-
C++ Primer Plus(第六版) 第四章 编程练习
-
C++ Primer Plus(第六版) 第五章 编程练习
-
c++ primer(第五版)学习笔记及习题答案代码版(第十四章)重载运算与类型转换
-
C Primer Plus (第六版) 第十二章_编程练习答案
-
C++ Primer Plus(第六版)摘选一些习题及答案分享
-
c++ primer(第五版)学习笔记及习题答案代码版(第十四章)重载运算与类型转换