Tag:class c++ inherit
/**copyright (c) 2014, College of Computer and Control engineering, Yantai University *all rights reserved.* file name: d.cpp* Author: Zhang Wanhua * Completion Date: June 3, 2015 * version number: v1.0*//** " Item 2-The list of students saved with a file "* file Score.dat is a number of students whose names and C + + classes, high scores and English grades are saved. * * #include <fstream> #include <iostream> #include <string> #include <cstdlib>using namespace std;//defines student class Student{public:student () {}; ~student (); Double Get_total (); static int get_stu_num (); Static double Get_total_sum (); Friend istream& operator>> (IStream &in, Student &s); You can define the input function instead of friend ostream& operator<< (ostream &out, Student &s); You can define the display function to replace the bool Pass ();p rivate:string name; Double CPP; Double math; Double 中文版; Double total; static int stu_num; Number of pupils, handling static members of the class as appropriate static double total_sum; Student total score and};int Student::stu_num = 0;double student::total_sum = 0; Student::~student () {total_sum-=total; stu_num--;} Double Student::get_total () {return total;} int Student::get_stu_num () {return stu_nUm;} Double Student::get_total_sum () {return total_sum;} istream& operator>> (IStream &in, Student &s) {in>>s.name>>s.cpp>>s.math>> S.english; S.total=s.cpp+s.math+s.english; student::stu_num++; In the process of reading data, the number of students and total score and student::total_sum+=s.total are recorded with static members. return in;} Ostream &operator<< (ostream &out, Student &s) {out<<s.name<< "\ t"; out<<s.cpp<< "\ t"; out<<s.math<< "\ t"; out<<s.english<< "\ t"; out<<s.total; return out;} Returns whether all courses have been all over the bool Student::p () {return cpp>=60 && math>=60 && english>=60;} int main () {Student stud[200],t;//stud[200] is an array of objects that holds the data string sname; Double Total_avg; int i=0; Reads the data in the file into an array of objects Ifstream infile ("Score.txt", ios::in); Open file as input if (!infile)//test successfully opened {cerr<< "open error!" <<endl; Exit (1); } while (!infile.eof ()) {infile>>stud[i++]; In the reading data, the number of people and other information is automatically recorded into the static member, see the implementation of operator overloading} infile.close (); The total score is averaged and output if (Student::get_stu_num () >0) {total_avg = Student::get_total_sum ()/Student::get_stu_num (); Ofstream outfile ("Pass_score.dat", ios::out); if (!outfile) {cerr<< "Open error!" <<endl; Exit (1); } for (i=0; I<student::get_stu_num (); i++) {if (Stud[i].get_total () >total_avg&&stud[ I].pass ()) {outfile<<stud[i]<<endl; }} outfile.close (); cout<< "Please see the list in the file Pass_score.dat:" <<endl; } return 0;}
14th Week * "Item 2-list of students saved with a file"