Hi,
I'm doing a project which requires me to make a database to hold student information and course marks. I have an abstract base class called student and have been told i need a pure virtual function to add courses (this is where the problem came in as it was working perfectly before i realised this).I also have a class called Course which simply holds course name, code and % mark.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class student //abstract class for student
{
friend ostream & operator<<(ostream &os, const student &stud);
protected:
string StuName;
string StuNum; //Used string for student number as no mathematical operations can be done
int Year;
vector<Course*> StuCourse; //Vector to hold individual modules
string programme;
double avgrade;
public:
virtual ~student(){}
void calcav();// calculates average grade over the modules
virtualvoid Addcourse(string CNamein,string CCodein,double CMarkin)=0;//required for adding course from fresh
virtualvoid Addcourse(Course Cin, double Markin)=0; //required for adding course using predefined core courses
So i have a derived class for a physics student:
1 2 3 4 5 6 7 8 9 10
class Phys : public student // Class for students on pure physics
{
protected:
public:
Phys(string Namein, string Numin, int Yearin); //When this function runs it prompts for grades in each module and adds courses into StuCourse using the Addcourse function
~Phys(){}
void Addcourse(string CNamein, string CCodein, double CMarkin); //Adds course with input of name, code and mark
void Addcourse(Course Cin, double Markin){StuCourse.push_back(new Course(Cin.getname(),Cin.getcode(),Markin));} //adds course with input of core module and mark
};
I am then told that I must use a map function to hold the student records: typedef map<string,student*> Databasemap; //Defines map for student classes with string (student number) as key
So to enter a physics student and output to screen (overloaded output function works fine so I haven't included the details):
The problem is that the output doesn't output any of the information that should be stored in the vector StuCourse. I have messed around with things and have found that until the end of the "Phys" constructor StuCourse has the correct size but that once it has been transferred to "Database" the size has gone to zero. I'm guessing this is caused by the referencing/dereferencing but I have no idea how else to use the map. I have to use the pointer to student inside the map or else it is instantiated in the Iterator (not allowed for the abstract class).
Can anyone give me any suggestions?
If there is any further information that i need to provide then please tell me, There is a lot more to the program but I hope i have included everything I needed to.
I think the problem is this line Database[Numin]= &(Phys(Namein,Numin,Yearin));
Phys(Namein,Numin,Yearin) will just create a temporary Phys object that will only exist on that line. If you want to create an object that stays alive until you delete it you could create it with new. Database[Numin]= new Phys(Namein,Numin,Yearin);
Yes thank you very much, that works. I hadn't thought to use it because earlier my map function didn't need to be a pointer and Database[Numin}=Phys(Namein,Numin,Yearin);
worked perfectly well.
Why does new remove the need for the &? Before it said that there was no operator to convert from Phys to student*, I can't see why this case is different?