| |

VerySource

 Forgot password?
 Register
Search
View: 25|Reply: 25

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

Post time: 2020-3-23 21:00:02
| Show all posts |Read mode
father
class People {
public:
   People (char * str); // Constructor
   ~ People (); // Destructor
protected:
   char * name;
};
#endif
Parent class implementation:
{
People :: People (char * str) {
   name = new char [strlen (str) +1];
   strcpy (name, str);
   cout << "People construct:" << name << endl;
}
// The implementation of the destructor
People :: ~ People () {
   cout << "People destroy:" << name << endl;
   delete [] name;
} People :: People (char * str) {
   name = new char [strlen (str) +1];
   strcpy (name, str);
   cout << "People construct:" << name << endl;
}
// The implementation of the destructor
People :: ~ People () {
   cout << "People destroy:" << name << endl;
   delete [] name;
}

Derived class:
class Teacher: public People {
public:
   Teacher (char * str, char * sch); // Constructor
   ~ Teacher (); // Destructor
protected:
   char * school;
};
Derived implementation
Teacher :: Teacher (char * str, char * sch)
: People (str) // Call the constructor of the base class
{school = new char [strlen (sch) +1];
   strcpy (school, sch);
   cout << "Teacher construct:" << name << "in" ------ 1
       << school << endl;
}
Teacher :: ~ Teacher () {
   cout << "Teacher destroy:" << name << "in" ----------- 2
       << school << endl;
   delete [] school;}


The name of the parent class member must be a protected type in order to use name in the derived and destructor functions 1, 2 and 1. Change to a private type and it will not be accessible in subclasses. However, the subclass inherits its own name attribute when inherited. Why can't it be called? Is the name of the parent class or a child class called at 1, 2?
Reply

Use magic Report

0

Threads

6

Posts

6

Credits

Newbie

Rank: 1

Credits
6

 China

Post time: 2020-7-4 06:15:01
| Show all posts
Private members can only be accessed by members and friends of the base class.
Subclasses can only inherit the public and protected members of the parent class, but they will not inherit private members.
Reply

Use magic Report

1

Threads

23

Posts

19

Credits

Newbie

Rank: 1

Credits
19

 China

Post time: 2020-7-4 20:00:02
| Show all posts
name should be a subclass
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 Unknown

Post time: 2020-7-5 07:30:01
| Show all posts
The first floor is right, the private subclass cannot be called at all, the member functions of the initial base class can be called, and then can only be called in the format of ::
Reply

Use magic Report

0

Threads

27

Posts

22

Credits

Newbie

Rank: 1

Credits
22

 China

Post time: 2020-7-8 14:30:01
| Show all posts
First, the subclass will inherit all data members of the parent class. Public inheritance thinks that B is an A, that is, the teacher is a people, so the name attribute is an attribute owned by both teachers and people, and it cannot be said who is who.

But the private members of the parent class have no right to question, and the meaning of "private" lies in their own possession. In this case, why should private members be inherited? C++ has semantic support for converting a subclass into a parent class. If the subclass loses a certain data member of the parent class, how can it be successfully converted?
Reply

Use magic Report

1

Threads

7

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

 Author| Post time: 2020-7-11 22:30:02
| Show all posts
The subclass inherits all members of the parent class, including the parent class. What is said in the book should be determined
The meaning upstairs
Although there is a private member name of the inherited parent class in the subclass, it cannot be used because the parent class owns it alone? That is, you can't use the inherited attributes, what is the role of inheritance?" Not really understand
Reply

Use magic Report

1

Threads

7

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

 Author| Post time: 2020-7-11 23:15:01
| Show all posts
Friends above may not understand what I mean
Since the subclass inherits the private members of the parent class, you can call your own. You don't have to use the parent class, so you don't have to worry about whether the parent class is private or what type of protection it does.
Reply

Use magic Report

0

Threads

27

Posts

22

Credits

Newbie

Rank: 1

Credits
22

 China

Post time: 2020-7-12 06:15:02
| Show all posts
Haha, I already said:
In this case, why should private members be inherited? C++ semantics support converting a subclass into a parent class. If a subclass loses a data member of the parent class, how can it be successfully converted?
Reply

Use magic Report

1

Threads

29

Posts

19

Credits

Newbie

Rank: 1

Credits
19

 China

Post time: 2020-7-12 15:15:02
| Show all posts
The subclass inherits all members of the parent class.
But it does not mean that all members of the parent class can be accessed.
The private property is restricted and cannot be accessed.

If you can't access it, it doesn't mean it has no effect. For example, some members of the private class hide him, do these hidden members just
Doesn't it make sense? Others only need to access the public interface, and the public interface can access private members, which is enough.
Reply

Use magic Report

0

Threads

6

Posts

7

Credits

Newbie

Rank: 1

Credits
7

 China

Post time: 2020-7-12 18:45:01
| Show all posts
Friends above may not understand what I mean
Since the subclass inherits the private members of the parent class, you can call your own. You don't have to use the parent class, so you don't have to worry about whether the parent class is private or what type of protection it does.
========================================
It seems that the landlord does not understand what the subclass object after inheritance looks like.
The subclass object is also a whole, including the inherited part from the parent class and the new part. The subclass object calls the name defined in the parent class instead of going to another place to find a parent class object and then calling its name, but itself. There is a name inherited from the parent class. However, if the name is private when it is defined in the parent class, although the name is in the subclass of private inheritance, it cannot be accessed by conventional methods. Call your own without having to use the parent "?"
If the subclass object needs to access the member defined in the parent class, it means that private inheritance should not be used or that the member should not be defined as private in 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