さまざまな学位(現在は物理学と化学)の試験結果を保存するコードを書いています。
次のような抽象基本クラスstudent
があります。
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
};
次に、物理学の派生クラスがあります(化学のクラスも同様になります)。
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
}
};
私のメインでは、その中にすべての学生(物理学と化学)を保存できるマップが必要です。物理学または化学のいずれかへの基本クラスポインタを持つキーとしての学生ID。学生は、私が推測しているように、行く方法です。
次のコードを試しました。
map<int,student*> allstudents;
physics S1(sName,sID);
physics* S2 = &S1;
allstudents.insert(pair<int,student*>(sID,S2));
しかし、これはうまくいきませんでした。何が何を指しているのか、少し混乱していると思います。マップでこれを行うこともできますか?情報を保存する場合、「片付け」も必要ですか。こちらです?ご協力いただきありがとうございます。