The simplest object-oriented problem,
On the Internet, even some textbooks provide various answers.
The relationship between private members and inheritance.
Today, my classmates and I have argued on this issue online,
We finally found that both of them were correct.
Haha.
Record this meaningful conversation.
I'm a pig "......
2009-07-17 21:46:08 WL
I often want to have a technical expert who keeps teaching you
2009-07-17 21:46:21 WL
You must be more advanced.
2009-07-17 21:46:36 WL
Just like what his mother got from the sunflower collection
2009-07-17 21:58:49 WL
If you say that a method of the base class is private, can you implement its inheritance method definition in the subclass and then change it to public?
2009-07-17 22:26:08 WL
Are you there?
22:43:27 success _ ● pig
No
22:43:32 success _ ● pig
You're an idiot.
22:43:41 success _ ● pig
The private of the base class is changed to the public of the subclass.
22:43:48 success _ ● pig
What is the use of C ++ private?
2009-07-17 22:45:17 WL
No
2009-07-17 22:45:27 WL
I mean the method inherited by subclass.
2009-07-17 22:45:29 WL
Me heavy load
2009-07-17 22:45:39 WL
And written as public
2009-07-17 22:45:41 WL
Yes
22:45:48 success _ ● pig
Yes
22:45:52 success _ ● pig
You can try
22:45:56 success _ ● pig
Yes
22:46:02 success _ ● pig
Overload should be fine.
2009-07-17 22:48:14 WL
Isn't it an override?
2009-07-17 22:49:11 WL
Do you need to declare keywords for this overload?
22:49:19 success _ ● pig
No
22:49:22 success _ ● pig
Override
22:49:24 success _ ● pig
Hmm
2009-07-17 22:49:36 WL
Just cover it directly, right?
22:49:49 success _ ● pig
You just need to try VC.
22:49:51 success _ ● pig
I am in an Internet cafe
22:49:54 success _ ● pig
Supplemented
2009-07-17 22:49:57 WL
Hmm
2009-07-17 22:49:59 WL
Okay.
2009-07-17 22:54:54 WL
Amount
2009-07-17 22:55:08 WL
In fact, there is no overwrite or overload at all.
2009-07-17 22:55:16 WL
These are two unrelated functions.
2009-07-17 22:55:30 WL
Because the function signature is different
2009-07-17 22:55:45 WL
Private and public belong to the function signature.
22:56:26 success _ ● pig
......
2009-07-17 22:56:43 WL
What is this?
2009-07-17 22:56:56 WL
Polymorphism
2009-07-17 22:57:02 WL
It can have the same name.
22:57:04 success _ ● pig
Yes
22:57:07 success _ ● pig
Is polymorphism.
22:57:09 success _ ● pig
Dizzy
22:57:12 success _ ● pig
I know what you mean.
2009-07-17 22:57:14 WL
But the signature is different.
22:57:14 success _ ● pig
Not that.
22:57:17 success _ ● pig
I mean polymorphism
22:57:38 success _ ● pig
When you overload a private class and then call the base class, do you need to use the function that has been overloaded by your subclass?
22:57:41 success _ ● pig
No
2009-07-17 22:57:52 WL
Oh
2009-07-17 22:58:43 WL
What do you mean is that private cannot be inherited?
22:58:50 success _ ● pig
Hmm
2009-07-17 22:59:01 WL
I have to say
2009-07-17 22:59:04 WL
You are sb
2009-07-17 22:59:14 WL
Private can be inherited
22:59:16 success _ ● pig
No
2009-07-17 22:59:19 WL
Yes
2009-07-17 22:59:24 WL
Absolutely.
2009-07-17 22:59:34 WL
I wrote a few such programs.
22:59:38 success _ ● pig
Standard C ++ is absolutely not acceptable
22:59:44 success _ ● pig
This is the original saying
22:59:55 success _ ● pig
Unless your compiler has a problem
2009-07-17 23:00:40 WL
What I wrote in my book is:
Y implements inheritance for X
2009-07-17 23:01:05 WL
Represents that y inherits all data members and Member objects of X.
2009-07-17 23:01:24 WL
Actually, I tried it a few days ago.
2009-07-17 23:01:37 WL
And I found that my previous understanding of inheritance is incorrect.
23:02:06 success _ ● pig
You are ill
2009-07-17 23:02:09 WL
...
2009-07-17 23:02:14 WL
You just made a mistake.
2009-07-17 23:02:22 WL
You can ask someone else.
23:02:23 success _ ● pig
You know, you asked me on MSN that day that private could be inherited.
23:02:31 success _ ● pig
My colleagues saw it and they laughed.
2009-07-17 23:02:36 WL
Try writing a program by yourself
23:02:38 success _ ● pig
You do not know that you cannot be inherited ......
23:02:47 success _ ● pig
I was no longer in my seat
2009-07-17 23:02:52 WL
Can be inherited
2009-07-17 23:02:54 WL
...
2009-07-17 23:03:21 WL
To be honest
2009-07-17 23:03:24 WL
After you tell me this
2009-07-17 23:03:33 WL
I just think they are ignorant.
2009-07-17 23:03:53 WL
For transactions that you know
2009-07-17 23:04:10 WL
Are you sure you know everything is right?
23:04:10 success _ ● pig
Class clidder {
Private final void Flipper (){
System. Out. println ("clidder ");
}
}
Public class example extends clidder {
Private final void Flipper (){
System. Out. println ("example ");
}
Public static void main (string [] ARGs ){
New example (). Flipper ();
}
}
Running result:
Example
The process has been completed.
The program has no errors. Although the flipper () method in the parent clidder is a final method, it cannot be overloaded. A flipper () method still exists in the subclass example without generating compilation errors, because the flipper () method in the subclass is not a overload method, but a new declared method. Because the flipper () method in the clidder of the parent class is a private () method and does not inherit from the quilt class, the method overload between the parent classes of the Child classes occurs.
23:04:35 success _ ● pig
Because the original words I saw on C ++ two days ago
23:04:39 success _ ● pig
Private cannot be inherited
23:04:45 success _ ● pig
C ++ Primer
2009-07-17 23:04:57 WL
You wait
2009-07-17 23:05:00 WL
Wait for me to finish reading
23:05:03 success _ ● pig
You search the following
23:05:09 success _ ● pig
C ++ primer has this original saying
2009-07-17 23:07:04 WL
Haha
2009-07-17 23:07:08 WL
I have a program
2009-07-17 23:07:17 WL
You can run it.
23:07:25 success _ ● pig
You can send it to me.
2009-07-17 23:07:55 WL
Check whether the parent class private is found in the subclass object after inheritance.
2009-07-17 23:08:25 WL
// Inheritance. cpp: defines the entry point of the console application.
//
# Include "stdafx. H"
# Include <iostream>
Using namespace STD;
Class X
{
Int I;
Public:
X () {I = 0 ;}
Void set (int ii) {I = II ;}
Int read () const {return I ;}
Int permute () {return I = I * 47 ;}
};
Class Y: Public x
{
Int I;
Public:
Y () {I = 100 ;}
Int change (){
I = permute ();
Return I;
}
Void set (int ii ){
I = II;
X: Set (II );
}
Int Geti () const {
Return I;
}
};
Int _ tmain (INT argc, _ tchar * argv [])
{
Cout <"sizeof (x)" <sizeof (x) <Endl;
Cout <"sizeof (y)" <sizeof (y) <Endl;
Y d;
Cout <"D. Geti" <D. Geti () <Endl;
Cout <"D. Change ()" <D. Change () <Endl;
Cout <"D. Geti" <D. Geti () <Endl;
D. Set (12 );
Cout <"D. Read ()" <D. Read () <Endl;
Cout <"D. Permute" <D. Permute () <Endl;
Cout <"D. Change ()" <D. Change () <Endl;
// D. Set (12 );
Cout <"D. Geti" <D. Geti () <Endl;
System ("pause ");
Return 0;
}
2009-07-17 23:08:58 WL
Before running a program, you can first think about the result of Russian understanding based on your inheritance.
23:09:04 success _ ● pig
Why is it so complicated?
2009-07-17 23:09:09 WL
Let's look at the facts.
2009-07-17 23:09:22 WL
Let me simplify it.
2009-07-17 23:09:30 WL
That's because I have done a lot of tests.
23:09:31 success _ ● pig
You can try it on your own.
2009-07-17 23:09:40 WL
I tried it hundreds of times.
2009-07-17 23:09:42 WL
..
23:09:48 success _ ● pig
Class Y: Public x
{
Int I ;,
23:09:56 success _ ● pig
You can skip this int I;
23:10:02 success _ ● pig
You have reloaded I.
2009-07-17 23:10:17 WL
No overload
2009-07-17 23:10:18 WL
...
2009-07-17 23:10:35 WL
These two I have no connection at all.
2009-07-17 23:10:45 WL
Mutual influence and
2009-07-17 23:11:01 WL
You can take my program and run it.
23:11:05 success _ ● pig
So how do you say that private is inherited?
23:11:11 success _ ● pig
I didn't see any other private
2009-07-17 23:11:23 WL
...
2009-07-17 23:11:25 WL
First
2009-07-17 23:11:29 WL
From the perspective of space
2009-07-17 23:11:42 WL
The space of the object of Y is twice that of X.
2009-07-17 23:12:02 WL
Because the y object has its own I and an x I
23:12:10 success _ ● pig
No matter what space you say, your program does not inherit private
23:12:18 success _ ● pig
Yes, but cannot access
2009-07-17 23:12:21 WL
And
23:12:27 success _ ● pig
The memory is definitely allocated.
2009-07-17 23:12:34 WL
Are you sure you want to finish it?
2009-07-17 23:13:02 WL
I can access the I in X through the method inherited from X in the Y object.
2009-07-17 23:13:29 WL
Access the I in X through inheritance Methods
2009-07-17 23:13:43 WL
I can change the I value.
2009-07-17 23:13:56 WL
The entire process does not generate any X objects.
23:14:00 success _ ● pig
I know what you mean.
23:14:03 success _ ● pig
You understood it wrong.
2009-07-17 23:14:12 WL
What do you mean
23:14:16 success _ ● pig
Complete your object-oriented understanding
23:14:33 success _ ● pig
What does inheritance mean?
2009-07-17 23:14:44 WL
Inheritance means code reuse.
23:14:50 success _ ● pig
Me and your voice
2009-07-17 23:14:57 WL
Well.
23:15:00 success _ ● pig
I must correct this problem.
2009-07-17 23:15:35 WL
I have no mi
2009-07-17 23:15:46 WL
Unable to hear
23:15:50 success _ ● pig
I have a bad mic.
2009-07-17 23:15:57 WL
Hmm
2009-07-17 23:16:00 WL
It takes 37 seconds to chat with you.
2009-07-17 23:16:21 WL
And I tell you that the inheritance in my book is in the space of its own object.
23:16:24 success _ ● pig
Let me tell you this.
2009-07-17 23:16:40 WL
Create a space for a parent class Object
23:17:08 success _ ● pig
Inheritance is a private member that is allocated to the base class in the memory.
2009-07-17 23:17:11 WL
In this space, all the parent class members are inherited.
23:17:14 success _ ● pig
Or private functions
2009-07-17 23:17:15 WL
Hmm
23:17:26 success _ ● pig
However, a derived class cannot directly access these private members.
2009-07-17 23:17:38 WL
And can be accessed through the public Method
23:17:43 success _ ● pig
However, you can access
23:17:55 success _ ● pig
Because the public interface belongs to the base class
23:18:06 success _ ● pig
Therefore, your private is visible.
23:18:27 success _ ● pig
If private can be inherited, it means
23:18:31 success _ ● pig
Direct Access
23:19:09 success _ ● pig
Private member of the base class
23:19:13 success _ ● pig
This is not acceptable.
2009-07-17 23:19:29 WL
Who said that inheritance means direct access
23:19:37 success _ ● pig
Private in the book cannot be inherited.
2009-07-17 23:19:54 WL
The Thinking in C ++ book I read is not like this.
23:20:06 success _ ● pig
It is defined in this way that the inherited methods or parameters can be directly accessed within the scope of this class.
23:20:14 success _ ● pig
Your example only inherits the public interfaces.
23:20:30 success _ ● pig
Definitions may be different.
2009-07-17 23:20:32 WL
I will show you how to write inheritance in my book
23:20:34 success _ ● pig
I mean, this is understandable.
2009-07-17 23:22:35 WL
All the private members of X are still private in Y, because if y inherits X, ykeyi does not comply with the protection mechanism. The private members in X still occupy the storage space, but they cannot be accessed directly.
23:22:52 success _ ● pig
Oh
23:23:03 success _ ● pig
I understand
23:23:06 success _ ● pig
There are two arguments:
23:23:12 success _ ● pig
All have their own principles
23:23:19 success _ ● pig
Yes, but cannot be accessed. So it seems that they cannot be inherited, but they are actually inherited.
23:23:29 success _ ● pig
This is a saying
2009-07-17 23:23:31 WL
We can see that y inherits from X, which means y will contain all data members and member functions in X.
23:23:33 success _ ● pig
Hmm
23:23:36 success _ ● pig
All make sense
2009-07-17 23:23:38 WL
This is the original saying
2009-07-17 23:23:45 WL
No missing words
23:23:52 success _ ● pig
However, the original private statement written on C ++ primer cannot be inherited.
23:24:01 success _ ● pig
The translator may have different understandings.
2009-07-17 23:24:14 WL
Space is opened
23:24:18 success _ ● pig
Hmm
2009-07-17 23:24:19 WL
And can be used
2009-07-17 23:24:29 WL
Whether or not you directly access
2009-07-17 23:24:41 WL
In short, 1. He opened the space.
2009-07-17 23:24:49 WL
2. This space can be used.
2009-07-17 23:25:20 WL
3. This object disappears.
2009-07-17 23:25:36 WL
If this is not an inheritance
2009-07-17 23:25:53 WL
The inheritance is very narrow.
2009-07-17 23:26:06 WL
Or the inheritance definition is narrow.
23:26:15 success _ ● pig
I decided I was too lazy to discuss this with you, but I understand it very well. It doesn't matter. Just use it.
2009-07-17 23:26:41 WL
Hmm
23:27:05 success _ ● pig
But it's true my company shouldn't do that.
23:27:11 success _ ● pig
Different ways of understanding
2009-07-17 23:27:13 WL
Haha
2009-07-17 23:27:31 WL
Inevitable