winec.h
#ifndef winec_H_ #define winec_H_ #include<iostream> #include<valarray> #include<string> using std::cout; using std::cin; template <typename T1, typename T2> class Pair { private: T1 a; T2 b; public: T1 & first() { return a; } T2 & second() { return b; } T1 first()const { return a; } T2 second()const { return b; } Pair() {} Pair(const T1 & a1, const T2 & b2) :a(a1), b(b2) {} void setfirst(const T1 & t) { a = t; } void setsecond(const T2 & t) { b = t; } }; typedef std::valarray<int>ArrayInt; typedef Pair<ArrayInt, ArrayInt>PairArray; class Wine { private: std::string brand; PairArray p; int year; public: Wine(const char * l, int y, const int yr[], const int bot[]); Wine(const char * l, int y); void GetBottles(); void show(); int sum(); std::string &Label() { return brand; } }; Wine::Wine(const char * l, int y, const int yr[], const int bot[]) { brand = l; year = y; ArrayInt f(yr, y); ArrayInt b(bot, y); p.first() = f; p.second() = b; } Wine::Wine(const char * l, int y) { brand = l; year = y; p.first() = ArrayInt(y); p.second() = ArrayInt(y); } void Wine::GetBottles() { cout << "Enter " << brand << " data for " << year << " year(s):\n"; for (int i = 0; i < year; ++i) { cout << "Enter year: "; cin >> p.first()[i]; cin.get(); cout << "Enter bottles for that year: "; cin >> p.second()[i]; cin.get(); } } void Wine::show() { cout << "Wine: " << brand << '\n'; cout << " Year Bottles" << '\n'; for (int i = 0; i < year; ++i) cout << " " << p.first()[i] << " " << p.second()[i] << '\n'; } int Wine::sum() { return p.second().sum(); } #endifmain.cpp
#include "winec.h" #include <iostream> int main() { 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 Graper Red", YRS, y, b); more.show(); cout << "Total bottles for " << more.Label() << ": " << more.sum() << endl; cout << "Bye\n"; return 0; }winec.h
#ifndef winec_H_ #define winec_H_ #include<iostream> #include<valarray> #include<string> using std::cout; using std::cin; template <typename T1, typename T2> class Pair { private: T1 a; T2 b; public: T1 & first() { return a; } T2 & second() { return b; } T1 first()const { return a; } T2 second()const { return b; } Pair() {} Pair(const T1 & a1, const T2 & b2) :a(a1), b(b2) {} void setfirst(const T1 & t) { a = t; } void setsecond(const T2 & t) { b = t; } }; typedef std::valarray<int>ArrayInt; typedef Pair<ArrayInt, ArrayInt>PairArray; class Wine :private std::string, private PairArray { private: int year; public: Wine(const char * l, int y, const int yr[], const int bot[]) :std::string(l), year(y), PairArray(ArrayInt(yr, y), ArrayInt(bot, y)) {} Wine(const char * l, int y) :std::string(l), year(y) {} void GetBottles(); void show(); int sum(); std::string &Label() { return (std::string &) *this; } }; void Wine::GetBottles() { ArrayInt yr(year), bot(year); cout << "Enter " << (const std::string &)*this << " data for " << year << " year(s):\n"; for (int i = 0; i < year; ++i) { cout << "Enter year: "; cin >> yr[i]; cin.get(); cout << "Enter bottles for that year: "; cin >> bot[i]; cin.get(); } PairArray::first() = yr; PairArray::second() = bot; } void Wine::show() { cout << "Wine: " << (const std::string &)*this << '\n'; cout << " Year Bottles" << '\n'; for (int i = 0; i < year; ++i) cout << " " << PairArray::first()[i] << " " << PairArray::second()[i] << '\n'; } int Wine::sum() { return PairArray::second().sum(); } #endifmain.cpp
#include "winec.h" #include <iostream> int main() { 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 Graper Red", YRS, y, b); more.show(); cout << "Total bottles for " << more.Label() << ": " << more.sum() << endl; cout << "Bye\n"; return 0; }QueueTP.h
#ifndef QUEUE_H_ #define QUEUE_H_ #include<iostream> #include<string> using std::cout; using std::cin; using std::endl; template<typename 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 { return items == 0; } bool isfull() const { return items == qsize; } int queuecount() const { return items; } bool enqueue(const T &item); bool dequeue(T & item); }; 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) {} virtual ~Worker() = 0; 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 const*pv[Vtypes]; 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 Waiter, public Singer { 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; }; template<typename T> QueueTP<T>::QueueTP(int qs) :qsize(qs) { Front = rear = nullptr; items = 0; } template<typename T> QueueTP<T>::~QueueTP() { Node * temp; while (Front != nullptr) { temp = Front; Front = Front->next; delete temp; } } template<typename T> bool QueueTP<T>::enqueue(const T &item) { if (isfull()) return false; Node * add = new Node; add->item = item; add->next = nullptr; items++; if (Front == nullptr) Front = add; else rear->next = add; rear = add; return true; } template<typename T> bool QueueTP<T>::dequeue(T & item) { if (Front == nullptr) return false; item = Front->item; items--; Node * temp = Front; Front = Front->next; delete temp; if (items == 0) rear = nullptr; return true; } Worker::~Worker() {} 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; } void Waiter::Set() { cout << "Enter waiter's name: "; Worker::Get(); Get(); } void Waiter::Show() const { cout << "Category:waiter\n"; Worker::Data(); Data(); } 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; } char const* 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(); } 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 << endl; cin >> voice; while (cin.get() != '\n') continue; } 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(); } #endifmain.cpp
#include "QueueTP.h" #include <iostream> const int SIZE = 5; int main() { using std::strchr; QueueTP<Worker*>line; 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)) { 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(); line.enqueue(lolas[ct]); } cout << "\nHere is your staff:\n"; for (int i = 0; i < ct; i++) { cout << endl; lolas[i]->Show(); } for (int i = 0; i < ct; i++) delete lolas[i]; cout << "Bye.\n"; return 0; }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 = 0; }; class Gunslinger : virtual public Person { private: 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); double Draw() const { return time; } int NikcCount() const { return nick; } 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) {} }; class BadDude : public Gunslinger, public PokerPlayer { public: BadDude():Person() {} 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) {} void Show() const; double Gdraw(); int Cdraw(); }; void Person::Show() const { cout << "Firstname: " << firstname << "\nLastname: " << lastname << endl; } Person::Person(const Person & p) { firstname = p.firstname; lastname = p.lastname; } Gunslinger::Gunslinger(const Gunslinger & g) : Person(g) { nick = g.nick; time = g.time; } void Gunslinger::Show() const { cout << "Category: Gunslinger\n"; Person::Show(); cout << "Nicks: " << nick << ", Gun out time: " << time << endl; } int PokerPlayer::Draw() const { return (rand() % 52); } void PokerPlayer::Show() const { cout << "Category: PokerPlayer\n"; Person::Show(); } 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(); } #endifmain.cpp
#include "Person.h" #include<time.h> #include<stdlib.h> 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; }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() :fname("none"), lname("none"), job("none") {} 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() :abstr_emp() {} 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(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("none") {} 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(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(); }; void abstr_emp::ShowAll() const { cout << "First name: " << fname << endl; cout << "Last name: " << lname << endl; cout << "Job: " << job << endl; } void abstr_emp::SetAll() { cout << "Please enter the first name: "; getline(cin, fname); cout << "Please enter the last name: "; getline(cin, lname); cout << "Please enter the job: "; getline(cin, job); } std::ostream & operator<<(std::ostream & os, const abstr_emp & e) { os << "First name: " << e.fname << endl; os << "Last name: " << e.lname << endl; os << "Job: " << e.job << endl; return os; } void employee::ShowAll() const { cout << "Employee: " << endl; abstr_emp::ShowAll(); } void employee::SetAll() { cout << "Employee: " << endl; abstr_emp::SetAll(); } void manager::ShowAll() const { cout << "manager: " << endl; abstr_emp::ShowAll(); cout << "in charge of " << inchargeof << "employees." << endl; } void manager::SetAll() { cout << "manager: " << endl; abstr_emp::SetAll(); cout << "Please Enter number of abstr_emps managed: "; cin >> inchargeof; cin.get(); } void fink::ShowAll() const { cout << "Fink: " << endl; abstr_emp::ShowAll(); cout << "reports to: " << reportsto; } void fink::SetAll() { cout << "Fink: " << endl; abstr_emp::SetAll(); cout << "Reports to: "; getline(cin, reportsto); } void highfink::ShowAll() const { cout << endl; cout << "Highfink: " << endl; abstr_emp::ShowAll(); //cin.get(); cout << "In charge of: " << manager::InChargeOf() << endl; cout << "Reports to: " << fink::ReportsTo() << endl; cout << endl; } void highfink::SetAll() { cout << "Highfink: " << endl; cin.get(); abstr_emp::SetAll(); cout << "In charge of: "; cin >> manager::InChargeOf(); cin.ignore(); cout << "Reports to: "; getline(cin, fink::ReportsTo()); } #endifmain.cpp
#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; }1.因为用不上 2.因为同一个方法在派生类和基类的行为是不同的,使用virtual,程序根据引用或指针指向的对象的类型来调用方法。 3.因为highfink是多重继承,每个继承都继承自同一个基类,使用虚基类使得从多个类派生出的对象只继承一个基类对象。 4.因为继承的类中已经包含足够的数据项了。 5.因为允许基类引用隐式地引用派生类对象,可以使用基类引用为派生类对象调用基类的方法。所以一个operator就行了 6.编译无法通过,因为abstr_emp是抽象类,无法生成对象.
