Elementos heredados C++

i have two c++ classes, one which inherits an abstract base class for a student database. The base class is the record which contains all the student information (name, id vector of courses & marks):

class student{
protected:
    string fName,sName;
    int id;
    vector<string> cname;
    vector<double> cmark;
public:
    virtual ~student();
    virtual void addClass(string name, double mark)=0;
};

I Need to be able to access the vector cname and cmark in the addCourse function in the below class

class degree : public student{
public:
    degree(string f, string s, int i){
        this->setName(f,s); 
        this->setID(i);
    }
    ~degree();

    void AddCourse(string name, int mark){

    }

I dont know how to do this without making a set function in the base class like i have done with the degree constructor.

I could just make a set function in the base class but i would rather some method of initializing the inherited elements without using functions, just to make the code less messy, is this possible? i thought about using this->cname but that gave me errors.

preguntado el 05 de mayo de 13 a las 14:05

What error did you get from using this->cname? -

You have access to both vectors in AddCourse. -

If degree inherits from student, you are essentially asserting that a degree is a student. Which doesn't sound right. -

I agree completely, but the project description says it has to be done like this... -

1 Respuestas

I Need to be able to access the vector cname and cmark in the addCourse function in the below class

Just access them, they are protected, so derived classes have access:

void AddCourse(string name, int mark){
  cname.push_back(name);
  cmark.push_back(mark);
}

contestado el 05 de mayo de 13 a las 14:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.