虚基类

#ifndef _WORKER_H_
#define _WORKER_H_

#include <string>

/**< A abstract base class */
class Worker
{
private:
    std::string fullname;
    long id;

protected:
    virtual void Data(void) const;
    virtual void Get(void);

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(void) = 0;
    virtual void Show(void) const = 0;
};

class Waiter : virtual public Worker
{
private:
    int panache;

protected:
    void Data(void) const;
    void Get(void);

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);
    void Show(void) const;
};

class Singer : virtual public Worker
{
protected:
    enum{other, alto, contralto, soprano, bass, baritone, tenor};
    enum{Vtypes = 7};

    void Data(void) const;
    void Get(void);

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);
    void Show(void) const;
};


/**< multiple inheritance */
class SingingWaiter : public Singer, public Waiter
{
protected:
    void Data(void) const;
    void Get(void);

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);
    void Show(void) const;
};

#endif //#define _WORKER_H_
#include "worker.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

/**< Worker methods */

//must implement virtual destructor, even if pure
Worker::~Worker() {}

//protected methods
void Worker::Data(void) const
{
    cout << "Name: " << fullname << endl;
    cout << "Employee ID: " << id << endl;
}

void Worker::Get(void)
{
    getline(cin, fullname);
    cout << "Enter worker's ID: ";
    cin >> id;
    while(cin.get() != '\n')
    {
        continue;
    }
}

/**< Waiter methods */

void Waiter::Set(void)
{
    cout << "Enter waiter's panache rating: ";
    Worker::Get();
    Get();
}

void Waiter::Show(void) const
{
    cout << "Category: waiter\n";
    Worker::Data();
    Data();
}

//protected methods
void Waiter::Data(void) const
{
    cout << "Panache rating: " << panache << endl;
}
void Waiter::Get(void)
{
    cout << "Enter waiter's panache rating: ";
    cin >> panache;
    while(cin.get() != '\n')
    {
        continue;
    }
}

/**< Singer methods */

char * Singer::pv[Singer::Vtypes] = {"other", "alto", "contralto", "soprano", "bass", "baritone", "tenor"};

void Singer::Set(void)
{
    cout << "Enter singer's name: ";
    Worker::Get();
    Get();
}

void Singer::Show(void) const
{
    cout << "Category: singer\n";
    Worker::Data();
    Data();
}

//protected methods
void Singer::Data(void) const
{
    cout << "Vocal range: " << pv[voice] << endl;
}

void Singer::Get(void)
{
    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;
    }
}

/**< SingingWaiter methods */

void SingingWaiter::Data(void) const
{
    Singer::Data();
    Waiter::Data();
}

void SingingWaiter::Get(void)
{
    Waiter::Get();
    Singer::Get();
}

void SingingWaiter::Set(void)
{
    cout << "Enter singing waiter's name: ";
    Worker::Get();
    Get();
}

void SingingWaiter::Show(void) const
{
    cout << "Category: singing waiter\n";
    Worker::Data();
    Data();
}
#include <iostream>
#include <cstring>
#include "worker.h"

using std::cout;
using std::cin;
using std::endl;

const int SIZE = 5;

int main(void)
{
    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";

    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;
}

 

posted @ 2013-10-18 14:18  瓶哥  Views(230)  Comments(0Edit  收藏  举报