0
C++: type of inheritance
If you use Public type of inheritance, derived class can access both protected and public. If you use Protected , public and protected become protected in derived class, which derived class still can access both. Then, why is it needed to have both Public and Protected type of inheritance when they perform the same for the derived class?🤔 If all members from the Base class will become Private in the derived class when using the Private type, which all members copied from the Base class cannot be accessed, then what is the meaning of using private type?🤔 Questions from a beginner
3 Respostas
+ 2
(1) Assume we have a variable "var" in the class.
If it's public, then the code below is valid:
cout << class.var;
But if it's protected, it is no longer valid.
Public modifier allows us to use and modify members outside of base class and derived class while protected and private don't.
(2) Private modifier prevents us to use or modify variable outside of it's class and derived class. It does not only provide a great encapsulation but also help us to debug easily. Imagine you have a code with hundreds or even thousands of lines, and a class member is modified by accident. Now you need to debug in hundreds of lines if you declared it public, while using private you only need to check a few lines in the class.
0
thx~