我正在编写代码以存储不同程度的考试结果(物理学&化学当前)。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