1.
winec.h
#ifndef WINEC_H
#define WINEC_H
#include <iostream>
using namespace std;
#include <valarray>
#include <string>
template <class T1, class T2>
class Pair{
T1 a;
T2 b;
public:
void set(const T1 &yr, const T2 &bot);
Pair(const T1 & aval, const T2 & bval) : a(aval), b(bval) {}
Pair() {}
int Sum() const ;
void Show(int y) const ;
};
template <class T1, class T2>
void Pair<T1, T2>::set(const T1 & yr, const T2 & bot)
{
a = yr;
b = bot;
}
template <class T1, class T2>
int Pair<T1, T2>::Sum() const
{
return b.sum();
}
template <class T1, class T2>
void Pair<T1, T2>::Show(int y) const
{
for(int i=0; i<y; i++)
cout << "\t\t" << a[i] << "\t\t" << b[i] << endl;
}
typedef valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
class Wine{
PairArray b;
string name;
int yrs;
public:
Wine(const char * l, int y, const int yr[], const int bot[]);
Wine(const char * l, int y);
void GetBottles();
string & Label();
int sum();
void Show();
};
#endif
winec.cpp
#include "winec.h"
#include <iostream>
using namespace std;
Wine::Wine(const char *l, int y, const int yr[], const int bot[])
{
yrs = y;
name = l;
b.set(ArrayInt(yr, yrs), ArrayInt(bot, yrs));
}
Wine::Wine(const char *l, int y)
{
yrs = y;
name = l;
}
void Wine::GetBottles()
{
ArrayInt yr(yrs), bot(yrs);
cout << "Enter " << name << " data for " << yrs << " year(s):\n";
for(int i=0; i<yrs; i++)
{
cout << "Enter year: ";
cin >> yr[i];
cout << "Enter bottles for that year: ";
cin >> bot[i];
}
b.set(yr, bot);
}
string & Wine::Label()
{
return name;
}
int Wine::sum()
{
return b.Sum();
}
void Wine::Show()
{
cout << "Wine: " << name << endl;
cout << "\t\tYear\tBottles\n";
b.Show(yrs);
}
play.cpp
//直接按题给
#include "winec.h"
using namespace std;
#include <iostream>
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";
return 0;
}
2.
本题要求在编程练习1的基础上,将包含关系修改为私有继承。那么Pair对象的变量就不需要作为私有变量了,直接继承即可。此时赋值的工作就可以通过初始化列表来完成,而且因为没有变量,需要使用类作用域限定来调用PairArray或者string.
winec.h
#ifndef WINEC_H
#define WINEC_H
#include <iostream>
using namespace std;
#include <valarray>
#include <string>
template <class T1, class T2>
class Pair{
T1 a;
T2 b;
public:
void set(const T1 &yr, const T2 &bot);
Pair(const T1 & aval, const T2 & bval) : a(aval), b(bval) {}
Pair() {}
int Sum() const ;
void Show(int y) const ;
};
template <class T1, class T2>
void Pair<T1, T2>::set(const T1 & yr, const T2 & bot)
{
a = yr;
b = bot;
}
template <class T1, class T2>
int Pair<T1, T2>::Sum() const
{
return b.sum();
}
template <class T1, class T2>
void Pair<T1, T2>::Show(int y) const
{
for(int i=0; i<y; i++)
cout << "\t\t" << a[i] << "\t\t" << b[i] << endl;
}
typedef valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
class Wine : private string , private PairArray //改为了私有继承
{
// PairArray b;
// string name;
int yrs;
public:
Wine(const char * l, int y, const int yr[], const int bot[]);
Wine(const char * l, int y);
void GetBottles();
string & Label();
int sum();
void Show();
};
#endif
winec.cpp
#include "winec.h"
#include <iostream>
using namespace std;
Wine::Wine(const char *l, int y, const int yr[], const int bot[])
: string(l), yrs(y), PairArray(ArrayInt(yr, y), ArrayInt(bot, y))
{}
Wine::Wine(const char *l, int y)
: string(l), yrs(y)
{}
void Wine::GetBottles()
{
ArrayInt yr(yrs), bot(yrs);
cout << "Enter " << (const string &) *this << " data for " << yrs << " year(s):\n";
for(int i=0; i<yrs; i++)
{
cout << "Enter year: ";
cin >> yr[i];
cout << "Enter bottles for that year: ";
cin >> bot[i];
}
PairArray::set(yr, bot);
}
string & Wine::Label()
{
return (string &) *this;
}
int Wine::sum()
{
return PairArray::Sum();
}
void Wine::Show()
{
cout << "Wine: " << (const string &) *this << endl;
cout << "\t\tYear\tBottles\n";
PairArray::Show(yrs);
}
检验文件同上。
3.
QueueTp.h
#ifndef QUEUETP_H_
#define QUEUETP_H_
#include <iostream>
#include <string>
using namespace std;
template <class T>
class QueueTP
{
private:
struct Node
{
T item;
struct Node * next;
};
enum { Q_SIZE = 10 };
Node * front;
Node * rear;
int items;
const int qsize;
QueueTP(const QueueTP & q) : qsize(0) {}
QueueTP & operator=(const QueueTP & q) { return *this; }
public:
QueueTP(int qs = Q_SIZE);
~QueueTP();
bool isempty() const;
bool isfull() const;
int queuecount() const;
bool enqueue(const T & item);
bool dequeue(T & item);
};
#endif
QueueTp.cpp
#include <iostream>
#include <string>
using namespace std;
#include "QueueTp.h"
template <class T>
QueueTP<T>::QueueTP(int qs) : qsize(qs)
{
items = 0;
front = rear = NULL;
}
template <class T>
QueueTP<T>::~QueueTP()
{
Node * temp;
while (front != NULL)
{
temp = front;
front = front -> next;
delete temp;
}
}
template <class T>
bool QueueTP<T>::isempty() const
{
return items == 0;
}
template <class T>
bool QueueTP<T>::isfull() const
{
return items == qsize;
}
template <class T>
int QueueTP<T>::queuecount() const
{
return items;
}
template <class T>
bool QueueTP<T>::enqueue(const T & item)
{
if (isfull())
{
cout << "Queue already full!\n";
return false;
}
Node * newitem = new Node;
newitem->item = item;
newitem->next = NULL;
items++;
if (front == NULL)
{
front = newitem;
}
else
{
rear->next = newitem;
}
rear = newitem;
return true;
}
template <class T>
bool QueueTP<T>::dequeue(T & item)
{
if (isempty())
{
cout << "It's empty now!\n";
return false;
}
else
{
item = front->item;
items--;
Node * temp = front;
front = front->next;
delete temp;
if (items == 0)
{
rear = NULL;
}
}
return true;
}
class Worker
{
private:
string fullname;
long id;
public:
Worker() : fullname("no one"), id(0L) {}
Worker(const string & s, long n) : fullname(s), id(n) {}
virtual ~Worker();
virtual void Set();
virtual void Show() const;
};
void Worker::Set()
{
cout << "Enter worker's name: ";
getline(cin, fullname);
cout << "Enter worker's ID: ";
cin >> id;
while (cin.get() != '\n')
{
continue;
}
}
void Worker::Show() const
{
cout << "Name: " << fullname << endl;
cout << "Employee ID: " << id << endl;
}
Worker::~Worker()
{
}
play.cpp
#include <iostream>
#include <string>
#include "QueueTP.h"
using namespace std;
int main()
{
QueueTP<Worker> * Qworker = new QueueTP<Worker>(20);
Worker w1("Leonardo", 1024);
Worker w2("Elma", 2035);
if (Qworker->isempty())
{
cout << "Qworker is empty!\n";
}
Qworker->enqueue(w1);
Qworker->enqueue(w2);
cout << "Qworker count: " << Qworker->queuecount() << endl;
Worker w3("hello", 2);
Qworker->enqueue(w3);
Qworker->enqueue(w2);
cout << "Qworker count: " << Qworker->queuecount() << endl;
Worker temp;
Qworker->dequeue(temp);
cout << "Dequeue: " << endl;
temp.Show();
Qworker->dequeue(temp);
cout << "Dequeue: " << endl;
temp.Show();
cout << "Qworker count: " << Qworker->queuecount() << endl;
system("pause");
return 0;
}
4.
person.h
#ifndef PERSON_H_
#define PERSON_H_
#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string firstname;
string lastname;
public:
Person() : firstname("None"), lastname("None") {}
Person(string fname, string lname) : firstname(fname), lastname(lname) {}
Person(const Person & p);
virtual ~Person() = 0;
virtual void Show() const {cout << "Firstname: " << firstname << "\nLastname: " << lastname << endl;}
};
class Gunslinger : virtual public Person
{
int nick;//刻痕数
double time; //拔枪时间
public:
Gunslinger(int n=0, double t=0) : Person(), nick(n), time(t) {}//默认
Gunslinger(const string fname, const string lname, int n, double t)
: Person(fname, lname) , nick(n), time(t) {}
Gunslinger(const Person &p, int n, double t=0)
: Person(p), nick(n), time(t) {}
Gunslinger(const Gunslinger &g);
~Gunslinger();
double Draw() const ;
void Show() const;
};
class PokerPlayer : virtual public Person
{
public:
int Draw() const ;
void Show() const ;
PokerPlayer() : Person() {} // 默认
PokerPlayer(const string fname, const string lname)
: Person(fname, lname) {}
PokerPlayer(const Person &p) : Person(p) {}
PokerPlayer(const PokerPlayer &p) : Person(p) {}
~PokerPlayer();
};
class BadDude : public Gunslinger, public PokerPlayer
{
public:
BadDude() {};
BadDude(const string fname, const string lname, int n, double t)
: Person(fname, lname), Gunslinger(fname, lname, n, t), PokerPlayer(fname, lname) {}
BadDude(const Person &p, int n, double t)
: Person(p), Gunslinger(p, n, t), PokerPlayer(p) {}
BadDude(const Gunslinger &g, int n, double t)
: Person(g), Gunslinger(g, n, t), PokerPlayer(g) {}
BadDude(const PokerPlayer &p, int n, double t)
: Person(p), Gunslinger(p, n, t), PokerPlayer(p) {}
~BadDude();
void Show() const ;
double Gdraw();
int Cdraw();
};
#endif
person.cpp
#include "person.h"
#include <iostream>
using namespace std;
#include <string>
Person::Person(const Person &p)
{
firstname = p.firstname;
lastname = p.lastname;
}
Person::~Person()
{}
Gunslinger::Gunslinger(const Gunslinger &g) : Person(g)
{
nick = g.nick;
time = g.time;
}
double Gunslinger::Draw() const
{
return time;
}
void Gunslinger::Show() const
{
cout << "Category: Gunslinger\n";
Person::Show();
cout << "Nicks: " << nick << ", Gun out time: " << time << endl;
}
Gunslinger::~Gunslinger()
{}
int PokerPlayer::Draw() const
{
return ( rand() % 52);
}
void PokerPlayer::Show() const
{
cout << "Category: PokerPlayer\n";
Person::Show();
}
PokerPlayer::~PokerPlayer()
{}
void BadDude::Show() const
{
cout << "Category: Bad Dude\n";
Gunslinger::Show();
cout << "Next card: " << PokerPlayer::Draw() << endl;
}
double BadDude::Gdraw()
{
return Gunslinger::Draw();
}
int BadDude::Cdraw()
{
return PokerPlayer::Draw();
}
BadDude::~BadDude()
{}
play.cpp
#include <iostream>
#include <string>
#include "person.h"
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
srand(time(0));
const int SIZE = 4;
Person * p[SIZE];
int i;
for (i = 0; i < SIZE; i++)
{
char flag;
cout << "Enter the person category:\n" << "g: Gunslinger p: PokerPlayer b: BadDude q: Quit\n";
cin >> flag;
cin.ignore();
while (strchr("gpbq", flag) == NULL)
{
cout << "Please enter a g, p, b, or q: ";
cin >> flag;
cin.ignore();
}
if (flag == 'q')
break;
string fname;
string lname;
int n = 0;
double t = 0;
cout << "Please enter the first name: ";
getline(cin, fname);
cout << "Please enter the last name: ";
getline(cin, lname);
switch (flag)
{
case 'g':
cout << "Please enter the nicks: ";
cin >> n;
cout << "Please enter the \"Gun Out\" time: ";
cin >> t;
p[i] = new Gunslinger(fname, lname, n, t);
break;
case 'p':
p[i] = new PokerPlayer(fname, lname);
break;
case 'b':
cout << "Please enter the nicks: ";
cin >> n;
cout << "Please enter the \"Gun Out\" time: ";
cin >> t;
p[i] = new BadDude(fname, lname, n, t);
break;
}
}
cout << "\nHere is your person:\n";
int j;
for (j = 0; j < i; j++)
{
cout << endl;
p[j]->Show();
}
for (j = 0; j < i; j++)
delete p[j];
cout << "Bye\n";
return 0;
}
5.
emp.h
//按题给
#ifndef EMP_H
#define EMP_H
using namespace std;
#include <iostream>
#include <string>
class abstr_emp
{
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 ostream &operator<<(ostream &os, const abstr_emp &e);
virtual ~abstr_emp() = 0;
};
class employee : public abstr_emp
{
public:
employee();
employee(const string &fn, const string &ln, const string &j);
virtual void ShowAll() const ;
virtual void SetAll();
};
class manager : virtual public abstr_emp
{
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
{
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 &e);
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
emp.cpp
using namespace std;
#include <iostream>
#include <string>
#include "emp.h"
abstr_emp::abstr_emp()
{
fname = "None";
lname = "None";
job = "None";
}
abstr_emp::abstr_emp(const string &fn, const string &ln, const string &j)
{
fname = fn;
lname = ln;
job = j;
}
void abstr_emp::ShowAll() const
{
cout << "Firstname: " << fname << endl;
cout << "Lastname: " << lname << endl;
cout << "Job: " << job << endl;
}
void abstr_emp::SetAll()
{
cout << "Enter firstname: ";
getline(cin, fname);
cout << "Enter lastname: ";
getline(cin, lname);
cout << "Enter job: ";
getline(cin, job);
}
ostream &operator<<(ostream &os, const abstr_emp &e)
{
os << endl;
os << "Firstname: " << e.fname << endl;
os << "Lastname: " << e.lname << endl;
return os;
}
abstr_emp::~abstr_emp()
{}
employee::employee() : abstr_emp()
{}
employee::employee(const string &fn, const string &ln, const string &j)
: abstr_emp(fn, ln, j)
{}
void employee::ShowAll() const
{
cout << "Employee: " << endl;
abstr_emp::ShowAll();
}
void employee::SetAll()
{
cout << "Employee: " << endl;
abstr_emp::SetAll();
}
manager::manager()
: abstr_emp()
{
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
{
cout << "Manager: " << endl;
abstr_emp::ShowAll();
cout << "In charge of " << inchargeof << " employees.\n";
}
void manager::SetAll()
{
cout << "Manager: " << endl;
abstr_emp::SetAll();
cout << "In charge of: ";
cin >> inchargeof;
cin.ignore();
}
fink::fink()
: abstr_emp()
{
reportsto = "None";
}
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 &e)
: abstr_emp(e)
{
reportsto = e.reportsto;
}
void fink::ShowAll() const
{
cout << "Fink: " << endl;
abstr_emp::ShowAll();
cout << "Reports to: " << reportsto << endl;
}
void fink::SetAll()
{
cout << "Fink: " << endl;
abstr_emp::SetAll();
cout << "Reports to: " ;
getline(cin, reportsto);
}
highfink::highfink()
: abstr_emp(), manager(), fink()
{}
highfink::highfink(const string &fn, const string &ln, const string &j, const string &rpo, int ico)
: abstr_emp(fn, ln, j), manager(fn, ln, j, ico), fink(fn, ln, j, rpo)
{}
highfink::highfink(const abstr_emp &e, const string &rpo, int ico)
: abstr_emp(e), manager(e, ico), fink(e, rpo)
{}
highfink::highfink(const fink &f, int ico)
: abstr_emp(f), manager(f, ico), fink(f)
{}
highfink::highfink(const manager &m, const 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
{
cout << endl;
cout << "Highfink: " << endl;
abstr_emp::ShowAll();
cout << "In charge of: " << manager::InChargeOf() << endl;
cout << "Reports to: " << fink::ReportsTo() << endl;
cout << endl;
}
void highfink::SetAll()
{
cout << endl;
cout << "Highfink: " << endl;
abstr_emp::SetAll();
cout << "In charge of: " ;
cin >> manager::InChargeOf();
cin.ignore();
cout << "Reports to: " ;
getline(cin, fink::ReportsTo());
}
play.cpp
//按题给
using namespace std;
#include <iostream>
#include <string>
#include "emp.h"
int main()
{
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();
cin.ignore();
highfink hf2;
hf2.ShowAll();
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;
}