I'm writing code to store exam results for different degrees (physics & chemistry currently).
I have an abstract base class student
which is as follows:
class student{
public:
virtual ~student(){}
//virtual function to add course + mark
virtual void addresult(std::string cName,int cCode,double cMark)=0;
//virtual function to print data
virtual void printinfo()=0; //to screen
virtual void outputinfo(std::string outputfilename)=0; //to file
};
I then have a derived class for physics (and will have a similar one for chemistry):
class physics : public student{
protected:
std::string studentname;
int studentID;
std::map<int,course> courses; //map to store course marks
public:
//constructors/destructors
void addresult(std::string cName,int cCode,double cMark){course temp(cName,cCode,cMark);courses[cCode]= temp;}
void printinfo(){
//function to output information to screen
}
void outputinfo(std::string outputfilename){
//function to write student profile to file
}
};
In my main, I would then like to have a map that can store all students within it (physics and chemistry). The student ID as the key with a base class pointer to either a physics or chem. student is, I'm guessing, the way to go.
I tried the following code:
map<int,student*> allstudents;
physics S1(sName,sID);
physics* S2 = &S1;
allstudents.insert(pair<int,student*>(sID,S2));
but this didn't work. I think I'm getting slightly confused with what should be pointing to what. Can you even do this with maps? Should there also be any 'clearing up' required if I store the info. this way? Thanks for your help.