简体   繁体   English

继承的元素C ++

[英]Inherited elements C++

i have two c++ classes, one which inherits an abstract base class for a student database. 我有两个c ++类,其中一个继承了学生数据库的抽象基类。 The base class is the record which contains all the student information (name, id vector of courses & marks): 基类是包含所有学生信息(名称,课程和成绩的ID向量)的记录:

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 我需要能够在下面的类的addCourse函数中访问向量cname和cmark

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. 我不知道如何在不像我使用degree构造函数那样在基类中set函数的情况下执行此操作。

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? 我可以在基类中创建一个set函数,但是我更希望一些不使用函数来初始化继承的元素的方法,只是为了使代码少一点混乱,这可能吗? i thought about using this->cname but that gave me errors. 我考虑过使用this->cname但这给了我错误。

I Need to be able to access the vector cname and cmark in the addCourse function in the below class 我需要能够在下面的类的addCourse函数中访问向量cname和cmark

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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