| |

VerySource

 Forgot password?
 Register
Search
Author: fei0105

A question about derived classes inheriting the private properties of the parent class

[Copy link]

1

Threads

30

Posts

20

Credits

Newbie

Rank: 1

Credits
20

 China

Post time: 2020-7-15 08:15:01
| Show all posts
When assigning the initial value to the parent class member in the subclass constructor, did you just access the private member of the parent class?
----------------------
Calling the public interface can also call access to private members? Then there is the one that is not accessing private members.
You don't care what the function does, the key is whether it can be called by you.
Reply

Use magic Report

1

Threads

7

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

 Author| Post time: 2020-7-18 11:00:01
| Show all posts
If it is private inheritance, the accessibility of name becomes private, and public inheritance is
    //name's accessibility remains unchanged (but if name is private in the base class, it is not accessible).

I ran the protected attribute of the parent class. The child class inherits privately, and the name in the parent class (still protected) can still be accessed in the child class.
At this time, the name parent class is still protected to the outside, and the subclass inherits it as private. Where is it reflected?
Reply

Use magic Report

0

Threads

3

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

Post time: 2020-7-18 12:00:01
| Show all posts
I ran the protected attribute of the parent class. The child class inherits privately, and the name in the parent class (still protected) can still be accessed in the child class.
At this time, the name parent class is still protected to the outside, and the subclass inherits it as private. Where is it reflected?
============================================
When you have another class inheritance, accessibility is lost.

Ancestor: protected
Parent: private inherits from the ancestor, then everything under protected and public becomes private
Zi: You can no longer directly access things under the ancestors.
Reply

Use magic Report

1

Threads

7

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

 Author| Post time: 2020-7-18 13:30:02
| Show all posts
I tried it just now
Ancestor: if it is public
Parent: private inherits from ancestor
Son: Cannot directly access the parent class object p.name directly accesses the public name under the ancestor, but the compilation is correct
Reply

Use magic Report

1

Threads

7

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

 Author| Post time: 2020-7-18 13:45:01
| Show all posts
In this way, the descendants inherited only change the scope of their own class, but not the scope of the parent class definition..
Reply

Use magic Report

0

Threads

6

Posts

7

Credits

Newbie

Rank: 1

Credits
7

 China

Post time: 2020-7-29 17:15:02
| Show all posts
In this way, the descendants inherited only change the scope of their own class, and not the scope of the parent class definition.
========================
Of course, the access rights of members in the parent class object will not be changed, but the access rights of some members in the subclass object inherited from the parent class will be changed.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list