has-a关系——包含对象成员的类

#ifndef _STUDENT_H_

#define _STUDENT_H_



#include <iostream>

#include <string>

#include <valarray>



class Student

{

private:

    typedef std::valarray<double> ArrayDb;

    std::string name;   //contained object

    ArrayDb scores;     //contained object



    //private method for scores output

    std::ostream & arr_out(std::ostream & os) const;



public:

    Student()                                           : name("Null Student"), scores()    {}

    explicit Student(const std::string & s)             : name(s), scores()                 {}

    explicit Student(int n)                             : name("Nully"), scores(n)          {}

    Student(const std::string & s, int n)               : name(s), scores(n)                {}

    Student(const std::string & s, const ArrayDb & a)   : name(s), scores(a)                {}

    Student(const char * str, const double * pd, int n) : name(str), scores(pd, n)          {}

    ~Student() {}



    double Average(void) const;

    const std::string & Name(void) const;



    double & operator[](int i);

    const double & operator[](int i) const;



    //friends

    //inout

    friend std::istream & operator>>(std::istream & is, Student & stu); //1 word

    friend std::istream & getline(std::istream & is, Student & stu);    //1 line

    //output

    friend std::ostream & operator<<(std::ostream & os, const Student & stu);

};



#endif
#include "student.h"

using std::ostream;

using std::istream;

using std::endl;

using std::string;



//public methods

double Student::Average(void) const

{

    if(scores.size() > 0)

    {

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

    }

    else

    {



        return 0;

    }

}



const string & Student::Name(void) const

{

    return name;

}



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

{

    return scores[i];

}



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

{

    return scores[i];

}



//private method

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

{

    int i;

    if(scores.size() > 0)

    {

        for(i = 0; i < scores.size(); i++)

        {

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

            if(i % 5 == 4)

            {

                os << endl;

            }

        }

        if(i % 5 != 0)

        {

            os << endl;

        }

    }

    else

    {

        os << "empty array";

    }

    return os;

}



//friend methods

istream & operator>>(istream & is, Student & stu)

{

    is >> stu.name;

    return is;

}



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

{

    getline(is, stu.name);

    return is;

}



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

{

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

    stu.arr_out(os);

    return os;

}
#include <iostream>

#include "student.h"

using std::cin;

using std::cout;

using std::endl;



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;

    }

}



const int pupils = 3;

const int quizzes = 5;



int main(void)

{

    Student ada[pupils] = {Student(quizzes), Student(quizzes), Student(quizzes)};



    for(int i = 0; i < pupils; i++)

    {

        set(ada[i], quizzes);

    }

    cout << "\nStudent List:\n";

    for(int i = 0; i < pupils; i++)

    {

        cout << ada[i].Name() << endl;

    }

    cout << "\nResults:";

    for(int i = 0; i < pupils; i++)

    {

        cout << endl << ada[i];

        cout << "average: " << ada[i].Average() << endl;

    }

    cout << "Done.\n";



    return 0;

}

 

你可能感兴趣的:(对象)