c++ - Object Array of Derived Objects -
i have array of objects derive class basestudent.
basestudent**studentlist = new basestudent*[atoi(listsize.c_str())];
that array populated either derived math, english or history objects. i'm trying print out specific data each object in array , output file.
for (int j=0; j<atoi(listsize.c_str()); j++){ if(studentlist[j]->getmt() == english){ output << studentlist[j]->getfn()<<" "<<studentlist[j]->getln(); output << right << setw(42) << studentlist[j]->getfinal(); // english public function can't call this. } }
i need able access derived classes private member data array of objects.
here's header. can see have setter , getter every protected member data.
#include <iostream> #include <string> using namespace std; #ifndef basestudent_h #define basestudent_h enum majortype {english, history, math}; // ********************************************************************* // base class. other classes (enlish, history, math) inherit // class. // ********************************************************************* class basestudent { public: basestudent(); basestudent(string fn, string ln, string m); string getfn(){return firstname;} string getln(){return lastname;} majortype getmt(){return course;} void setfn(string fn){firstname = fn;} void setln(string ln){lastname = ln;} void setmt(string m); protected: string firstname; string lastname; majortype course; }; // end base class // ********************************************************************* // enlish class. // ********************************************************************* class english: public basestudent { public: english(string fn, string ln, string m, double a, double p, double mt, double f); double finalaverage(); double getattendance(){return attendance;} double getproject(){return project;} double getmidterm(){return midterm;} double getfinal(){return final;} double getfinalaverage(){return finalaverage;} void setattendance(double a){attendance = a;} void setproject(double p){project = p;} void setmidterm(double m){midterm = m;} void setfinal(double f){final = f;} void setfinalaverage(double fa){finalaverage = fa;} protected: double attendance; double project; double midterm; double final; double finalaverage; }; // end english class // ********************************************************************* // history class. // ********************************************************************* class history: public basestudent { public: history(string fn, string ln, string m, double t, double mt, double f); double finalaverage(); double gettermpaper(){return termpaper;} double getmidterm(){return midterm;} double getfinalexam(){return finalexam;} double getfinalaverage(){return finalaverage;} double finalexam(){return finalexam;} void settermpaper(double t){termpaper = t;} void setmidterm(double m){midterm = m;} void setfinalexam(double f){finalexam = f;} void setfinalaverage(double fa){finalaverage = fa;} protected: double termpaper; double midterm; double finalexam; double finalaverage; }; // end history class. // ********************************************************************* // math class. // ********************************************************************* class math: public basestudent { public: math(string fn, string ln, string m, double q1, double q2, double q3, double q4, double q, double t1, double t2, double f); double finalaverage(); double getquiz1(){return quiz1;} double getquiz2(){return quiz2;} double getquiz3(){return quiz3;} double getquiz4(){return quiz4;} double getquiz5(){return quiz5;} double getfinalexam(){return finalexam;} double gettest1(){return test1;} double gettest2(){return test2;} double getquizaverage(){return quizaverage;} double getfinalaverage(){return finalaverage;} void setquiz1(double q){quiz1 = q;} void setquiz2(double q){quiz2 = q;} void setquiz3(double q){quiz3 = q;} void setquiz4(double q){quiz4 = q;} void setquiz5(double q){quiz5 = q;} void settest1(double q){test1 = q;} void settest2(double q){test2 = q;} void setfinalexam(double q){finalexam = q;} void setquizaverage(); void setfinalaverage(double fa){finalaverage = fa;} protected: double quiz1; double quiz2; double quiz3; double quiz4; double quiz5; double test1; double test2; double finalexam; double quizaverage; double finalaverage; }; // end math class. #endif
do need sort of implementation of virtual functions?
here's driver far:
#include<iostream> #include<fstream> #include<string> #include<iomanip> #include"basestudent.h" using namespace std; int main(void) { string listsize; string filein = ""; string fileout = ""; string firstname =""; string lastname =""; string major = ""; string eoldummy; int mquiz1, mquiz2, mquiz3, mquiz4, mquiz5, mtest1, mtest2, mfinalexam; int eattendance, eproject, emidterm, efinalexam; int htermpaper, hmidterm, hfinalexam; ifstream input; ofstream output; do{ input.clear(); cout << "please enter filename: "; cin >> filein; cout << "please enter output name: "; cin >> fileout; input.open(filein); if (!input) cout << "invalid file, please enter again." << endl; } while(!input); input >> listsize; basestudent**studentlist = new basestudent*[atoi(listsize.c_str())]; int = 0; while (!input.eof()) { getline(input, lastname, ','); getline(input, firstname, '\n'); input >> major; if (major == "math") { input >>mquiz1>>mquiz2>>mquiz3>>mquiz4>>mquiz5>>mtest1>>mtest2 >>mfinalexam>>eoldummy; // math constructor call // array += object studentlist[i] = new math(firstname,lastname,major,mquiz1,mquiz2,mquiz3,mquiz4,mquiz5, mtest1,mtest2,mfinalexam); } else if (major == "history"){ input >>htermpaper>>hmidterm>>hfinalexam>>eoldummy; // history constructor call // array += object studentlist[i] = new history(firstname,lastname,major,htermpaper,hmidterm,hfinalexam); } else if(major == "english"){ input >>eattendance>>eproject>>emidterm>>efinalexam>>eoldummy; // english constructor call // array += object studentlist[i] = new english(firstname,lastname,major,eattendance,eproject,emidterm,efinalexam); } i++; } output.open(fileout); output << "student grade summary" << endl; output << "---------------------" << endl << endl; output << "english class "<<endl<<endl; output << "student final final letter"<<endl; output << "name exam avg grade"<<endl; output << "----------------------------------------------------------------"<<endl; (int j=0; j<atoi(listsize.c_str()); j++){ if(studentlist[j]->getmt() == english){ output << studentlist[j]->getfn()<<" "<<studentlist[j]->getln(); output << right << setw(42) << studentlist[j]-> input.close(); output.close(); return 0; }
when take pointer array, need cast using dynamic_cast
appropriate class
e.g.
basestudent *p = somearray[0]; if ( english* penglish = dynamic_cast<english*>(p) ) { // call methods cout << p->finalaverage(); ... } else if ( history* phistory = dynamic_cast<history*>(p) ) { // call methods } else if ( math* pmath = dynamic_cast<math*>(p) ) { // call methods }
btw use vector instead of raw array, more convenient , safer.
std::vector<basestudent*> yourvector;
Comments
Post a Comment