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

    os << "Scores for " << stu.name << ":\n";


    ...


}

Because stu.name is a string object, it invokes the operator<<(ostream &, const string &) function, which is provided as part of the string class package. Note that the operator<<(ostream & os, const Student & stu) function has to be a friend to the Student class so that it can access the name member. (Alternatively, the function could use the public Name() method instead of the private name data member.)

Similarly, the function could use the valarray implementation of << for output; unfortunately, there is none. Therefore, the class defines a private helper method to handle this task:

// private method


ostream & Student::arr_out(ostream & os) const


{


    int i;


    int lim = scores.size();


    if (lim > 0)


    {


        for (i = 0; i < lim; i++)


        {


            os << scores[i] << " ";


            if (i % 5 == 4)


                os << endl;


        }


        if (i % 5 != 0)


            os << endl;


    }


    else


        os << " empty array ";


    return os;


}

Using a helper like this gathers the messy details together in one place and makes the coding of the friend function neater:

// use string version of operator<<()


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


{


    os << "Scores for " << stu.name << ":\n";


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


    return os;


}

The helper function could also act as a building block for other user-level output functions, should you choose to provide them.

Listing 14.2 shows the class methods file for the Student class. It includes methods that allow you to use the [] operator to access individual scores in a Student object.

Listing 14.2. studentc.cpp


// studentc.cpp -- Student class using containment


#include "studentc.h"


using std::ostream;


using std::endl;


using std::istream;


using std::string;



//public methods


double Student::Average() const


{


    if (scores.size() > 0)


        return scores.sum()/scores.size();


    else


        return 0;


}



const string & Student::Name() const


{


    return name;


}



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


{


    return scores[i];         // use valarray::operator[]()


}



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


{


    return scores[i];


}



// private method


ostream & Student::arr_out(ostream & os) const


{


    int i;


    int lim = scores.size();


    if (lim > 0)


    {


        for (i = 0; i < lim; i++)


        {


            os << scores[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 >> stu.name;


    return is;


}



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


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


{


    getline(is, stu.name);


    return is;


}



// use string version of operator<<()


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


{


    os << "Scores for " << stu.name << ":\n";


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


    return os;


}


Aside from the private helper method, Listing 14.2 doesn’t require much new code. Using containment allows you to take advantage of the code you or someone else has already written.

Using the New Student Class

Let’s put together a small program to test the new Student class. To keep things simple, it should use an array of just three Student objects, each holding five quiz scores. And it should use an unsophisticated input cycle that doesn’t verify input and that doesn’t let you cut the input process short. Listing 14.3 presents the test program. Be sure to compile it along with studentc.cpp.

Listing 14.3. use_stuc.cpp


// use_stuc.cpp -- using a composite class


// compile with studentc.cpp


#include


#include "studentc.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 Listings 14.1, 14.2, and 14.3:

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.

Private Inheritance

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

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

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

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