C ++ single inheritance

xiaoxiao2021-03-06  16

In the "base class and derived class", the basic concepts of single inheritance are described, which focuses on the specific application of inheritance.

In single inheritance, each class can have multiple derived classes, but each derived class can only have a base class to form a tree structure.

Control of members access

In the "base class and derived class", we talked about several provisions of the access to the base class and derived class for the access authority of the base class, here further discussed the specific control of access rights, and then it was used The call method when three inheritance methods.

Optimum access to // inherited public inheritance mode

#include

File: / / Define the base class a

Class A

{

PUBLIC:

A () {cout << "The constructor of class A!"

Void Aprint () {cout << "class a prints yourself Private member aa:" Int Aa;

Private:

Int aa;

protected:

Int aaa;

}

FILE: / / Define class B derived from base class A

Class B: Public A

{

PUBLIC:

B () {cout << "The constructor of class B!"

Void bprint () {cout << "class B prints its own private member BB and protected members BBB:"

Getaaaa1 ();

Private:

INT BB;

Void getaaa () {cout << "Private function accessed class AA:"

Int bbb;

Void getaaa1 () {cout << "Protected function accessed the class A of the PROTECTED Data member AAA:" <} "AAA: <};

File: // The constructor of the base class B needs to be responsible for the initialization of the constructor of the base class A

B :: B (INT I, INT J, INT K): A (I), BB (J), BBB (k) {}

File: // Program main function

void main ()

{

B B1 (100, 200, 300); file: / / Define an object B1 of class B, and initialize the constructor and the base class constructor

b1.b_aprint (); file: // class B calls its own member function B_APRINT function

b1.bprint (); file: // class B object B1 Access your own private and protected members

b1.aprint (); file: // Call class A's public member function by calling class A object B1

}

The output result of the program is:

Class B PUBLIC Function Access Class A PUBLIC Data Member AA: 100

The PUBLIC Function of Class B Access Class A Protected Data Members AAA: 100

The Private Function of Class B Access Class A PUBLIC Data Member AA: 100

The Private Function of Class B Access Class A Protected Data Members AAA: 100

The Protected Function of Class B Access Class A PUBLIC Data Member AA: 100

Protected Data Members of Class B Access Class A AAA: 100 Class B Print Owners BB and Protected Members BB: 200,300

Class a Private member AA: 100

The above is a public inheritance method, we can draw the following conclusions:

When public inheritance (PUBLIC), the members of the derived class can access public members and protect members in the base class; the object of derived class can only access public members in the base class.

Let us change the inheritance mode PUBLIC to Private, and one of the compile results appear below:

'Aprint': Cannot Access Public MEMBER DECLARED IN CLASS 'A'

The wrong statement is: b1.aprint (); and therefore, we can draw the following conclusions:

When public inheritance (Private), the derived public, private, protected-type member function can access public member and protection members in the base class; but the object of derived class is not accessible to any member in the base class. In addition, when using the Class keyword definition class, the default inheritance mode is Private, that is, when the inheritance mode is private inheritance, PRIVATE can be omitted.

Let us change the inheritance mode public to protected, it can be seen that the result is the same as the Private inheritance.

Constructor and destructor

The structure of the derived class is the main problem with the description of the discussion, and the reader must master it.

Constructor

We have known that the data structure of the object of derived class is constructed by the data member described in the data member described in the base class and the data sheet described in the derived class. The encapsulation composed of data members and operations described in the classic class in the object class are referred to as a base-based sub-object, which is initialized by the constructor in the base class.

The constructor cannot be inherited, so the constructor of the derived class must initialize the based sub-object by calling the constructor of the base class. Therefore, in addition to the constructor of the derived class, in addition to initialization of its own data, it must be responsible for calling the base class constructor to initialize the base class data member. If there is still a child object in the derived class, it should also contain the constructor initialization of the sub-object.

The general format of the derived class constructor is as follows:

(): (Parameter Table 1), ()

{

}

The sequence of calls of derived class constructors is as follows:

· Texture function of the base class

· Sub-object class constructor (if any)

· Detective class constructor

In the previous example, B:: B (INT I, INT J, INT K): A (i), BB (J), BBB (K) is the definition of derived class constructors, and then give a construction party Example of constructor.

#include

Class A

{

PUBLIC:

A () = 0; cout << "The default constructor of class A./N";

A (INT I) {a = i; cout << "The constructor of class A./N";

~ A () {cout << "The destructor of class A./N";

Void Print () const {cout

Private:

Int a;

}

Class B: Public A

{

PUBLIC:

B () {b = 0; COUT << "The default constructor of class B./N";

B (INT I, INT J, INT K);

~ B () {cout << "The destructor of class B./N";} void print ();

Private:

INT B;

A aa;

}

B :: B (INT I, INT J, INT K): A (i), AA (j)

{

B = K;

COUT << "The constructor of class B./N";

}

Void B :: Print ()

{

A :: print ();

Cout <}

void main ()

{

B bb [2];

BB [0] = B (1, 2, 5);

BB [1] = B (3, 4, 7);

For (int i = 0; i <2; i )

BB [i] .print ();

}

2. Constructor

When the object is deleted, the destructor of the derived class is executed. Since the destructor cannot be inherited, the secting function of the base class will also be called when the sectic function function is executed. The execution order is to execute the constructor of the derived class, and then perform the destructor of the base class, the order in which the order is in the order of the execution constructor. This can be seen from the examples mentioned above, please analyze it by the reader.

3. Problems should be paid attention to in the use of derived class constructors

(1) The definition of the derived class constructor can omit the call to the base class constructor, and the condition is that the default constructor must have a default constructor in the base class or the constructor is not defined. Of course, there is no defined constructor in the base class, and the derived class does not have to be responsible for calling the analyte function.

(2) When the constructor of the base class uses one or more parameters, the derived class must define the constructor to provide the passage of the parameter to the base class constructor. In some cases, the function body of the derived class constructor may be empty and only serve as a parameter transmission. If the first example of this is a case.

Submaturing and type adaptation

Subtype

The concept of subtype involves behavioral sharing, which is closely related to inheritance.

There is a specific type S, and when it provides at least the behavior of the type T, the title type S is a subtype of type T. Submatures are general and special relationships between types.

In inheritance, public inheritance can achieve subtypes. E.g:

Class A

{

PUBLIC:

Void Print () const {cout << "A :: print () Called./N";

}

Class B: Public A

{

PUBLIC:

Void f () {}

}

Class B inherits class A and is a public inheritance. Therefore, it can be said that class B is a subtype of class A. Class A can also have other subtypes. Class B is a subtype of class A, class B is an operation in class A, or the operation in class A can be used to operate the object of class B.

Sub-type relationship is irreversible. That is to say, it is known that B is a subtype of A, and it is considered that A is also a subtype of B is wrong, or that the child type relationship is asymmetrical.

Therefore, it can be said that the public inheritance can realize sub-type.

2. Type adaptation

Type adaptation refers to the relationship between two types. For example, the B-type adaptation A type refers to the occasion that the B type object can be used for a type A type of object.

The object of the derived class that can be used earlier can be used in the case where the base class object can be used, and we say that the derived class is adapted to the base class.

The same principle, the pointer and reference of the derived class object are also adapted to the pointers and references of the base class object.

Submaturing and type adaptation is The A type is a subtype of a B type, then the A type will adapt to the B type.

The importance of subtypes is to mitigate the burden of programmed program code. Because a function can be used for a type of object, it can also be used for each subtype of the type, so it is not necessary to overload the function to process objects that process these subtypes.


New Post(0)