// studentc.h -- defining a Student class using containment
#ifndef STUDENTC_H_
#define STUDENTC_H_
using std::istream;
using std::ostream;
using std::cin;
using std::string;
#include <string>
#include <valarray>
class Student
{
private:
typedef std::valarray<double> ArrayDb;
std::string name; // contained object
ArrayDb scores; // contained object
// private method for scores output
std::ostream& arr_out(std::ostream& os) const;
public:
Student() : name("Null Student"), scores() {}
explicit Student(const std::string& s)
: name(s), scores() {}
explicit Student(int n) : name("Nully"), scores(n) {}
Student(const std::string& s, int n)
: name(s), scores(n) {}
Student(const std::string& s, const ArrayDb& a)
: name(s), scores(a) {}
Student(const char* str, const double* pd, int n)
: name(str), scores(pd, n) {}
~Student() {}
double Average() const;
const std::string& Name() const;
double& operator[](int i);
double operator[](int i) const;
// friends
// input
friend std::istream& operator>>(std::istream& is,
Student& stu); // 1 word
friend std::istream& getline(std::istream& is,
Student& stu); // 1 line
// output
friend std::ostream& operator<<(std::ostream& os,
const Student& stu);
};
#endif
#ifndef Student2C_H_
#define Student2C_H_
#include <iostream>
#include <valarray>
#include <string>
class Student2 : private std::string, private std::valarray<double>
{
private:
typedef std::valarray<double> ArrayDb;
// private method for scores output
std::ostream& arr_out(std::ostream& os) const;
public:
Student2() : std::string("Null Student2"), ArrayDb() {}
explicit Student2(const std::string& s)
: std::string(s), ArrayDb() {}
explicit Student2(int n) : std::string("Nully"), ArrayDb(n) {}
Student2(const std::string& s, int n)
: std::string(s), ArrayDb(n) {}
Student2(const std::string& s, const ArrayDb& a)
: std::string(s), ArrayDb(a) {}
Student2(const char* str, const double* pd, int n)
: std::string(str), ArrayDb(pd, n) {}
~Student2() {}
double Average() const;
double& operator[](int i);
double operator[](int i) const;
const std::string& Name() const;
// friends
// input
friend std::istream& operator>>(std::istream& is,
Student2& stu); // 1 word
friend std::istream& getline(std::istream& is,
Student2& stu); // 1 line
// output
friend std::ostream& operator<<(std::ostream& os,
const Student2& stu);
};
#endif
#ifndef WORKER0_H_
#define WORKER0_H_
#include <string>
class Worker // an abstract base class
{
private:
std::string fullname;
long id;
public:
Worker() : fullname("no one"), id(0L) {}
Worker(const std::string& s, long n)
: fullname(s), id(n) {}
virtual ~Worker() = 0; // pure virtual destructor
virtual void Set();
virtual void Show() const;
};
class Waiter :virtual public Worker
{
private:
int panache;
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 };
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;
};
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 STACKTP_H_
#define STACKTP_H_
template <class Type>
class Stack
{
private:
enum { MAX = 10 }; // constant specific to class
Type items[MAX]; // holds stack items
int top; // index for top stack item
public:
Stack();
bool isempty();
bool isfull();
bool push(const Type& item); // add item to stack
bool pop(Type& item); // pop top into item
};
template <class Type>
Stack<Type>::Stack()
{
top = 0;
}
template <class Type>
bool Stack<Type>::isempty()
{
return top == 0;
}
template <class Type>
bool Stack<Type>::isfull()
{
return top == MAX;
}
template <class Type>
bool Stack<Type>::push(const Type& item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
template <class Type>
bool Stack<Type>::pop(Type& item)
{
if (top > 0)
{
item = items[--top];
return true;
}
else
return false;
}
#endif
void set(Student& sa, int n);
void set(Student2& sa, int n);
const int pupils = 3;
const int quizzes = 5;
#include <iostream>
using std::cin;
using std::endl;
using std::cout;
int main()
{
//14.1 baohan obj in class
Student ada[pupils] =
{ Student(quizzes), Student(quizzes), Student(quizzes) };
int i;
for (i = 0; i < pupils; ++i)
set(ada[i], quizzes);
cout << "\nStudent List:\n";
for (i = 0; i < pupils; ++i)
cout << ada[i].Name() << endl;
cout << "\nResults:";
for (i = 0; i < pupils; ++i)
{
cout << endl << ada[i];
cout << "average: " << ada[i].Average() << endl;
}
cout << "Done.\n";
// 14.2 private jicheng
Student2 ada[pupils] =
{ Student2(quizzes), Student2(quizzes), Student2(quizzes) };
int i;
for (i = 0; i < pupils; i++)
set(ada[i], quizzes);
cout << "\nStudent2 List:\n";
for (i = 0; i < pupils; ++i)
cout << ada[i].Name() << endl;
cout << "\nResults:";
for (i = 0; i < pupils; i++)
{
cout << endl << ada[i];
cout << "average: " << ada[i].Average() << endl;
}
cout << "Done.\n";
// 14.3 duochong jicheng MI
const int LIM = 4;
Waiter bob("Bob Apple", 314L, 5);
Singer bev("Beverly Hills", 522L, 3);
Waiter w_temp;
Singer s_temp;
Worker* pw[4] = { &bob, &bev, &w_temp, &s_temp };
int i;
for (i = 2; i < LIM; i++)
pw[i]->Set();
for (i = 0; i < LIM; i++)
{
pw[i]->Show();
std::cout << std::endl;
}
//14.3.2
using std::strchr;
const int SIZE = 5;
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[ct] = new Waiter;
break;
case 's': lolas[ct] = new Singer;
break;
case 't': lolas[ct] = new SingingWaiter;
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";
//14.4 class typelate
Stack<std::string> st; // create an empty stack
char ch;
std::string po;
cout << "Please enter A to add a purchase order,\n"
<< "P to process a PO, or Q to quit.\n";
while (cin >> ch && std::toupper(ch) != 'Q')
{
while (cin.get() != '\n')
continue;
if (!std::isalpha(ch))
{
cout << '\a';
continue;
}
switch (ch)
{
case 'A':
case 'a': cout << "Enter a PO number to add: ";
cin >> po;
if (st.isfull())
cout << "stack already full\n";
else
st.push(po);
break;
case 'P':
case 'p': if (st.isempty())
cout << "stack already empty\n";
else {
st.pop(po);
cout << "PO #" << po << " popped\n";
break;
}
}
cout << "Please enter A to add a purchase order,\n"
<< "P to process a PO, or Q to quit.\n";
}
cout << "Bye\n";
// cin.get();
// cin.get();
return 0;
void set(Student2& sa, int n)
{
cout << "Please enter the Student2's name: ";
getline(cin, sa);
cout << "Please enter " << n << " quiz scores:\n";
for (int i = 0; i < n; i++)
cin >> sa[i];
while (cin.get() != '\n')
continue;
}
void set(Student& sa, int n)
{
cout << "Please enter the student's name: ";
getline(cin, sa);
cout << "Please enter " << n << " quiz scores:\n";
for (int i = 0; i < n; i++)
cin >> sa[i];
while (cin.get() != '\n')
continue;
}
//public methods
double Student::Average() const
{
if (scores.size() > 0)
return scores.sum() / scores.size();
else
return 0;
}
const string& Student::Name() const
{
return name;
}
double& Student::operator[](int i)
{
return scores[i]; // use valarray<double>::operator[]()
}
double Student::operator[](int i) const
{
return scores[i];
}
// private method
ostream& Student::arr_out(ostream& os) const
{
int i;
int lim = scores.size();
if (lim > 0)
{
for (i = 0; i < lim; i++)
{
os << scores[i] << " ";
if (i % 5 == 4)
os << endl;
}
if (i % 5 != 0)
os << endl;
}
else
os << " empty array ";
return os;
}
// friends
// use string version of operator>>()
istream& operator>>(istream& is, Student& stu)
{
is >> stu.name;
return is;
}
// use string friend getline(ostream &, const string &)
istream& getline(istream& is, Student& stu)
{
getline(is, stu.name);
return is;
}
// use string version of operator<<()
ostream& operator<<(ostream& os, const Student& stu)
{
os << "Scores for " << stu.name << ":\n";
stu.arr_out(os); // use private method for scores
return os;
}
double Student2::Average() const
{
if (ArrayDb::size() > 0)
return ArrayDb::sum() / ArrayDb::size();
else
return 0;
}
const string& Student2::Name() const
{
return (const string&)*this;
}
double& Student2::operator[](int i)
{
return ArrayDb::operator[](i); // use ArrayDb::operator[]()
}
double Student2::operator[](int i) const
{
return ArrayDb::operator[](i);
}
// private method
ostream& Student2::arr_out(ostream& os) const
{
int i;
int lim = ArrayDb::size();
if (lim > 0)
{
for (i = 0; i < lim; i++)
{
os << ArrayDb::operator[](i) << " ";
if (i % 5 == 4)
os << endl;
}
if (i % 5 != 0)
os << endl;
}
else
os << " empty array ";
return os;
}
// friends
// use String version of operator>>()
istream& operator>>(istream& is, Student2& stu)
{
is >> (string&)stu;
return is;
}
// use string friend getline(ostream &, const string &)
istream& getline(istream& is, Student2& stu)
{
getline(is, (string&)stu);
return is;
}
// use string version of operator<<()
ostream& operator<<(ostream& os, const Student2& stu)
{
os << "Scores for " << (const string&)stu << ":\n";
stu.arr_out(os); // use private method for scores
return os;
}
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
Worker::~Worker() {}
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 << "\n";
cout << "Employee ID: " << id << "\n";
}
// Waiter methods
void Waiter::Set()
{
Worker::Set();
cout << "Enter waiter's panache rating: ";
cin >> panache;
while (cin.get() != '\n')
continue;
}
void Waiter::Show() const
{
cout << "Category: waiter\n";
Worker::Show();
cout << "Panache rating: " << panache << "\n";
}
// Singer methods
char* Singer::pv[7] = { "other", "alto", "contralto",
"soprano", "bass", "baritone", "tenor" };
void Singer::Set()
{
Worker::Set();
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;
while (cin >> voice && (voice < 0 || voice >= Vtypes))
cout << "Please enter a value >= 0 and < " << Vtypes << endl;
while (cin.get() != '\n')
continue;
}
void Singer::Show() const
{
cout << "Category: singer\n";
Worker::Show();
cout << "Vocal range: " << pv[voice] << endl;
}
// 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();
}
chapter14
最新推荐文章于 2022-04-07 22:33:00 发布