个人体会:继承并不是基类的东西全是你的,当出现多次间接继承的时候,这个问题就凸显出来了
c++中private,public,protected的访问控制
第一:private,public,protected方法的访问范围.(public继承下)
private: 只能由该类中的函数、其友元函数访问,不能被任何其他访问,该类的对象也不能访问.
protected: 可以被该类中的函数、子类的函数、以及其友元函数访问,但不能被该类的对象访问
public: 可以被该类中的函数、子类的函数、其友元函数访问,也可以由该类的对象访问
注:友元函数包括两种:设为友元的全局函数,设为友元类中的成员函数
第二:类的继承后方法属性变化:
使用private继承,父类的所有方法在子类中变为private;
使用protected继承,父类的protected和public方法在子类中变为protected,private方法不变;
使用public继承,父类中的方法属性不发生改变;
1.public继承方式下
- #include <iostream>
- using namespace std;
- class Animal
- {
- public:
- Animal(){}
- void eat()
- {
- cout << "eat/n";
- }
- protected:
- void play()
- {
- cout << "play/n";
- }
- private:
- void drink()
- {
- cout << "drink/n";
- }
- };
- class Giraffe: public Animal
- {
- public:
- Giraffe(){}
- void StrechNeck()
- {
- cout << "Strech neck /n";
- }
- void take()
- {
- eat(); // ok,公有继承下,基类的公有成员对派生类可见
- // drink(); // error,公有继承下,基类的私有成员对派生类不可见,也就是说,派生类不能访问基类私有成员
- play(); // ok,公有继承下,基类的保护成员对派生类可见
- }
- };
- int main()
- {
- Giraffe gir;
- // 以下三条语句说明基类成员对派生类对象的可见性
- gir.eat(); // ok,公有继承下,基类的公有成员对派生类对象可见
- // gir.play(); // error,公有继承下,基类的保护成员对派生类对象不可见
- // gir.drink(); // error,公有继承下,基类的私有成员对派生类对象不可见
- // 下面一条语句说明基类成员对派生类的可见性
- // 派生类(即对派生类的成员函数,派生类的友元函数和有元类可见性,和派生类对象无关)
- gir.take();
- gir.StrechNeck();
- return 0;
- }
2.private继承方式下
- #include <iostream>
- using namespace std;
- class Animal
- {
- public:
- Animal(){}
- void eat()
- {
- cout << "eat/n";
- }
- protected:
- void play()
- {
- cout << "play/n";
- }
- private:
- void drink()
- {
- cout << "drink/n";
- }
- };
- class Giraffe: private Animal
- {
- public:
- Giraffe(){}
- void StrechNeck()
- {
- cout << "Strech neck /n";
- }
- void take()
- {
- eat(); // ok,私有继承下,基类的公有成员对派生类可见
- // drink(); // error,私有继承下,基类的私有成员对派生类不可见,也就是说,派生类不能访问基类私有成员
- play(); // ok,私有继承下,基类的保护成员对派生类可见
- }
- };
- int main()
- {
- Giraffe gir;
- // 以下三条语句说明基类成员对派生类对象的可见性
- // gir.eat(); // error,私有继承下,基类的公有成员对派生类对象不可见
- // gir.play(); // error,私有继承下,基类的保护成员对派生类对象不可见
- // gir.drink(); // error,私有继承下,基类的私有成员对派生类对象不可见
- // 下面一条语句说明基类成员对派生类的可见性
- // 派生类(即对派生类的成员函数,派生类的友元函数和有元类可见性,和派生类对象无关)
- gir.take();
- gir.StrechNeck();
- return 0;
- }
3.protected继承方式下
- #include <iostream>
- using namespace std;
- class Animal
- {
- public:
- Animal(){}
- void eat()
- {
- cout << "eat/n";
- }
- protected:
- void play()
- {
- cout << "play/n";
- }
- private:
- void drink()
- {
- cout << "drink/n";
- }
- };
- class Giraffe: protected Animal
- {
- public:
- Giraffe(){}
- void StrechNeck()
- {
- cout << "Strech neck /n";
- }
- void take()
- {
- eat(); // ok,保护继承下,基类的公有成员对派生类可见
- // drink(); // error,保护继承下,基类的私有成员对派生类不可见,也就是说,派生类不能访问基类私有成员
- play(); // ok,保护继承下,基类的保护成员对派生类可见
- }
- };
- int main()
- {
- Giraffe gir;
- // 以下三条语句说明基类成员对派生类对象的可见性
- // gir.eat(); // error,保护继承下,基类的公有成员对派生类对象不可见
- // gir.play(); // error,保护继承下,基类的保护成员对派生类对象不可见
- // gir.drink(); // error,保护继承下,基类的私有成员对派生类对象不可见
- // 下面一条语句说明基类成员对派生类的可见性
- // 派生类(即对派生类的成员函数,派生类的友元函数和有元类可见性,和派生类对象无关)
- gir.take();
- gir.StrechNeck();
- return 0;
- }