C++ primer plus 第十四章课后编程练习参考

C++ primer plus 书已经看到第14章,明显感觉到难度上升,也同时在逐渐逼近C++的核心内容
课后编程参考如下:
14.1

//wine.h
#ifndef WINE_H_INCLUDED
#define WINE_H_INCLUDED
#include <string>
#include <valarray>
#include <iostream>
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::valarray;

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){}
    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;
}
typedef valarray<int>ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
class Wine
{
private:
    int year;
    string label;
    PairArray yr_bot;
public:
    Wine(const char * la, int y,const int yr[], const int bot[]);
    Wine(const char * la, int y);
    void GetBottles();
    string& Label(){return label;}
    string Label()const{return label;}
    int sum(){return yr_bot.second().sum();}
    void Show()const;
};
Wine::Wine(const char * la, int y, const int yr[], const int bot[])
{
    label = la;
    yr_bot = PairArray(ArrayInt(y), ArrayInt(y));
    year = y;
    for (int i = 0; i < year; i++)
    {
        yr_bot.first()[i] = yr[i];
        yr_bot.second()[i] = bot[i];
    }
}
Wine::Wine (const char * la, int y)
{
    label = la;
    year = y;
    yr_bot = PairArray(ArrayInt(y), ArrayInt(y));
}
void Wine::GetBottles()
{
    cout << "Enter " << label << " data for " << year << " years.\n";
    for (int i = 0; i < year; i++)
    {
        cout << "Enter years: ";
        cin >> yr_bot.first()[i];
        cout << "Enter bottles for the year: ";
        cin >> yr_bot.second()[i];
    }
}
void Wine::Show()const
{
    cout << "Wine: " << label << endl;
    if (year < 1)
        cout << "Out of stock~~";
    else
    {
        cout << "\tYear\tBottles" << endl;
        for (int i = 0; i < year; i++)
            cout << "\t" << yr_bot.first()[i]
                << "\t" << yr_bot.second()[i] << endl;
    }
}

#endif // WINE_H_INCLUDED
//14.1主程序
#include <iostream>
#include "wine.h"
using std::cin;
using std::cout;
using std::endl;
//using std::

int main()
{
    cout << "Enter name of wine: ";
    char lab[50];
    cin.getline(lab, 50);
    cout << "Enter number of year: ";
    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;
}

14.2

//wine2.h
#ifndef WINE_H_INCLUDED
#define WINE_H_INCLUDED
#include <string>
#include <valarray>
#include <iostream>
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::valarray;

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){}
    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;
}
typedef valarray<int>ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;

class Wine : private string, private PairArray
{
private:
    int year;
public:
    Wine(const char * la, int y,const int yr[], const int bot[]);
    Wine(const char * la, int y);
    void GetBottles();
    string& Label(){return (string& )(*this);}
    string Label()const{return (string& )(*this);}
    int sum(){return PairArray(*this).second().sum();}
    void Show()const;

};
Wine::Wine(const char * la, int y, const int yr[], const int bot[])
            :string(la),PairArray(ArrayInt(yr, y), ArrayInt(bot, y))
{
    year = y;
}
Wine::Wine (const char * la, int y) :string(la), PairArray(ArrayInt(y), ArrayInt(y))
{
    year = y;
}
void Wine::GetBottles()
{
    cout << "Enter " << (const string& )(*this) << " data for " << year << " years.\n";
    for (int i = 0; i < year; i++)
    {
        cout << "Enter years: ";
        cin >> (PairArray)(*this).first()[i];//PairArray::first()[i];
        cout << "Enter bottles for the year: ";
        cin >> PairArray::second()[i];
    }
}
void Wine::Show()const
{
    cout << "Wine: " << (const string& )(*this) << endl;
    if (year < 1)
        cout << "Out of stock~~";
    else
    {
        cout << "\tYear\tBottles" << endl;
        for (int i = 0; i < year; i++)
            cout << "\t" << PairArray::first()[i]
                << "\t" << PairArray::second()[i] << endl;
    }
}

#endif // WINE_H_INCLUDED

//14.2主程序
#include <iostream>
#include "wine2.h"
using std::cin;
using std::cout;
using std::endl;
//using std::

int main()
{
    cout << "Enter name of wine: ";
    char lab[50];
    cin.getline(lab, 50);
    cout << "Enter number of year: ";
    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;
}

14.3

//QueueTp.h
#ifndef QUEUETP_H_INCLUDED
#define QUEUETP_H_INCLUDED
template <typename Type>
class QueueTp
{
private:
    struct Node{Type item; struct Node* next;};
    enum {Q_SIZE = 10};
    Node * front;
    Node * rear;
    int items;
    const int qsize;
    QueueTp(QueueTp & QT);
    QueueTp& operator=(QueueTp & QT);
public:
    QueueTp(int qs = Q_SIZE):qsize(qs), items(0){front = rear = nullptr;}
    ~QueueTp();
    bool isempty()const;
    bool isfull()const;
    int queuecount ()const;
    bool enqueue(const Type &item);
    bool dequeue(Type &item);
};
template <typename Type>
QueueTp<Type>::~QueueTp()
{
    Node* Temp;
    while(front != nullptr)
    {
        Temp = front;
        front = front->next;
        delete Temp;
    }
}
template <typename Type>
bool QueueTp<Type>::isempty()const
{
    return items == 0;
}
template <typename Type>
bool QueueTp<Type>::isfull()const
{
    return items == qsize;
}
template <typename Type>
int QueueTp<Type>::queuecount ()const
{
    return items;
}
template <typename Type>
bool QueueTp<Type>::enqueue(const Type &item)
{
    if (isfull())
        return false;
    Node * add = new Node;
    add->item = item;
    add->next = nullptr;
    items++;
    if (front == nullptr)
    {
        front = add;
        rear = add;
    }
    else
    {
        rear->next = add;
        rear = add;
    }
    return true;

}
template <typename Type>
bool QueueTp<Type>::dequeue(Type &item)
{
    if (isempty())
        return false;
    item = front->item;
    Node* temp;
    temp = front;
    front = front->next;
    delete temp;
    items--;
    if (front == nullptr)
        rear = nullptr;
    return true;
}

#endif // QUEUETP_H_INCLUDED

//workermi.h
#ifndef WORKERMI_H_INCLUDED
#define WORKERMI_H_INCLUDED
#include <string>
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::cin;

class Worker //abstract base class
{
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 const char *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(other){}
    void Set();
    void Show()const;
};
//multiple inheritance
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), Singer(wt), Waiter(wt, p){}
    void Set();
    void Show()const;
};


//worker methods
Worker::~Worker(){};

//private:
//    std::string fullname;
//    long id;
//protected:
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;
}
//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 Waiter::Data()const
{
    cout << "Panache rating: " << panache << endl;
}
void Waiter::Get()
{
    cout << "Enter waiter's panache rating: ";
    cin >> panache;
    while (cin.get() != '\n')
        continue;
}

//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 Waiter::Set()
{
    cout << "Enter waiter's name: ";
    Worker::Get();
    Get();
}
void Waiter::Show() const
{
    cout << "Category: waiter\n";
    Worker::Data();
    Data();
}

//class Singer : virtual public Worker

//protected:
//    enum {other, alto, contralto, soprano,
//            bass, baritone, tenor};
//    enum {Vtypes = 7};
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 << '\n';
    cin >> voice;
    while (cin.get() != '\n')
        continue;
}
//private:
const char * Singer::pv[Singer::Vtypes] = {"other", "alto",
        "contralto", "soprano", "bass", "baritone", "tenor"}; // 存储声音类型字符串,注意静态成员初始化方法
//    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(other){}
void Singer::Set()
{
    cout << "Enter singer's name: ";
    Worker::Get();
    Get();
}
void Singer::Show()const
{
    cout << "Category: singer\n";
    Worker::Data();
    Data();
}

//class SingingWaiter : public Singer, public Waiter

//protected:
void SingingWaiter::Data()const
{
    Singer::Data();
    Waiter::Data();
}
void SingingWaiter::Get()
{
    Waiter::Get();
    Singer::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), Singer(wt), Waiter(wt, p){}
void SingingWaiter::Set()
{
    cout << "Enter singing waiter's name: ";
    Worker::Get();
    Get();
}
void SingingWaiter::Show()const
{
    cout << "Category: singing waiter\n";
    Worker::Data();
    Data();
}


#endif // WORKERMI_H_INCLUDED

//14.3 主程序
#include <iostream>
#include <cstring>
#include "QueueTp.h"
#include "workermi.h"
using std::cin;
using std::cout;
using std::endl;
using std::strchr;
const int SIZE = 5;
int main()
{
    QueueTp<Worker*> Qwk(SIZE);
    Worker* wtemp;
    int ct;
    for (ct = 0; ct < SIZE; ct++)
    {
        char choice;
        cout << "Enter the employee catagory:\n"
            << "w: waiter  s: singer  t: singing waiter  q: quit\n";
        cin >> choice;
        while (strchr( "wstq", choice ) == nullptr)
        {
            cout << "Please enter a w, s, t, or q: ";
            cin.get(choice);
        }
        cin.get();
        if (choice == 'q')
            break;
        switch (choice)
        {
            case 'w':
                    wtemp = new Waiter;

                    wtemp->Set();
                    Qwk.enqueue(wtemp);
                    break;
            case 's':
                    wtemp = new Singer;
                    wtemp->Set();
                    Qwk.enqueue(wtemp);
                    break;
            case 't':
                    wtemp = new SingingWaiter;
                    wtemp->Set();
                    Qwk.enqueue(wtemp);
                    break;
        }
    }
    cout << "\nHere is your staff:\n";
    for (int i = 0; i < SIZE; i++)
    {
        Qwk.dequeue(wtemp);
        wtemp->Show();
        delete wtemp; //这里出队后直接释放,这种显示队列方法,破坏了队列
                    //也可以采用循环的方式,出队后再入队,最后再加一个循环
                    //用以释放内存。使用模板指针队列就麻烦在这里...
                    //将模板部分显示具体化,对指针队列进行特殊处理,
                    //会好点
    }


    return 0;
}

14.4

//Person.h
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED
#include <string>
using std::string;
class Person
{
private:
    string lname;
    string fname;
protected:
    virtual void Data()const;
    virtual void Get();
public:
    Person(const char* ln = "null",
           const char* fn = "null") :lname(ln), fname(fn){}
    virtual void set() = 0;
    virtual~Person(){}
    virtual void Show()const = 0;
};
class Gunslinger : virtual public Person
{
private:
    double draw;
    int scratchs;
protected:
    virtual void Data()const;
    virtual void Get();
public:
    Gunslinger(const char* ln = "null",const char* fn = "null"
            , double dr = 0, int schs = 0)
             : Person(ln, fn), draw(dr), scratchs(schs){}
    Gunslinger(const Person& pr, double dr = 0, int schs = 0)
                :Person(pr), draw(dr), scratchs(schs){}
    ~Gunslinger(){};
    double Draw()const{return draw;}
    void set();
    void Show()const;
};

class PokerPlayer : virtual public Person
{
public:
    PokerPlayer(const char* ln = "null",
           const char* fn = "null") :Person(ln, fn){}
    PokerPlayer(const Person& ps): Person(ps){}
    int Draw();
    void set();
    void Show()const;
};
class BadDude : public Gunslinger, public PokerPlayer
{
public:
    BadDude(const char* ln = "null",const char* fn = "null",
            double dr = 0, int schs = 0)
        :Person(ln, fn), Gunslinger(ln, fn, dr, schs), PokerPlayer(ln, fn){}
    BadDude(const Person& pr, double dr = 0, int schs = 0)
        :Person(pr), Gunslinger(pr, dr, schs),PokerPlayer(pr){}
    BadDude(const Gunslinger& gs):Person(gs), Gunslinger(gs){}
    BadDude(const PokerPlayer& pp, double dr = 0, int schs = 0)
        :Person(pp), Gunslinger(pp, dr, schs), PokerPlayer(pp){}

    double Gdraw();
    int Cdraw();
    void set();
    void Show()const;
};


#endif // PERSON_H_INCLUDED
//Person.cpp
#include "person.h"
#include <string>
#include <iostream>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
using std::string;
//class Person Methods

//private:
//    string lname;
//    string fname;
//public:
//Person::Person(const char* ln = "null",
//           const char* fn = "null") :lname(ln), fname(fn){}
//    virtual~Person(){}
void Person::Get()
{
    cout << "Name rewrite: \n";
    cout << "last name: ";
    getline(cin, lname);
    cout << "first name: ";
    getline(cin, fname);
}
void Person::Data()const
{
    cout << "Name: " << lname << ' ' << fname << endl;
}

//class Gunslinger : public Person Methods

//private:
//    double draw;
//    int scratchs;
//public:
//    Gunslinger(const char* ln = "null",const char* fn = "null"
//            , double dr = 0, int schs = 0)
//             : Person(ln, fn), draw(dr), scratchs(schs){}
//    Gunslinger(const Person& pr, double dr = 0, int schs = 0)
//                :Person(pr), draw(dr), scratchs(schs){}
//    double Draw()const{return draw;}
void Gunslinger::Data()const
{
    cout << "Draw time: " << draw << endl;
    cout << "Scratchs: " << scratchs << endl;
}
void Gunslinger::Get()
{
    cout << "Draw time: ";
    cin >> draw;
    cout << "scratchs: ";
    cin >> scratchs;
}
void Gunslinger::Show()const
{
    cout << "Gunslinger: \n";
    Person::Data();
    Data();
}
void Gunslinger::set()
{
    cout << "Gunslinger Information rewrite: \n";
    Person::Get();
    Get();
}

//class PokerPlayer : virtual Person

//public:
//    PokerPlayer(const char* ln = "null",
//           const char* fn = "null") :Person(ln, fn){}
//    PokerPlayer(Person& ps): Person(ps){}
int PokerPlayer::Draw()
{
    return (rand() / 52 + 1);
}
void PokerPlayer::set()
{
    cout << "PokerPlayer Information rewrite: \n";
    Person::Get();
}
void PokerPlayer::Show()const
{
    cout << "PokerPlayer: \n";
    Person::Data();
}
//class BadDude :public Gunslinger, public PokerPlayer

double BadDude::Gdraw()
{
    return Gunslinger::Draw();
}
int BadDude::Cdraw()
{
    return PokerPlayer::Draw();
}
void BadDude::set()
{
    cout << "Bad Dude information rewrite: \n";
    Person::Get();
    Gunslinger::Get();
}
void BadDude::Show()const
{
    cout << "Bad Dude:\n";
    Person::Data();
    Gunslinger::Data();
}
//14.4主程序
#include <iostream>
#include "person.h"
#include <cstring>
#include <ctime>
#include <cstdlib>
using std::cin;
using std::cout;
using std::endl;
using std::strchr;
const int SIZE = 3;

int main()
{
    srand(time(0));
    Person* PO[SIZE];
    int ct;
    for (ct = 0; ct < SIZE; ct++)
    {
        char choice;
        cout << "Enter the people kind:\n"
            << "g: gunslinger  p: pokerplayer  b: bad dude  q: quit\n";
        cin >> choice;
        while (strchr( "gpbq", choice ) == nullptr)
        {
            cout << "Please enter a g, p, b or q: ";
            cin.get(choice);
        }
        cin.get();
        if (choice == 'q')
            break;
        switch (choice)
        {
            case 'g':
                    PO[ct] = new Gunslinger;
                    PO[ct]->set();
                    break;
            case 'p':
                    PO[ct] = new PokerPlayer;
                    PO[ct]->set();
                    break;
            case 'b':
                    PO[ct] = new BadDude;
                    PO[ct]->set();
                    break;
                    break;
        }
    }
    cout << "\nHere is your staff:\n";
    for (int i = 0; i < SIZE; i++)
    {
        PO[i]->Show();
    }
    PokerPlayer temp("yulong", "gao");
    temp.Show();
    cout << "His next card: " << temp.Draw();
    return 0;
}

14.5

//emp.h
#ifndef EMP_H_INCLUDED
#define EMP_H_INCLUDED
#include <iostream>
#include <string>

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);
    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)
        :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);
    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)
        : abstr_emp(fn, ln, j), manager(fn, ln, j, ico), fink(fn, ln, j, 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);
    virtual void ShowAll() const;
    virtual void SetAll();
};
#endif // EMP_H_INCLUDED

//emp.cpp
#include "emp.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
//class abstr_emp

//private:
//    std::string fname;
//    std::string lname;
//    std::string job;
//public:
abstr_emp::abstr_emp()
{
    fname = lname = job = "null";
}
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 << "Name: " << fname << ' ' << lname << endl;
    cout << "Job: " << job << endl;
}
void abstr_emp::SetAll()
{
    cout << "first name: ";
    cin >> fname;
    cout << "last name: ";
    cin >> lname;
    cout << "job: " ;
    cin >> job;
}
std::ostream& operator<<(std::ostream& os, const abstr_emp & e)
{
    os << e.fname << ' ' << e.lname;
    return os;
}

abstr_emp::~abstr_emp(){}

//class employee : public abstr_emp Methods

//public:
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();
}

//class manager: virtual public abstr_emp

//private:
//    int inchargeof;
//public:
manager::manager(){inchargeof = 0;}
manager::manager(const std::string & fn, const std::string & ln,
         const std::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 information: \n";
    abstr_emp::ShowAll();
    cout << "Incharge: " << inchargeof << endl;
}
void manager::SetAll()
{
    abstr_emp::SetAll();
    cout << "Incharge: ";
    cin >> inchargeof;
}

//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()
{
    reportsto = "null";
}


fink::fink(const fink& e):abstr_emp(e)
{
    reportsto = e.reportsto;
}
void fink::ShowAll()const
{
    cout << "Fink information: " << endl;
    abstr_emp::ShowAll();
    cout << "Reports to: " << reportsto << endl;
}
void fink::SetAll()
{
    cout <<"Fink name: " << endl;
    abstr_emp::SetAll();
    cout << "Reports to: " << endl;
    getline(cin, reportsto);
}
//class highfink: public manager, public fink Methods

//public:
//    highfink(){}
//    highfink(const std::string & fn, const std::string & ln,
//            const std::string & j, std::string& rpo,
//            int ico)
//        : abstr_emp(fn, ln, j), manager(fn, ln, j, ico), fink(fn, ln, j, 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::highfink(const highfink& h)
:abstr_emp(h), manager(h), fink(h)
{

}
void highfink::ShowAll() const
{
    cout << "High fink information: \n";
    abstr_emp::ShowAll();
    cout << "Incharge: " << manager::InChargeOf() << endl;;
    cout << "Reports to: " << fink::ReportsTo() << endl;
}
void highfink::SetAll()
{
    cout << "High fink name: ";
    abstr_emp::SetAll();
    cout << "Incharge: ";
    cin >> manager::InChargeOf();
    cin.get();
    cout << "Report to: ";
    getline(cin, fink::ReportsTo());
}


//14.5主程序
#include <iostream>
#include "emp.h"
using namespace std;

int main()
{
    employee em("Trip", "Harris", "Thumpsr");
    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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值