关于继承的成员的问题

tianshilei1992 tianshilei1992 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
子类不能直接使用父类的成员吗?比如初始化成员变量?

class Student
{
public:
Student() : name("Student"), sex('M'), age(18), num("1") {}
Student(string n, char s, int a, string no) : name(n), sex(s), age(a), num(no) {}
protected:
int age;
string name;
char sex;
string num;
};

class CollegeStudent : public Student
{
public:
CollegeStudent(string n, char s, int a, string no) : name("CollegeStudent") {}
};


编译器在子类那name那里提示错误,说是“name不是类CollegeStudent的非静态数据成员或基类”,这是咋回事?
我的理解是,子类继承了基类的非protected变量,作为自己的成员变量来用,可以这样不?
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
tianshilei1992
C++ 语言 65,214 成员 250,514 内容