Its mountain stone ---- private derived

zhaozj2021-02-17  49

Its mountain stone

---- Private derived

Author: HolyFire

Its mountain stone can attack jade. It is good to use things to do things you can't do.

I have been in the relationship between "White Malaysian ---- inherited", and said a lot of publicly derived topics, pointed out that the relationship between subclass and parent class is a relationship, now I want The private derived is a relationship with one. Changes a keyword, which will bring essential changes, which is the changing evidence of the world.

Let's take a look at it, what is the difference between private deragement?

This is a packaged class

Class a {

PUBLIC:

Int data;

}

Class B: private a {

}

Class C: public a {

}

Void Function1 (a a)

{

}

void main ()

{

B B;

C C;

B.DATA = 0;

Function1 (b);

C.DATA = 0;

Function1 (C);

}

We compiled and discover

B.DATA = 0;

Function1 (b);

These two sentences were rejected, and

C.DATA = 0;

Function1 (C);

No problem

B.DATA = 0;

C.DATA = 0;

The first difference between private derivation and public derived, and the members of all base classes of privately derived subclasses will become private, and the member functions are no exception. I don't believe you can try it.

Function1 (b);

Function1 (c);

The second difference between private derived and public derived, the privately derived subclass is no longer a relationship, so function1 (b); cannot convert parameters to a temporary variable of type A.

However, privately derived subclasses can also use their base classes of Public and Protected members, different from the public derived, the original class interface is no longer all subclasses, and the subclasses have only its implementation.

Why do you say this, the problem in C is that the interfaces and implementations are functions (methods). This makes us confused, but in private derived, this issue will be clarified.

Class a {

PUBLIC:

Void doa (void) {}

}

Class B: private a {

PUBLIC:

Void Dob (void) {DOA ();

}

Class C: public a {

PUBLIC:

Void Doc (void) {DOA ();

}

void main ()

{

B B;

C C;

B.DOB ();

C. doc ();

B. DOA ();

C. DOA ();

}

B.DOB ();

C. doc ();

Is the interface of the derived subclass

B. DOA ();

C. DOA ();

It is a subclass using the base class interface, but B.DOA (); compiled pass, because the private derived cannot inherit the base class interface, but he is

Void Dob (void) {DOA ();

However, A :: DOA (); because he inherited the structure and implementation of the base class.

Ok, class B can use the implementation of class A, but cannot provide A interface like a class A, that is, B is used, b uses a A, and B is not A. The relationship between B and A is actually in terms of functionality, and the relationship between private derived neutron and parent class is used.

Since the idea knows the meaning of private derivation, then what is it used in the end, it is necessary to derive it in the end.

This is easy to think of, since private derived is a relationship with a relationship, and we say that the child is just a need for the base class, that is, what is the base class, then we know what is privately derived. The occasion. For example, the cowards use an ax to fire firewood, so the woods should use an ax. Class AxE {

}

Class Axman: Private AxE {

}

Wait, some people can't help but think of

Class axman {

Axe Ax;

}

Can you describe this relationship, right! If the woodman has an ax, can you use an ax? This code seems to be more easily understood. Use one relationship can indeed replace it with a relationship, using a combination of associations to independently replace privately derived solutions.

Class axman {

AXE * AX;

}

Such a relationship looks more flexible, because the woodman has no reason to bring the ax to the body forever, and the ax can also give other cowards.

Obviously, this is also the problem of efficiency and elastic hindle, without a perfect program, only suitable for discomfort. If the problem is obvious, it is very small, and then uses inheritance, and then uses the association. Some people have to say, in fact, inheritance and related effects are not the same.

Class AxE {

}

Class Axman: Private AxE {

}

Class axman {

Axe Ax;

}

There should be no efficiency difference. In fact, the actual problem is not the case.

I believe that when many people have programmed, they know that the empty class and empty structure are occupying resources.

Class Empty {

}

At least one byte should be occupied, if there is a byte alignment, then

Class axman {

Axe Ax;

}

AXE AXE; there will be resource occupations, if the compiler supports empty base class optimization, that is, the subclass of the subclass of the empty class will not occupy resources, then AxE Axe; additional losses will be Disappeared in private inheritance.

Oh, this example I will lift is too special, and the details of the compiler are involved. In fact, what I want to say, there is no perfect program, only suitable for discomfort. It is mainly the main thing for too much inheritance and associated form.

One of the data structures we often use is stack, the stack is a first-filled container, then it needs to use a container, the vector in STL is a container, but Stack is not a vector, he just needs to be stored. The function is.

#include

#include

Using namespace std;

Class Stack: private vector {

PUBLIC:

Stack (void) {}

Void Push (int value) {push_back (value);

INT POP (Void)

{

INT value = * rbegin ();

POP_BACK ();

Return Value;

}

BOOL Empty (void) {Return Empty ();

}

Void main (void)

{

Stack S;

S.push (1);

S.push (7);

S.push (6);

S.push (4);

S.push (5);

While (! s.empty ())

Cout << S.POP () << Endl;

cin.get ();

}

turn out:

5

4

6

Seduce

1

STACK uses the features of the PUSH_BACK, POP_BACK, EMPTY, RBEGIN in the vector, implements its own PUSH, POP, EMPTY interface and implementation. Stack used Vector. Ok, now we know what is private, and how to use it.

2001/10/9

Ding Ning

转载请注明原文地址:https://www.9cbs.com/read-28790.html

New Post(0)