Читаем C++ Primer Plus полностью

Multiple Inheritance

MI describes a class that has more than one immediate base class. As with single inheritance, public MI should express an is-a relationship. For example, if you have a Waiter class and a Singer class, you could derive a SingingWaiter class from the two:

class SingingWaiter : public Waiter, public Singer {...};

Note that you must qualify each base class with the keyword public. That’s because the compiler assumes private derivation unless instructed otherwise:

class SingingWaiter : public Waiter, Singer {...}; // Singer is a private base

As discussed earlier in this chapter, private and protected MI can express a has-a relationship; the studenti.h implementation of the Student class is an example. We’ll concentrate on public inheritance now.

MI can introduce new problems for programmers. The two chief problems are inheriting different methods with the same name from two different base classes and inheriting multiple instances of a class via two or more related immediate base classes. Solving these problems involves introducing a few new rules and syntax variations. Thus, using MI can be more difficult and problem-prone than using single inheritance. For this reason, many in the C++ community object strongly to MI; some want it removed from the language. Others love MI and argue that it’s very useful, even necessary, for particular projects. Still others suggest using MI cautiously and in moderation.

Let’s explore a particular example and see what the problems and solutions are. You need several classes to create an MI situation. For this example, we’ll define an abstract Worker base class and derive a Waiter class and a Singer class from it. Then we can use MI to derive a SingingWaiter class from the Waiter and Singer classes (see Figure 14.3). This is a case in which a base class (Worker) is inherited via two separate derivations, which is the circumstance that causes the most difficulties with MI. We start with declarations for the Worker, Waiter, and Singer classes, as shown in Listing 14.7.

Figure 14.3. MI with a shared ancestor.

Listing 14.7. worker0.h


// worker0.h  -- working classes


#ifndef WORKER0_H_


#define WORKER0_H_



#include



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


};



#endif


The class declarations in Listing 14.7 include some internal constants that represent voice types. An enumeration makes alto, contralto, and so on symbolic constants for voice types, and the static array pv holds pointers to the C-style string equivalents. The implementation file, shown in Listing 14.8, initializes this array and provides method definitions.

Listing 14.8. worker0.cpp


// worker0.cpp -- working class methods


#include "worker0.h"


#include


using std::cout;


using std::cin;


using std::endl;


// Worker methods



// must implement virtual destructor, even if pure


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[] = {"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;


}


Перейти на страницу:

Все книги серии Developer's Library

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных

Похожие книги

1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных