| |

VerySource

 Forgot password?
 Register
Search
View: 1573|Reply: 10

Think of inheritance and derivation ...

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-6 16:50:01
| Show all posts |Read mode
As we all know, inheritance and derivation are very important characteristics in c ++.
In our college class, the teacher told us that if the derived class wanted direct access
Private members of the base class, then protected can be used, needless to say


But I thought about it and thought, if a class is designed for derivation, is it right?
All its private members are set to protected to cough? ? ?

If this is the case, then I don't think private will appear in MFC.

Maybe my undergraduate is too naive, but since protected can serve as "private"
And "public", so why set private access control? ? ? ?


I hope IT seniors-brothers and sisters to help me a bit :)
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-6 20:30:01
| Show all posts
Ha ha, the private nature of private guarantees that it can only be accessed by the class itself and friends, not even its derived classes. If you use protected instead, then it is not really private, because its derived classes can also access it, and the encapsulation is actually incomplete.
There are indeed many members of a base class that are not accessible to derived classes. So there must be private.
Reply

Use magic Report

0

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-6 20:36:01
| Show all posts
If we use the application (that is, write code) perspective, we don't have to ask why C ++ provides so many features, we just need to know how we can use these features.
From my experience, the use of three access rights can be like this:
1. Regardless of whether this class will be inherited, all data is set to private using the "data encapsulation" principle.
2. If you want to use these data in the inherited class, you can define protected member functions in the base class to access or modify private data members. The inherited classes do not directly access these data members, but access through these member functions.
3. It is best not to define interfaces (public member functions) to directly access data members, and to implement class and external communication through well-defined interfaces.
Reply

Use magic Report

0

Threads

11

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

Post time: 2020-1-6 20:51:01
| Show all posts
Not every private data is suitable for direct operation of derived classes-derived classes are not necessarily reliable. If you directly modify data members arbitrarily, the state legitimacy of the class may be broken.
Reply

Use magic Report

0

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Hong Kong

Post time: 2020-1-7 09:48:01
| Show all posts
The difference between private and protected is more reflected in class design,
Private is not public and belongs to the characteristics of the class itself
Protected means that the class authorizes a certain feature of itself to other classes.

From an implementation point of view, private shows more implementation inheritance, that is, the child class inherits the functions implemented by the parent class and cannot change its functions.
Reply

Use magic Report

0

Threads

36

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-7 10:42:01
| Show all posts
It is recommended that the landlord look at the design pattern.For the single model, the semantics of using protected and private are different.
Protect emphasizes that this monomer can be used to inherit from other monomers, and the semantics of private is that this monomer is only used for this class, and it cannot be inherited from other classes.
So the landlord has to learn a lot, and the road is slowly repairing.
Reply

Use magic Report

3

Threads

10

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-1-7 22:18:01
| Show all posts
The landlord can understand so

Private property is the essential difference between things
And what protects is the particularity of the objects in things
Reply

Use magic Report

0

Threads

15

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-8 21:54:02
| Show all posts
In the old-fashioned sense, existence is reasonable, and when it is not reasonable (when no one is using it in the end), there is no meaning of existence.

All said upstairs, these properties are just the needs of the design, there is no need to dig into the horns in this regard
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-9 14:00:02
| Show all posts
Private variables and methods are used by those who write the class, protected is used by those who extend the class, and public is used by those who use the class. protected breaks encapsulation, composition and inheritance, giving priority to composition,
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-15 20:18:01
| Show all posts
This design definitely has his reasoning:
protected members:
     The class it belongs to, and more importantly, the member functions of any derived class of the class it belongs to can access it. If you think that you may use a class as a base class for other classes in the future, any derived class may need Accessed data members should be set to protected. Instead of private, this ensures that this class is "suitable for inheritance."

Dangers of protected members:
     Let's say you write a class library and intend to distribute it as public. Any programmer who bought this library can simply derive other classes from it to access the data members of the protected class, which makes Protect members' security much lower than private ones,
To prevent data corruption, derived classes are usually forced to use only public functions of the base class to access the data.

in conclusion:
     The use of protected indicators makes program design easy, but when you really use it, you have to weigh the rights and disadvantages!

Ha ha!!
Reply

Use magic Report

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

Points Rules

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

Quick Reply To Top Return to the list