C++ Primer Plus第十四章练习

14.7.1
Wine.h

#ifndef WINE_H
#define WINE_H
#include <iostream>
#include <valarray>
#include <string>
using namespace std;
template <typename T1, typename T2>
class Pair
{
private:
    T1 a;
    T2 b;
public:
    Pair(void) {}
    int Sum(void) const;
    void Set(T1, T2);
    void Show(void) const;
};
class Wine
{
private:
    typedef valarray<int> ArrayInt;
    typedef Pair<ArrayInt, ArrayInt> PairArray;
    string name;
    PairArray data;
    int num;
public:
    Wine(const char * l, int y);
    Wine(const char * l, int y, const int yr[], const int bot[]);
    void Show(void) const;
    void GetBottles(void);
    const string & Label(void) const;
    int sum(void) const;
};
#endif

Wine.cpp

#include "Wine.h"
template <typename T1, typename T2>
int Pair<T1, T2>::Sum(void) const
{
    return b.sum();
}
template <typename T1, typename T2>
void Pair<T1, T2>::Set(T1 yr, T2 bt)
{
    a = yr;
    b = bt;
}
template <typename T1, typename T2>
void Pair<T1, T2>::Show(void) const
{
    cout << "Year" << '\t' << "Bottles" << endl;
    for(int i = 0; i < a.size(); i++)
        cout << a[i] << '\t' << b[i] << endl;
}
Wine::Wine(const char * l, int y)
{
    name = l;
    num = y;
}
Wine::Wine(const char * l, int y, const int yr[], const int bot[])
{
    name = l;
    num = y;
    data.Set(ArrayInt(yr, num), ArrayInt(bot, num));
}
void Wine::Show(void) const
{
    cout << "Wine: " << name << endl;
    data.Show();
}
void Wine::GetBottles(void)
{
    ArrayInt a1(num), a2(num);
    for(int i = 0; i < num; i++)
    {
        cout << "Enter year: ";
        cin >> a1[i];
        cout << "Enter bottles for that year: ";
        cin >> a2[i];
    }
    data.Set(a1, a2);
}
const string & Wine::Label(void) const
{
    return name;
}
int Wine::sum(void) const
{
    return data.Sum();
}

main.cpp

#include<iostream>
#include"Wine.h"
int main()
{
	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";
	system("pause");
	return 0;
}

14.7.2
Wine.h

#ifndef WINE_H
#define WINE_H
#include <iostream>
#include <valarray>
#include <string>
using namespace std;
template <typename T1, typename T2>
class Pair
{
private:
    T1 a;
    T2 b;
public:
    Pair(void) {}
    int Sum(void) const;
    void Set(T1, T2);
    void Show(void) const;
};
typedef valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
class Wine : private string, private PairArray
{
private:
    int num;
public:
    Wine(const char * l, int y);
    Wine(const char * l, int y, const int yr[], const int bot[]);
    void Show(void) const;
    void GetBottles(void);
    const string & Label(void) const;
    int sum(void) const;
};
#endif

Wine.cpp

#include "Wine.h"
template <typename T1, typename T2>
int Pair<T1, T2>::Sum(void) const
{
    return b.sum();
}
template <typename T1, typename T2>
void Pair<T1, T2>::Set(T1 yr, T2 bt)
{
    a = yr;
    b = bt;
}
template <typename T1, typename T2>
void Pair<T1, T2>::Show(void) const
{
    cout << "Year" << '\t' << "Bottles" << endl;
    for(int i = 0; i < a.size(); i++)
        cout << a[i] << '\t' << b[i] << endl;
}
Wine::Wine(const char * l, int y) : string(l)
{
    num = y;
}
Wine::Wine(const char * l, int y, const int yr[], const int bot[]) : string(l)
{
    num = y;
    PairArray::Set(ArrayInt(yr, num), ArrayInt(bot, num));
}
void Wine::Show(void) const
{
    cout << "Wine: " << (const string &)*this << endl;
    PairArray::Show();
}
void Wine::GetBottles(void)
{
    ArrayInt a1(num), a2(num);
    for(int i = 0; i < num; i++)
    {
        cout << "Enter year: ";
        cin >> a1[i];
        cout << "Enter bottles for that year: ";
        cin >> a2[i];
    }
    PairArray::Set(a1, a2);
}
const string & Wine::Label(void) const
{
    return (const string &)*this;
}
int Wine::sum(void) const
{
    return PairArray::Sum();
}

14.7.3
QueueTp.h

#ifndef QUEUETP_H
#define QUEUETP_H
#include <iostream>
#include <string>
using namespace std;
template <typename T>
class QueueTp
{
private:
    struct Node
    {
        T item;
        Node * n;
    };
    Node * f;
    Node * l;
    int present;
    cosnt int size;
public:
    QueueTp(int s = 10);
    ~QueueTp(void);
    bool isempty(void) const;
	bool isfull(void) const;
	int count(void) const;
	bool enqueue(const T &);
	bool dequeue(T &);
};
class Worker
{
private:
	string name;
	int id;
public:
	Worker(void) : name("none"), id(0) {}
	Worker(const string & s, int n) : name(s), id(n) {}
	~Worker(void) {}
	void set(void);
	void show(void) const;
};
#endif

QueueTp.cpp

#include "QueueTp.h"
template <typename T>
QueueTp<T>::QueueTp(int s) : size(s)
{
    f = l = nullptr;
    present = 0;
}
template <typename T>
QueueTp<T>::~QueueTp(void)
{
    delete [] f;
    l = NULL;
}
template <typename T>
bool QueueTp<T>::isempty(void) const
{
    return present == 0;
}
template <typename T>
bool QueueTp<T>::isfull(void) const
{
    return present == size;
}
template <typename T>
int QueueTp<T>::count(void) const
{
    return present;
}
template <typename T>
bool QueueTp<T>::enqueue(const T & w)
{
    if(isfull())
        return false;
    T * temp = new Node;
    temp->item = w;
    temp->n = NULL;
    if(present == 0)
    {
        l = temp;
        f = temp;
    }
    l->n = temp;
    l = l->n;
    present++;
    return true;
}
template <typename T>
bool QueueTp<T>::dequeue(T & w)
{
    if(isempty())
        return false;
    w = f->item;
    T * temp = f;
    f = f->n;
    delete temp;
    present--;
    if(present == 0)
        l = nullptr;
    return true;
}
void Worker::set(void)
{
    cout << "Enter name: ";
    cin >> name;
    cout << "Enter id: ";
    cin >> id;
}
void Worker::show(void) const
{
    cout << "name: " << name << " id: " << id << endl;
}

14.7.4
本来写完了感觉不太对 去借鉴了一下声明重新写了定义
https://blog.csdn.net/weixin_41413441/article/details/79753178
Person.h

#ifndef PERSON_H
#define PERSON_H
#include <string>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class person//总结了下 get方法是修改当前类型的对象的数据成员 set方法是修改继承到的数据成员与当前类型的数据成员 data与show也是如此
{
private:
	string fname;
	string lname;
protected:
	void data(void) const;
	void get(void);
public:
	person(const char * s, const char * t) : fname(s), lname(t) {}
	person(void) : fname("none"), lname("none") {}
	person(const person & p) : fname(p.fname), lname(p.lname) {}
	virtual void show(void) const = 0;
	virtual void set(void) = 0;
};
class Gunslinger : virtual public person
{
private:
	int count;
protected:
    void data(void) const;
    void get(void);
public:
	Gunslinger(void) : person(), count(0) {}
	Gunslinger(const char * f, const char * l, int c = 0) : person(f, l), count(0) {}
	Gunslinger(const person & pr, int c = 0) : person(pr), count(c) {}
	void set(void);
	void show(void) const;
	double Draw(void) const;
};
class pokerplayer : virtual public person
{
protected:
	void data(void) const;
public:
	pokerplayer(void) : person() {}
	pokerplayer(const char * s, const char * t) : person(s, t) {}
	pokerplayer(const person & pr) : person(pr) {}
	void set(void);
	void show(void) const;
	int Draw(void) const;
};
class baddude : public pokerplayer, public Gunslinger
{
protected:
	void data(void) const;
	void get(void);
public:
	baddude(void) : person(), Gunslinger(), pokerplayer() {}
	baddude(const char * f, const char * l,int c) : person(f, l), Gunslinger(f, l, c), pokerplayer(f, l) {}
	baddude(int c, const person & p) : person(p), Gunslinger(p, c), pokerplayer(p) {}
	void set(void);
	void show(void) const;
	double Gdraw(void) const;
	int Cdraw(void) const;
};
#endif

Person.cpp

#include "Person.h"
void person::data(void) const
{
    cout << "fname: " << fname << endl;
    cout << "lname: " << lname << endl;
}
void person::get(void)
{
    cout << "Enter fname: ";
    getline(cin, fname);
    cout << "Enter lname: ";
    getline(cin, lname);
}
void person::show(void) const
{
    data();
}
void person::set(void)
{
    get();
}

void Gunslinger::data(void) const
{
    cout << "count: " << count << endl;
    cout << "Time: " << Draw() << endl;
}
void Gunslinger::get(void)
{
    cout << "Enter count: ";
    cin >> count;
    while (cin.get() != '\n')
		continue;
}
void Gunslinger::set(void)
{
    person::get();
    get();
}
void Gunslinger::show(void) const
{
    person::data();
    data();
}
double Gunslinger::Draw(void) const
{
    return rand() % 3 + 1;
}

void pokerplayer::data(void) const
{
    cout << "点数为: " << Draw() << endl;
}
void pokerplayer::set(void)
{
    person::get();
}
void pokerplayer::show(void) const
{
    person::data();
    data();
}
int pokerplayer::Draw(void) const
{
    return rand() % 51 + 1;
}

void baddude::set(void)
{
    person::get();
    Gunslinger::get();
}
void baddude::show(void) const
{
    person::data();
    Gunslinger::data();
    pokerplayer::data();
}
double baddude::Gdraw(void) const
{
    return Gunslinger::Draw();
}
int baddude::Cdraw(void) const
{
    return pokerplayer::Draw();
}

main.cpp
测试程序直接照搬了

#include "Person.h"
int main(void)
{
    const int Size = 5;
    person* sit[Size];
	int ct ;
	for (ct = 0; ct < Size; ct++)
	{
		char choice;
		cout << "enter the person: \n"
		<< "g:Gunslinger  p:pokerplayer " << "b:baddude  q:quit\n";
		cin >> choice;
		while (strchr("gpbq", choice) == NULL)
		{
			cout << "please enter a g、p、b、q:";
			cin >> choice;
		}
		if (choice == 'q')
			break;
		switch (choice)
		{
		case'g':sit[ct] = new Gunslinger;
			break;
		case'p':sit[ct] = new pokerplayer;
			break;
		case'b':sit[ct] = new baddude;
			break;
		}
		cin.get();
		sit[ct]->set();
	}
	cout << "\nhere is your staff:\n";
	int i;
	for (i = 0; i < ct; i++)
	{
		cout << endl;
		sit[ct]->show();
	}
	for (i = 0; i < ct; i++)
	{
		delete sit[ct];
	}
	cout << "bye:\n";
	system("pause");
	return 0;
}

14.7.5
emp.h

#ifndef EMP_H
#define EMP_H
#include <iostream>
#include <string>
using namespace std;
class abstr_emp
{
private:
	std::string fname;
	std::string lname;
	std::string job;
public:
	abstr_emp();
	abstr_emp(const std::string& fn, const std::string& ln, const std::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();
	employee(const std::string& fn, const std::string& ln, const std::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 std::string& fn, const std::string& ln, const std::string& j, int ico = 0);
	manager(const abstr_emp&e, int ico = 0);
	manager(const manager&m);
	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();
	fink(const std::string& fn, const std::string& ln, const std::string& j,const std::string& rpo);
	fink(const abstr_emp& e, const std::string rpo);
	fink(const fink&e);
	virtual void Showall()const;
	virtual void Setall();
};
class highfink: public manager,public fink
{
public:
	highfink();
	highfink(const std::string& fn, const std::string& ln, const std::string& j, std::string& rpo, int ico=0);
	highfink(const abstr_emp&e, const std::string & rpo, int ico);
	highfink(const fink&f, int ico);
	highfink(const manager&m, const std::string&rpo);
	highfink(const highfink&h);
	virtual void Showall()const;
	virtual void Setall();
};
#endif

emp.cpp

#include "emp.h"
abstr_emp::abstr_emp() : fname("none"), lname("none"), job("none") {}
abstr_emp::abstr_emp(const std::string& fn, const std::string& ln, const std::string& j) : fname(fn), lname(ln), job(j) {}
void abstr_emp::Showall()const
{
    cout << "fname: " << fname << endl;
    cout << "lname: " << lname << endl;
    cout << "job: " << job << endl;
}
void abstr_emp::Setall()
{
    cout << "Enter fname: ";
    getline(cin, fname);
    cout << "Enter lname: ";
    getline(cin, lname);
    cout << "Enter job: ";
    getline(cin, job);
}
ostream&operator<<(ostream&os, const abstr_emp&e)
{
    os << "fname: " << e.fname << endl;
    os << "lname: " << e.lname << endl;
    os << "job: " << e.job << endl;
    return os;
}
abstr_emp::~abstr_emp() {}

employee::employee() {}
employee::employee(const std::string& fn, const std::string& ln, const std::string& j) : abstr_emp(fn, ln, j) {}
void employee::Showall()const
{
    abstr_emp::Showall();
}
void employee::Setall()
{
    abstr_emp::Setall();
}

manager::manager() : inchargeof(0) {}
manager::manager(const std::string& fn, const std::string& ln, const std::string& j, int ico) : inchargeof(ico), abstr_emp(fn, ln, j) {}
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;
}
void manager::Setall()
{
    abstr_emp::Setall();
    cout << "Enter inchargeof: ";
    cin >> inchargeof;
    while(cin.get() != '\n')
        continue;
}

fink::fink() : reportsto("none") {}
fink::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::fink(const abstr_emp& e, const std::string rpo) : abstr_emp(e), reportsto(rpo) {}
fink::fink(const fink&e) : abstr_emp(e), reportsto(e.reportsto) {}
void fink::Showall()const
{
    abstr_emp::Showall();
    cout << "reportsto: " << reportsto << endl;
}
void fink::Setall()
{
    abstr_emp::Setall();
    cout << "Enter reportsto: ";
    getline(cin, reportsto);
}

highfink::highfink() {}
highfink::highfink(const std::string& fn, const std::string& ln, const std::string& j, std::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 std::string & rpo, int ico) : abstr_emp(e), fink(e, rpo), manager(e, ico) {}
highfink::highfink(const fink&f, int ico) : abstr_emp(f), manager(f, ico), fink(f) {}
highfink::highfink(const manager&m, const std::string&rpo) : abstr_emp(m), manager(m), fink(m, rpo) {}
highfink::highfink(const highfink&h) : abstr_emp(h), manager(h), fink(h) {}
void highfink::Showall()const
{
	manager::Showall();
	fink::Showall();
}
void highfink::Setall()
{
    manager::Setall();
	fink::Setall();
}

main.cpp

#include"emp.h"
int main()
{
	employee em("trip", "harris", "thumper");
	cout << em << endl;
	em.Showall();
	manager ma("amorphia", "sprindragon", "nuancer", 5);
	cout << ma << endl;
	ma.Showall();
	fink fi("oggs","oggs", "olier", "juno barr");
	cout << fi << endl;
	fi.Showall();
	highfink hf(ma, "curly kew");
	hf.Showall();
	cout << "please enter 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();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值