继承方式
- 继承方式分为
public 、protected 、private 三种 - 三种继承方式都不能继承到父类的private属性
- public继承:父类的public&&protected属性继承到子类不发生变化,仍然是public&&protected
- protected继承:父类的public&&protected属性继承到子类都变成了protected
- private继承:父类的public&&protected属性继承到子类都变成了private
class Father
{
public:
int a;
protected:
int b;
private:
int c;
};
class Son_public : public Father
{
public:
void function()
{
a = 100;
b = 100;
}
};
void test1()
{
Son_public son;
son.a = 100;
}
class Son_protected : protected Father
{
public:
void function()
{
a = 100;
b = 100;
}
};
void test2()
{
Son_protected son;
}
class Son_private : private Father
{
public:
void function()
{
a = 100;
b = 100;
}
};
void test3()
{
Son_private son;
}
class Son_private_son : public Son_private
{
public:
void function()
{
}
};
继承方式的大致关系如图所示
|