#ifndef _STUDENT_H_ #define _STUDENT_H_ #include <iostream> #include <string> #include <valarray> class Student : private std::string, private std::valarray<double> { private: typedef std::valarray<double> ArrayDb; //private method for scores output std::ostream & arr_out(std::ostream & os) const; public: Student() : std::string("Null Student"), ArrayDb() {} explicit Student(const std::string & s) : std::string(s), ArrayDb() {} explicit Student(int n) : std::string("Nully"), ArrayDb(n) {} Student(const std::string & s, int n) : std::string(s), ArrayDb(n) {} Student(const std::string & s, const ArrayDb & a) : std::string(s), ArrayDb(a) {} Student(const char * str, const double * pd, int n) : std::string(str), ArrayDb(pd, n) {} ~Student() {} double Average(void) const; const std::string & Name(void) const; //double & operator[](int i); //const double & operator[](int i) const; using std::valarray<double>::operator[]; //使用using重新定义基类方法在私有继承中的访问权限 //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(ArrayDb::size() > 0) { return ArrayDb::sum() / ArrayDb::size(); } else { return 0; } } const string & Student::Name(void) const { return (const string &)*this; } /* double & Student::operator[](int i) { return ArrayDb::operator[](i); } const double & Student::operator[](int i) const { return ArrayDb::operator[](i); } */ //private method ostream & Student::arr_out(ostream & os) const { int i; if(ArrayDb::size() > 0) { for(i = 0; i < ArrayDb::size(); i++) { //os << ArrayDb::operator[](i) << " "; os << ((const ArrayDb &)*this)[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 >> (string &)stu; return is; } istream & getline(istream & is, Student & stu) { getline(is, (string &)stu); return is; } ostream & operator<<(ostream & os, const Student & stu) { os << "Scores for " << (const string &)stu << ": "; 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: "; for(int i = 0; i < n; i++) { cin >> sa[i]; } while(cin.get() != ' ') { 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 << " Student List: "; for(int i = 0; i < pupils; i++) { cout << ada[i].Name() << endl; } cout << " Results:"; for(int i = 0; i < pupils; i++) { cout << endl << ada[i]; cout << "average: " << ada[i].Average() << endl; } cout << "Done. "; return 0; }