简体   繁体   中英

Inherited elements 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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM