1.派生的定义:
把student类,细分成本科生和研究生
本科生和研究生也是学生,都是学生的子类
学生是父类,也叫基类或者超类
所谓派生:相对与父类而言的
所谓的继承:相对子类而言的
class student :
{
public:
bool set (int a);
bool set (string a);
bool read() const;
student();
student(int a, int b);
private:
int age;
string name;
}
//派生出本科生的定义
class undergraduate : public student //使用 : 表示从student类公有派生而来
{
publice:
string coure://新定义一个公有的字符串属性,属于该子类特有
}
class postgraduate: public student //使用 : 表示从student类公有派生而来
{
publice:
string resea://新定义一个公有的字符串属性,属于该子类特有
}
子类会自动继承父类的publice成员,继承来的东西不要重新在子类定义中写入 ---属于派生继承中公有的特性(是我方便理解才这样子说的,非官法说法)
同时子类还有会自己的特性,根据需求写入自己的成员----属于私有特性
但是父类的private子类没办法继承,为了决绝这个问题---引入protect类
在没有派生继承情况下,protect和private功能是一样的,都不允许外部直接访问,但是pretect允许子类继承。因此需要被子类继承的私有可以放在protect类中
1116

被折叠的 条评论
为什么被折叠?



