// 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

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