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

// public methods


double Student::Average() const


{


    if (ArrayDb::size() > 0)


        return ArrayDb::sum()/ArrayDb::size();


    else


        return 0;


}



const string & Student::Name() const


{


    return (const string &) *this;


}



double & Student::operator[](int i)


{


    return ArrayDb::operator[](i);         // use ArrayDb::operator[]()


}



double Student::operator[](int i) const


{


    return ArrayDb::operator[](i);


}



// private method


ostream & Student::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, Student & stu)


{


    is >> (string &)stu;


    return is;


}



// use string friend getline(ostream &, const string &)


istream & getline(istream & is, Student & stu)


{


    getline(is, (string &)stu);


    return is;


}



// use string version of operator<<()


ostream & operator<<(ostream & os, const Student & stu)


{


    os << "Scores for " << (const string &) stu  << ":\n";


    stu.arr_out(os);  // use private method for scores


    return os;


}


Again, because the example reuses the string and valarray code, relatively little new code is needed, aside from the private helper method.

Using the Revised Student Class

Once again, it’s time to test a new class. Note that the two versions of the Student class have exactly the same public interface, so you can test the two versions with exactly the same program. The only difference is that you have to include studenti.h instead of studentc.h, and you have to link the program with studenti.cpp instead of with studentc.cpp. Listing 14.6 shows the program. Be sure to compile it along with studenti.cpp.

Listing 14.6. use_stui.cpp


// use_stui.cpp -- using a class with private inheritance


// compile with studenti.cpp


#include


#include "studenti.h"


using std::cin;


using std::cout;


using std::endl;



void set(Student & sa, int n);



const int pupils = 3;


const int quizzes = 5;



int main()


{


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


    return 0;


}



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;


}


Here is a sample run of the program in Listing 14.6:

Please enter the student's name: Gil Bayts


Please enter 5 quiz scores:


92 94 96 93 95


Please enter the student's name: Pat Roone


Please enter 5 quiz scores:


83 89 72 78 95


Please enter the student's name: Fleur O'Day


Please enter 5 quiz scores:


92 89 96 74 64



Student List:


Gil Bayts


Pat Roone


Fleur O'Day



Results:


Scores for Gil Bayts:


92 94 96 93 95


average: 94



Scores for Pat Roone:


83 89 72 78 95


average: 83.4



Scores for Fleur O'Day:


92 89 96 74 64


average: 83


Done.

The same input as before leads to the same output that the containment version produces.

Containment or Private Inheritance?

Given that you can model a has-a relationship either with containment or with private inheritance, which should you use? Most C++ programmers prefer containment. First, it’s easier to follow. When you look at the class declaration, you see explicitly named objects representing the contained classes, and your code can refer to these objects by name. Using inheritance makes the relationship appear more abstract. Second, inheritance can raise problems, particularly if a class inherits from more than one base class. You may have to deal with issues such as separate base classes having methods with the same name or of separate base classes sharing a common ancestor. All in all, you’re less likely to run into trouble using containment. Also containment allows you to include more than one subobject of the same class. If a class needs three string objects, you can declare three separate string members by using the containment approach. But inheritance limits you to a single object. (It is difficult to tell objects apart when they are all nameless.)

However, private inheritance does offer features beyond those provided by containment. Suppose, for example, that a class has protected members, which could either be data members or member functions. Such members are available to derived classes but not to the world at large. If you include such a class in another class by using composition, the new class is part of the world at large, not a derived class. Hence it can’t access protected members. But using inheritance makes the new class a derived class, so it can access protected members.

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

Все книги серии 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.

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

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