C++继承笔记

继承的方式:
①、public公有继承:
父类的公有成员和受保护成员在子类中保持原有的访问属性,其私有成员仍为父类私有,在子类中是访问不了的,即使通过子类的共有成员函数也访问不了;
②、private私有继承:
父类的公有成员和受保护的成员在子类中变成了私有成员,其私有成员仍为父类私有, 在子类中是访问不了的,即使通过子类的共有成员函数也访问不了;
③、protected受保护继承:
父类的公有成员和受保护的成员在子类中变成了受保护成员,其私有成员仍为父类私有, 在子类中是访问不了的,即使通过子类的共有成员函数也访问不了;

#include "Student.h"
class CXiaoStudent : public CStudent
{
public:
    int yuwen_score;
    int shuxue_score;
    int english_score;

private:
    int flag_private;

protected:
    int flag_protected;
};

class CZhongStudent : public CXiaoStudent
{
public:
    int wuli_score;
    int huaxue_score;

public:
    int get_flag_1()
    {
        //return flag_private;私有成员子类无法访问
        return flag_protected;
    }
};
穷则独善其身,达则兼济天下……
原文地址:https://www.cnblogs.com/hmy-666/p/14414076.html