我正在編寫代碼以存儲不同程度的考試結果(物理學&化學當前)。std ::基類指針映射
我有一個抽象基類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));
但這並沒有工作。我想我對於什麼應該指向什麼有些困惑。你甚至可以用地圖做這個嗎? 如果我存儲信息,是否還需要「清理」。這條路? 感謝您的幫助。
公共繼承模仿IS-A關係,但物理學是科學而不是學生。 – sbi 2012-04-15 10:05:24