C++ Primer Plus 第十四章 课后编程练习题1-5

//1题///////////////////////////////////////////////////////////////////////////////////////////
类声明

#ifndef wine_hpp
#define wine_hpp
#include
#include
#include
template <class T1, class T2>
class Pair
{
private:
T1 a;
T2 b;
public:
T1 & first();
T2 & second();
T1 first() const {return a; }
T2 second() const { return b; }
Pair(const T1 & aval, const T2 & bval)
:a(aval), b(bval) {}
void Set(const T1 & aval, const T2 & bval);
void Show()const;
~Pair(){}
};

template<class T1, class T2>
T1 & Pair<T1, T2>::first()
{
return a;
}

template<class T1, class T2>
T2 & Pair<T1, T2>::second()
{
return b;
}

template<class T1, class T2>
void Pair<T1, T2>::Set(const T1 & aval, const T2 & bval)
{
a = aval;
b = bval;
}

template<class T1, class T2>
void Pair<T1, T2>::Show()const
{
std::cout<<"\t\tYear " << ‘\t’ <<“Bottles\n”;
for (int i = 0; i < a.size(); i++)
{
std::cout <<’\t’<<’\t’<< a[i] <<’\t’ << b[i] << std::endl;
}
}
typedef std::valarray ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;

class Wine
{
private:
PairArray pa;
std::string name; //存储葡萄酒名
int years; //存储几种年数
public:
Wine(const char * l , const int y, const int ye[], const int bot[]);
Wine(const char * l, int y);
void GetBottles();
const std::string & Label()const;
int sum()const;
void Show()const;
~Wine(){}
};

#endif /* wine_hpp */

类函数实现/////////
#include “wine.hpp”
Wine::Wine(const char * l, const int y, const int ye[], const int bot[])
{
std::valarray a(ye, y);
std::valarray b(bot, y);
name = l;
years = y;
pa.Set(a, b);
}

Wine::Wine(const char * l, const int y)
{
std::valarray a(y);
std::valarray b(y);
pa.Set(a, b);
name = l;
years = y;
}

void Wine::GetBottles()
{
std::valarray a(years);
std::valarray b(years);
std::cout <<“Enter " << name <<” data for " << years <<" year(s):\n";
for (int i = 0; i < years; i++)
{
std::cout <<"Enter years: ";
std::cin >> a[i];
std::cout <<"Enter bottles for that year: ";
std::cin >> b[i];
}
pa.Set(a, b);
}
const std::string & Wine::Label()const
{
return name;
}
int Wine::sum()const
{
std::valarray a(years);
a = pa.second();
int totl = 0;
totl = a.sum();

return totl;

}
void Wine::Show()const
{
std::cout<<"Wine: " << name << std::endl;
pa.Show();
}

//2题//////////////
类定义
#ifndef wine_hpp
#define wine_hpp
#include
#include
#include
template <class T1, class T2>
class Pair
{
private:
T1 a;
T2 b;
public:
T1 & first();
T2 & second();
T1 first() const {return a; }
T2 second() const { return b; }
Pair(const T1 & aval, const T2 & bval)
:a(aval), b(bval) {}
void Set(const T1 & aval, const T2 & bval);
virtual void Show()const;
virtual ~Pair(){}
};

template<class T1, class T2>
T1 & Pair<T1, T2>::first()
{
return a;
}

template<class T1, class T2>
T2 & Pair<T1, T2>::second()
{
return b;
}

template<class T1, class T2>
void Pair<T1, T2>::Set(const T1 & aval, const T2 & bval)
{
a = aval;
b = bval;
}

template<class T1, class T2>
void Pair<T1, T2>::Show()const
{
std::cout<<"\t\tYear " << ‘\t’ <<“Bottles\n”;
for (int i = 0; i < a.size(); i++)
{
std::cout <<’\t’<<’\t’<< a[i] <<’\t’ << b[i] << std::endl;
}
}
typedef std::valarray ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;

class Wine :private PairArray
{
private:
std::string name; //存储葡萄酒名
int years; //存储几种年数
public:
Wine(const char * l , const int y, const int ye[], const int bot[]);
Wine(const char * l, int y);
void GetBottles();
const std::string & Label()const;
int sum()const;
void Show()const;
~Wine(){}
};

类函数定义/////////
#include “wine.hpp”
Wine::Wine(const char * l, const int y, const int ye[], const int bot[])
:PairArray(ArrayInt(ye, y), ArrayInt(bot, y))
{
name = l;
years = y;
}

Wine::Wine(const char * l, const int y)
:PairArray(ArrayInt(y), ArrayInt(y))
{
name = l;
years = y;
}

void Wine::GetBottles()
{
std::valarray a(years);
std::valarray b(years);
std::cout <<“Enter " << name <<” data for " << years <<" year(s):\n";
for (int i = 0; i < years; i++)
{
std::cout <<"Enter years: ";
std::cin >> a[i];
std::cout <<"Enter bottles for that year: ";
std::cin >> b[i];
}
PairArray::Set(a, b);
}
const std::string & Wine::Label()const
{
return name;
}
int Wine::sum()const
{
std::valarray a(years);
a = PairArray::second();
int totl = 0;
totl = a.sum();

return totl;

}
void Wine::Show()const
{
std::cout<<"Wine: " << name << std::endl;
PairArray::Show();
}

//3题//////////////////////////////////////////////////////////////////////////////////////////
//类定义
#ifndef queutp_hpp
#define queutp_hpp
#include
#include
template
class QueueTp
{
private:
enum { MAX = 10}; //队列最长多少
typedef struct node
{
Tp item; //队列首元素地址
node * next; //指向下一个元素的指针
}Node;
Node * front; //队列首指针
Node * rear; //队列尾指针
int top; //现在队列的长度
const int qsize = MAX; //队列最长多少
public:
QueueTp();
QueueTp(const QueueTp & qt); //复制函数
bool isfull()const; //是否为满
bool isempty()const; //是否为空
bool addto(const Tp & it); //添加项
int itemcount()const; //确定队列中的项数
bool deletequeue(); //从队列的开头删除项
void empty(); //清空队列
QueueTp & operator=(QueueTp & qt); //赋值运算符
void Pshow()const; //模版为指针类型的显示函数
void Show()const; //模版非指针类型的显示函数
~QueueTp(); //析构函数
};

template
QueueTp::QueueTp()
{
front = rear = nullptr;
top = 0;
}

template
QueueTp::QueueTp(const QueueTp & qt)
{
Node * temp;
temp = qt.front;
if (front != nullptr)
~QueueTp();
front = new Node;
rear = front;
rear->item = temp->item;
temp = temp->next;
rear->next = nullptr;
for (int i = 0; i < top - 1; i++)
{
rear->next = new Node;
rear = rear->next;
rear->item = temp->item;
rear->next = nullptr;
}
}

template
bool QueueTp::isfull()const
{
return top == qsize;
}

template
bool QueueTp::isempty()const
{
return top == 0;
}

template
bool QueueTp::addto(const Tp & it)
{
if (top == 0)
{
front = new Node;
front->item = it;
front->next = nullptr;
rear = front;
++top;
return true;
}
else if(top <= qsize)
{
rear->next = new Node;
rear = rear->next;
rear->item = it;
rear->next = nullptr;
++top;
return true;
}
else
std::cout <<“Is full!\n”;
return false;
}

template
int QueueTp::itemcount()const
{
return top;
}

template
bool QueueTp::deletequeue()
{
Node * temp;
if (top > 0)
{
temp = front;
front = front->next;
delete temp;
–top;
return true;
}
else
std::cout <<“Is empty!\n”;
return false;
}

template
void QueueTp::empty()
{
~QueueTp();
}

template
QueueTp & QueueTp::operator=(QueueTp & qt)
{
if (this == &qt)
return *this;
else
{
Node * temp;
temp = qt.front;
~QueueTp();
front = new Node;
rear = front;
rear->item = temp->item;
temp = temp->next;
rear->next = nullptr;
for (int i = 0; i < top - 1; i++)
{
rear->next = new Node;
rear = rear->next;
rear->item = temp->item;
rear->next = nullptr;
}
}
return *this;
}

template
QueueTp::~QueueTp()
{
Node * temp;
for (int i = 0; i < top; i++)
{
temp = front;
front = front->next;
delete temp;
}
front = rear = nullptr;
top = 0;
}

template
void QueueTp::Show()const
{
Node * temp;
temp = front;
for (int i = 0; i < top; i++)
{
std::cout << i+1 <<": " << temp->item << std::endl;
temp = temp->next;
}
}

template
void QueueTp::Pshow()const
{
Node * temp;
temp = front;
for (int i = 0; i < top; i++)
{
std::cout << i+1 <<": " << *temp->item << std::endl;
temp = temp->next;
}
}
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) {}
~Worker() {};
void Set();
void Show() const;
friend std::ostream & operator<<(std::ostream & os,const Worker & wk);
};

#endif /* queutp_hpp */

//Worker类函数定义
#include “queutp.hpp”
#include
#include
#include
using std::cout;
using std::endl;
using std::cin;
void Worker::Data() const
{
cout <<"Name: " << fullname << endl;
cout <<"ID: " << id << endl;
}

void Worker::Get()
{
getline(cin, fullname);
cout <<"Enter worker’s ID: ";
cin >> id;
while (cin.get() != ‘\n’)
continue;
}

std::ostream & operator<<(std::ostream & os,const Worker & wk)
{
os <<"Name: " << wk.fullname << endl;
os <<"ID: " << wk.id << endl;
return os;
}

void Worker::Set()
{
cout <<"Enter waiter’s name: ";
Worker::Get();
}

//main()
#include
#include
#include “queutp.hpp”

const int MAX = 5;
int main()
{
using std::cin;
using std::cout;
using std::endl;

Worker * lolas[MAX];

int ct;
for (ct = 0; ct < MAX; ct++)
{
    lolas[ct] = new Worker;
    lolas[ct]->Set();
}
QueueTp<Worker *> qw;
for (ct = 0; ct < MAX; ct++)
    qw.addto(lolas[ct]);
qw.Pshow();
qw.deletequeue();
qw.Pshow();
for (ct = 0; ct < MAX; ct++)
{
    delete lolas[ct];
}
return 0;

}

//4题////////////////////////////////////////////////////////////////////////////////////////
//类定义
#ifndef person_hpp
#define person_hpp
#include
#include
class Person
{
private:
std::string fname;
std::string lname;
protected:
void Data() const;
void Get();
public:
Person()
: fname(“no one”), lname(“no one”){}
Person(std::string & f, std::string & l)
:fname(f), lname(l) {}
virtual void Set() = 0;
virtual void Show() const = 0;
virtual ~Person() {};
};

class Gunslinger :virtual public Person
{
private:
int notch; //刻痕
double time;
protected:
void Data()const;
void Get1();
void Get2();
public:
Gunslinger()
: Person(), notch(0), time(0.0) {}
Gunslinger(std::string & fn, std::string & ln, int n = 0, double t = 0.0)
: Person(fn, ln), notch(n),time(t) {}
Gunslinger(Person & p, int n = 0, double t = 0.0)
: Person§, notch(n) {}
void Set();
double Draw() { return time;}; //返回枪手拔枪时间
void Show()const;
};

class PokerPlayer :virtual public Person
{
public:
PokerPlayer()
: Person() {}

PokerPlayer(std::string & fn, std::string & ln)
:Person(fn, ln) {}
void Set();
int Draw();
void Show() const;

};

class BadDude :public PokerPlayer, public Gunslinger
{
protected:
void Data()const;
void Get1();
void Get2();
public:
BadDude()
:Gunslinger() {}
BadDude(std::string & fn, std::string & ln, int n = 0, double d = 0.0)
:Gunslinger(fn, ln, n, d) {}
double Gdraw(){ return Gunslinger::Draw();};
void Set();
int Cdraw() { return PokerPlayer::Draw();}
void Show()const;
};
#endif /* person_hpp */

//类函数定义
#include “person.hpp”
#include

//Person protected
void Person::Data() const
{
std::cout <<"Fastname: " << fname << std::endl;
std::cout <<"Lastname: " << lname << std::endl;
}

void Person::Get()
{
getline(std::cin, fname);
std::cout <<"Enter lastname: ";
getline(std::cin, lname);
}
//Card
//protected

//Gunslinger
//protected
void Gunslinger::Data()const
{
std::cout <<"Notch: " << notch << std::endl;
std::cout <<"Shooting time: "
<< time << std::endl;
}

void Gunslinger::Get1()
{
std::cin >> notch;
while (std::cin.get() != ‘\n’)
continue;
}

void Gunslinger::Get2()
{
std::cin >> time;
while (std::cin.get() != ‘\n’)
continue;
}
//Gunslinger public

void Gunslinger::Set()
{
std::cout <<"Enter Gunslinger’s fastname: ";
Person::Get();
std::cout <<"Enter Gunslinger’s notch: ";
Get1();
std::cout <<“Gunslinger Shooting time: “;
Get2();
}
void Gunslinger::Show()const
{
std::cout <<”\nCategory: Gunslinger\n”;
Person::Data();
Data();
}

//PokerPlayer public
void PokerPlayer::Show() const
{
std::cout <<"\nCategory: PokerPlayer\n";
Person::Data();
}

int PokerPlayer::Draw()
{
std::srand((unsigned int) time(0));
int t;
t = std::rand() % 54 + 1;
return t;
}

void PokerPlayer::Set()
{
std::cout <<"Enter PokerPlayer’s fastname: ";
Person::Get();
}

//BadDude
//protected
void BadDude::Data()const
{
Gunslinger::Data();
}
void BadDude::Get1()
{
Gunslinger::Get1();
}

void BadDude::Get2()
{
Gunslinger::Get2();
}
void BadDude::Set()
{
std::cout <<"Enter BadDude’s fastname: ";
Person::Get();
std::cout <<"BadDude notch: ";
Get1();
std::cout <<"BadDude Shooting time: ";
Get2();
}

void BadDude::Show()const
{
std::cout <<"\nCategory: BadDude\n";
Person::Data();
Data();
}

//实验主函数
#include
#include
#include “person.hpp”
const int SIZE = 5;

int main()
{
using std::cout;
using std::cin;
using std::endl;
using std::strchr;

Person * lolas[SIZE];
int ct;
for (ct = 0; ct < SIZE; ct++)
{
    char choice;
    cout <<"\nEnter the employee category:\n"
    <<"p: PokerPlayer  g: Gunslinger  \n"
    <<"b: Bad Dude  q: quit\n";
    cin >> choice;
    while (strchr("pgbq", choice) == nullptr)
    {
        cout <<"\nPlease enter p g b or q: ";
        cin >> choice;
    }
    if (choice == 'q')
        break;
    switch(choice)
    {
        case 'p':  lolas[ct] = new PokerPlayer;
            break;
        case 'g':  lolas[ct] = new Gunslinger;
            break;
        case 'b':  lolas[ct] = new BadDude;
            break;
    }
    while (cin.get() != '\n')
               continue;
    lolas[ct]->Set();
}

cout <<"\nThis is a message for Pocker Player, Gunslinger and BadDude:\n";
int i;
for (i = 0; i < ct; i++)
{
    cout << endl;
    lolas[i]->Show();
}
PokerPlayer cd;
int k;
k = cd.Draw();
cout << k;
for (i = 0; i < ct; i++)
    delete lolas[i];
cout <<"Bye.\n";
return 0;

}

//5///////////////////////////////////////////////////////////////////////////////////////////
//类定义
#ifndef emp_hpp
#define emp_hpp
#include
#include

class abstr_emp
{
private:
std::string fname;
std::string lname;
std::string job;
public:
abstr_emp(): fname(), lname(), job() {}
abstr_emp(const std::string & fn, const std::string & ln,
const std::string & j)
:fname(fn), lname(ln), job(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();
employee(const std::string & fn, const std::string ln,
const std::string & j)
:abstr_emp(fn, ln, 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()
:abstr_emp(), inchargeof(0) {}
manager(const std::string & fn, const std::string & ln,
const std::string & j, int ico = 0)
:abstr_emp(fn, ln, j), inchargeof(ico) {}
manager(const abstr_emp & e, int ico)
:abstr_emp(e), inchargeof(ico) {}
manager(const manager & m)
:abstr_emp((abstr_emp &) m), inchargeof(m.inchargeof) {}
virtual void ShowAll() const;
virtual void SetAll();
};

class fink: virtual public abstr_emp
{
private:
std::string reportsto;
protected:
const std::string ReportsTo() const { return reportsto; }
std::string & ReportsTo() { return reportsto; }
public:
fink()
:abstr_emp(), reportsto() {}
fink(const std::string & fn, const std::string & ln,
const std::string & j, const std::string & rpo)
:abstr_emp(fn, ln, j), reportsto(rpo){}
fink(const abstr_emp & e, const std::string &rpo)
:abstr_emp(e), reportsto(rpo) {}
fink(const fink & e)
:abstr_emp((abstr_emp &) e), reportsto(e.reportsto) {}
virtual void ShowAll() const;
virtual void SetAll();
};

class highfink: public manager, public fink
{
public:
highfink()
:abstr_emp(), manager(), fink() {}
highfink(const std::string & fn, const std::string & ln,
const std::string & j, const std::string & rpo,
int ico)
:abstr_emp(fn, ln, j), manager(fn, ln, j, ico), fink(fn, ln, j, rpo) {}
highfink(const abstr_emp & e, const std::string & rpo, int ico)
:abstr_emp(e), manager(e, ico), fink(e, rpo){}
highfink(const fink & f, int ico)
:abstr_emp(f), manager(f, ico), fink(f){}
highfink(const manager & m, const std::string rpo)
:abstr_emp(m), manager(m), fink(m, rpo){}
highfink(const highfink & h)
:abstr_emp(h), manager(h), fink(h) {}
virtual void ShowAll()const;
virtual void SetAll();
};
#endif /* emp_hpp */

//类函数定义
//为什么没有定义赋值运算符? 没有用到, 没有使用动态new分配内存没有必要。
//为什么要将showall, setall,定义为虚的? 可以再类指针数组中根据指针或引用指向
//的类类型自动调用相应的showall, setall 函数
//为什么highfink类没有数据部分,派生类可以用于修改基类数据,他的数据都存储在基类中
//为什么只有一个operator<<()版本? 为了通用,这一个版本是包括基类在内的所有派生类共同使用。
//其他派生类的数据结构互不相同。
//如果替换成下面代码替换程序的结尾部分, 会发生什么?
//原来的showall() 是根据指针或引用指向的类类型调用相应的showall()
//替换后 只会调用abstr_emp,也就是数组声明时类型的showall()。

#include “emp.hpp”
#include
//abstr_emp
abstr_emp::~abstr_emp()
{
}

void abstr_emp::ShowAll() const
{
std::cout <<“Fname: " << fname ;
std::cout <<”\nLname: " << lname ;
std::cout <<"\nJob: " << job << std::endl;
}

void abstr_emp::SetAll()
{
std::cout <<"Fname: ";
getline(std::cin, fname);
std::cout <<“Lname: “;
getline(std::cin, lname);
std::cout <<“Job: “;
getline(std::cin, job);
}
std::ostream & operator<<(std::ostream & os, const abstr_emp & e)
{
os << e.fname <<”\n”<< e.lname <<”\n”<< e.job << std::endl;
return os;
}

//employee
void employee::ShowAll()const
{
std::cout << std::endl;
abstr_emp::ShowAll();
}

void employee::SetAll()
{
abstr_emp::SetAll();
}

//manager
void manager::ShowAll()const
{
std::cout << std::endl;
abstr_emp::ShowAll();
std::cout <<"Inchargeof: "
<< inchargeof << std::endl;
}

void manager::SetAll()
{
abstr_emp::SetAll();
std::cout <<"Inchargeof: ";
std::cin >> inchargeof;
std::cin.get();
}

//fink
void fink::ShowAll()const
{
std::cout << std::endl;
abstr_emp::ShowAll();
std::cout <<"Reportsto: "
<< reportsto << std::endl;
}

void fink::SetAll()
{
abstr_emp::SetAll();
std::cout <<"Reportsto: ";
getline(std::cin, reportsto);
}

//highfink
void highfink::ShowAll()const
{
std::cout << std::endl;
abstr_emp::ShowAll();
std::cout <<"Inchargeof: "
<< manager::InChargeOf() << std::endl;
std::cout <<"Reportsto: "
<< fink::ReportsTo() << std::endl;
}

void highfink::SetAll()
{
abstr_emp::SetAll();
std::cout <<"Reportsto: ";
getline(std::cin, fink::ReportsTo());
std::cout <<"Inchargeof: ";
std::cin >> manager::InChargeOf();
std::cin.get();
}

发布了85 篇原创文章 · 获赞 1 · 访问量 1889

猜你喜欢

转载自blog.csdn.net/Tekkenwxp/article/details/105004886