| |

VerySource

 Forgot password?
 Register
Search
Author: fei0105

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

[Copy link]

1

Threads

7

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

 Author| Post time: 2020-7-12 21:45:01
| Show all posts
julinesI understand what you mean. The inherited private property is just to be able to use the subclass object as the parent class object to ensure the integrity of the members. Can it not be used in other ways? Do you call your own inherited name line in the subclass member function
cout<<"Teacher construct: "<<name<<" in "------Can the name inside be subclasses (inherited)
       <<school<<endl;
You said that the name attribute is an attribute owned by both teachers and people. You can't say who is who.
It’s still a little vague. When subclasses are instantiated, should they still have their own name attribute?
Reply

Use magic Report

0

Threads

6

Posts

7

Credits

Newbie

Rank: 1

Credits
7

 China

Post time: 2020-7-13 08:15:01
| Show all posts
Just call your own, you don’t have to use the parent
================================
Does the landlord want to say that since the subclass already has this member defined in the parent class, let me access the subclass, and what private inheritance can't access the private members defined in the parent class, mysterious?
Then I ask you: Why do you want private inheritance? Do you know what private inheritance means? Since you want to access the private members defined in the parent class in the subclass, why not use public inheritance?

Public inheritance is an is-a relationship. Each subclass object is a parent class object. All operations on the parent class object are suitable for subclass objects.
Private inheritance means implemented-in-terms-of (implemented according to something).
If you let class D inherit class B in private form, your intention is to adopt
Some of the features already prepared in class B (or D objects are implemented based on B objects), not because of
There is any conceptual relationship between the B object and the D object.
In this case, according to the idea of ​​encapsulation, of course, you should not be allowed to access a private member defined in the parent class.
Private inheritance means that only the implementation part is inherited, and any interfaces that need to be announced must be re-transmitted.
Reply

Use magic Report

0

Threads

27

Posts

22

Credits

Newbie

Rank: 1

Credits
22

 China

Post time: 2020-7-13 10:30:02
| Show all posts
The instantiation of the subclass requires the name attribute. In fact, this value is used by the base class constructor.
Reply

Use magic Report

0

Threads

27

Posts

22

Credits

Newbie

Rank: 1

Credits
22

 China

Post time: 2020-7-13 11:15:01
| Show all posts
To put it bluntly, in the object of the subclass, the parent class is still a logical and physical whole.
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-7-13 14:00:02
| Show all posts
If it is a private parent class member, it is impossible for subclasses to inherit it.
There are several ways to call members, through objects, through class names, through friends.
Think about it, since it is used directly, it is not necessarily a member of the subclass book, right?
Reply

Use magic Report

0

Threads

3

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-7-14 00:30:01
| Show all posts
After reading so many answers upstairs, I also have the same doubts as the landlord:

Since public is used to inherit the parent class, the child class inherits all the members of the parent class (including private, public, protected), but cannot access the private members inherited from the parent class (because it is private to the parent class)
, If you want to access it, you can access it indirectly through the Set and Get access functions.

In other words, although the subclass has this private member inherited from the parent class, it cannot be used directly! ! (-_-b do not know if it is understood or not)

My question now is: What is the initial value assigned to the parent class member in the subclass constructor?
Reply

Use magic Report

0

Threads

3

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-7-14 07:00:02
| Show all posts
The subclass inherits the attributes of the parent class but cannot use it.
Because the authors of the parent and child categories are very likely not the same person. The author of the subclass may be unfamiliar with the private properties of the parent class. If you operate on it, it may be dangerous, but the subclass can operate on the private properties by calling the parent class encapsulated methods (such as get, set). Be aware that encapsulation is one of the object-oriented fame attributes.
Reply

Use magic Report

0

Threads

3

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-7-14 09:45:01
| Show all posts
Halo, did not explain the problem I said above
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?
Reply

Use magic Report

0

Threads

6

Posts

7

Credits

Newbie

Rank: 1

Credits
7

 United States

Post time: 2020-7-14 15:00:01
| Show all posts
//Assume that there are classes Base and Drived as follows:
class Base
{
private:
    char name[10];
};
class Drived:public Base
{
    //Whether it is public inheritance or private inheritance, the memory layout of the Drived object
    //The same as Drived_likeness below, the difference is accessibility and operation on name.
    //Because the name here comes from Base, if name is declared private in Base,
    //So, although there is a position of name in the Drived object, it cannot be accessed directly.
    //The operation depends on the non-private functions of the base class.
    //If name is declared as protected or public in Base, functions of this class can be
    //Accessed. If it is private inheritance, the accessibility of name becomes private, and public inheritance is
    //name accessibility is unchanged (but if name is private in the base class, it is not accessible).
    //Wrong view 1: If it is a private parent class member, it is impossible for subclasses to inherit it.
    //This seems to be saying that there is no name position in Drived, so wrong.
    //Wrong view 2: After subclass inheritance is equivalent to a reference, the definition of parent class is still used when calling
    //How did you get the reference?
    //Emphasize again: the subclass object is also a whole, including the inherited part from the parent class (such as the name in this example)
    //Add a new part with yourself (such as age in this example)
private:
    int age;
};
class Drived_likeness
{
private:
    char name[10];
    int age;
};
Reply

Use magic Report

0

Threads

3

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-7-14 20:00: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?
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