Основы языка программирования C++ с применением объектно-ориентированного программирования. Чернышов М.К. - 21 стр.

UptoLike

Составители: 

Ɉɩɟɪɚɰɢɹ «::» (ɞɜɚ ɞɜɨɟɬɨɱɢɹ) ɩɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɭɬɨɱɧɟɧɢɹ ɢɦɟɧɢ
ɨɛɴɟɤɬɚ ɩɪɨɝɪɚɦɦɵ ɜ ɫɥɭɱɚɟ, ɤɨɝɞɚ ɜ ɷɬɨɦ ɦɟɫɬɟ ɩɪɨɝɪɚɦɦɵ ɢɡɜɟɫɬɧɵ ɞɜɚ
ɨɞɢɧɚɤɨɜɵɯ ɢɦɟɧɢ, ɧɚɩɪɢɦɟɪ, ɤɨɝɞɚ ɨɞɧɨ ɢɦɹ ɨɛɴɹɜɥɟɧɨ ɝɥɨɛɚɥɶɧɨ, ɚɞɪɭ-
ɝɨɟ ɡɚɞɚɧɨ ɜ ɬɟɥɟ ɮɭɧɤɰɢɢ. ȿɫɥɢ ɢɦɟɧɢ ɩɪɟɞɲɟɫɬɜɭɸɬ ɞɜɚ ɞɜɨɟɬɨɱɢɹ, ɬɨ
ɷɬɨ ɝɥɨɛɚɥɶɧɨɟ ɢɦɹ.
Ⱦɥɹ ɨɛɪɚɳɟɧɢɹ ɤ ɱɥɟɧɚɦ ɫɬɪɭɤɬɭɪɵ ɢɥɢ ɨɛɴɟɞɢɧɟɧɢɹ ɦɨɠɧɨ ɜɨɫɩɨɥɶ-
ɡɨɜɚɬɶɫɹ ɥɢɛɨ ɢɦɟɧɟɦ ɫɬɪɭɤɬɭɪɧɨɝɨ ɞɚɧɧɨɝɨ, ɥɢɛɨ ɭɤɚɡɚɬɟɥɟɦ ɧɚ ɫɬɪɭɤɬɭɪ-
ɧɨɟ ɞɚɧɧɨɟ. ȼ ɩɟɪɜɨɦ ɫɥɭɱɚɟ ɩɨɥɧɨɟ ɢɦɹ ɱɥɟɧɚ ɫɬɪɭɤɬɭɪɵ ɫɨɫɬɨɢɬ ɢɡ ɢɦɟ-
ɧɢ ɫɚɦɨɣ ɫɬɪɭɤɬɭɪɵ ɢ ɢɦɟɧɢ ɱɥɟɧɚ ɫɬɪɭɤɬɭɪɵ, ɪɚɡɞɟɥɟɧɧɵɯ ɬɨɱɤɨɣ. ȼɨ
ɜɬɨɪɨɦ ɫɥɭɱɚɟ ɡɚ ɢɦɟɧɟɦ ɭɤɚɡɚɬɟɥɹ ɧɚ ɫɬɪɭɤɬɭɪɭ ɫɬɚɜɢɬɫɹ ɡɧɚɤ
«->»
(ɫɬɪɟɥɤɚ), ɚ ɡɚ ɧɢɦ ɢɦɹ ɱɥɟɧɚ ɫɬɪɭɤɬɭɪɵ.
ɉɭɫɬɶ ɜ ɩɪɨɝɪɚɦɦɟ ɨɛɴɹɜɥɟɧ ɫɬɪɭɤɬɭɪɧɵɣ ɬɢɩ
AnyStruct, ɫɨɞɟɪɠɚ-
ɳɢɣ ɤɨɦɩɨɧɟɧɬɭ ɫ ɢɦɟɧɟɦ
member ɬɢɩɚ int, ɢ ɨɛɴɹɜɥɟɧɵ ɩɟɪɟɦɟɧɧɵɟ
AnyStruct s1; // Ⱦɚɧɧɨɟ s1 ɬɢɩɚ AnyStruct
AnyStruct *ps1 = &s1; /* ɍɤɚɡɚɬɟɥɶ ɧɚ ɞɚɧɧɨɟ ɬɢɩɚ
AnyStruct */
Ɍɨɝɞɚ ɤ ɱɥɟɧɭ ɫɬɪɭɤɬɭɪɵ member ɢɡ s1 ɦɨɠɧɨ ɨɛɪɚɬɢɬɶɫɹ ɤɚɤ ɤ
s1.member ɢɥɢ ɤɚɤ ɤ ps1->member.
ɉɨɫɤɨɥɶɤɭ ɱɥɟɧɨɦ ɫɬɪɭɤɬɭɪɵ ɦɨɠɟɬ ɛɵɬɶ ɭɤɚɡɚɬɟɥɶ, ɜɋɢ++ ɢɦɟɸɬɫɹ
ɫɩɟɰɢɚɥɶɧɵɟ ɨɩɟɪɚɰɢɢ ɪɚɡɵɦɟɧɨɜɚɧɢɹ ɬɚɤɨɝɨ ɭɤɚɡɚɬɟɥɹ, ɨɩɟɪɚɰɢɢ
«.*» ɢ «->*». ɉɭɫɬɶ ɨɞɧɢɦ ɢɡ ɱɥɟɧɨɜ ɫɬɪɭɤɬɭɪɵ AnyStruct ɹɜɥɹɟɬɫɹ ɭɤɚ-
ɡɚɬɟɥɶ
pp1 ɧɚ ɞɚɧɧɨɟ ɬɢɩɚ int. Ɍɨɝɞɚ ɜɵɪɚɠɟɧɢɹ s1.*pp1 ɢ ps1->*pp1
ɨɛɟɫɩɟɱɚɬ ɞɨɫɬɭɩ ɤ ɡɧɚɱɟɧɢɸ ɞɚɧɧɨɝɨ, ɧɚ ɤɨɬɨɪɨɟ ɭɤɚɡɵɜɚɟɬ
pp1 ɢɡ s1.
ȼɵɲɟ ɨɬɦɟɱɚɥɨɫɶ, ɱɬɨ ɢɦɹ ɦɚɫɫɢɜɚ ɜ ɋɢ/ɋɢ++ ɢɧɬɟɪɩɪɟɬɢɪɭɟɬɫɹ ɤɚɤ
ɭɤɚɡɚɬɟɥɶ-ɤɨɧɫɬɚɧɬɚ ɧɚ ɩɟɪɜɵɣ ɷɥɟɦɟɧɬ ɦɚɫɫɢɜɚ. Ⱦɥɹ ɪɚɡɵɦɟɧɨɜɚɧɢɹ ɭɤɚ-
ɡɚɬɟɥɹ, ɬ. ɟ. ɞɥɹ ɞɨɫɬɭɩɚ ɤ ɞɚɧɧɨɦɭ ɩɨ ɭɤɚɡɚɬɟɥɸ ɧɚ ɷɬɨ ɞɚɧɧɨɟ, ɫɥɭɠɢɬ
ɨɩɟɪɚɰɢɹ «*» (ɡɜɟɡɞɨɱɤɚ). ɋɥɟɞɨɜɚɬɟɥɶɧɨ, ɟɫɥɢ ɜ ɩɪɨɝɪɚɦɦɟ ɨɛɴɹɜɥɟɧ ɦɚɫ-
ɫɢɜ
int Array1[10];
ɬɨ ɜɵɪɚɠɟɧɢɟ *Array1=0 ɫɥɭɠɢɬ ɞɥɹ ɩɪɢɫɜɨɟɧɢɹ ɧɭɥɟɜɨɝɨ ɡɧɚɱɟɧɢɹ ɩɟɪ-
ɜɨɦɭ ɷɥɟɦɟɧɬɭ ɦɚɫɫɢɜɚ. ɑɬɨɛɵ ɨɛɪɚɬɢɬɶɫɹ ɤ ɩɪɨɢɡɜɨɥɶɧɨɦɭ ɷɥɟɦɟɧɬɭ ɦɚɫ-
ɫɢɜɚ, ɧɭɠɧɨ ɭɤɚɡɚɬɶ ɢɧɞɟɤɫ ɷɥɟɦɟɧɬɚ, ɧɚɩɪɢɦɟɪ,
Array1[3]. ɗɬɨ ɜɵɪɚɠɟ-
ɧɢɟ ɷɤɜɢɜɚɥɟɧɬɧɨ ɜɵɪɚɠɟɧɢɸ
*(Array1 + 3), ɬɨ ɟɫɬɶ ɬɪɟɛɭɟɬɫɹ ɫɧɚɱɚɥɚ
ɭɜɟɥɢɱɢɬɶ ɭɤɚɡɚɬɟɥɶ
Array1 ɧɚ 3 ɟɞɢɧɢɰɵ, ɚ ɡɚɬɟɦ ɪɚɡɵɦɟɧɨɜɚɬɶ ɩɨɥɭɱɟɧ-
ɧɵɣ ɭɤɚɡɚɬɟɥɶ. ɉɪɢ ɫɥɨɠɟɧɢɢ ɭɤɚɡɚɬɟɥɹ ɧɚ ɨɛɴɟɤɬ ɧɟɤɨɬɨɪɨɝɨ ɬɢɩɚ
T ɫɰɟ-
ɥɵɦ ɱɢɫɥɨɦ
N ɡɧɚɱɟɧɢɟ ɭɤɚɡɚɬɟɥɹ ɭɜɟɥɢɱɢɜɚɟɬɫɹ ɧɚ N, ɭɦɧɨɠɟɧɧɨɟ ɧɚ
ɞɥɢɧɭ ɞɚɧɧɨɝɨ ɬɢɩɚ
T. Ɉɬɦɟɬɢɦ, ɱɬɨ ɢɧɞɟɤɫ ɦɨɠɧɨ ɡɚɞɚɜɚɬɶ ɧɟ ɬɨɥɶɤɨ ɞɥɹ
ɢɦɟɧ ɦɚɫɫɢɜɨɜ, ɧɨ ɢ ɞɥɹ ɥɸɛɨɝɨ ɬɢɩɚ ɭɤɚɡɚɬɟɥɹ, ɤɪɨɦɟ ɭɤɚɡɚɬɟɥɹ ɧɚ ɬɢɩ
void:
int *pint=&Array[4];
pint[2]=1;
21
     Ɉɩɟɪɚɰɢɹ «::» (ɞɜɚ ɞɜɨɟɬɨɱɢɹ) ɩɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɭɬɨɱɧɟɧɢɹ ɢɦɟɧɢ
ɨɛɴɟɤɬɚ ɩɪɨɝɪɚɦɦɵ ɜ ɫɥɭɱɚɟ, ɤɨɝɞɚ ɜ ɷɬɨɦ ɦɟɫɬɟ ɩɪɨɝɪɚɦɦɵ ɢɡɜɟɫɬɧɵ ɞɜɚ
ɨɞɢɧɚɤɨɜɵɯ ɢɦɟɧɢ, ɧɚɩɪɢɦɟɪ, ɤɨɝɞɚ ɨɞɧɨ ɢɦɹ ɨɛɴɹɜɥɟɧɨ ɝɥɨɛɚɥɶɧɨ, ɚ ɞɪɭ-
ɝɨɟ ɡɚɞɚɧɨ ɜ ɬɟɥɟ ɮɭɧɤɰɢɢ. ȿɫɥɢ ɢɦɟɧɢ ɩɪɟɞɲɟɫɬɜɭɸɬ ɞɜɚ ɞɜɨɟɬɨɱɢɹ, ɬɨ
ɷɬɨ ɝɥɨɛɚɥɶɧɨɟ ɢɦɹ.
     Ⱦɥɹ ɨɛɪɚɳɟɧɢɹ ɤ ɱɥɟɧɚɦ ɫɬɪɭɤɬɭɪɵ ɢɥɢ ɨɛɴɟɞɢɧɟɧɢɹ ɦɨɠɧɨ ɜɨɫɩɨɥɶ-
ɡɨɜɚɬɶɫɹ ɥɢɛɨ ɢɦɟɧɟɦ ɫɬɪɭɤɬɭɪɧɨɝɨ ɞɚɧɧɨɝɨ, ɥɢɛɨ ɭɤɚɡɚɬɟɥɟɦ ɧɚ ɫɬɪɭɤɬɭɪ-
ɧɨɟ ɞɚɧɧɨɟ. ȼ ɩɟɪɜɨɦ ɫɥɭɱɚɟ ɩɨɥɧɨɟ ɢɦɹ ɱɥɟɧɚ ɫɬɪɭɤɬɭɪɵ ɫɨɫɬɨɢɬ ɢɡ ɢɦɟ-
ɧɢ ɫɚɦɨɣ ɫɬɪɭɤɬɭɪɵ ɢ ɢɦɟɧɢ ɱɥɟɧɚ ɫɬɪɭɤɬɭɪɵ, ɪɚɡɞɟɥɟɧɧɵɯ ɬɨɱɤɨɣ. ȼɨ
ɜɬɨɪɨɦ ɫɥɭɱɚɟ ɡɚ ɢɦɟɧɟɦ ɭɤɚɡɚɬɟɥɹ ɧɚ ɫɬɪɭɤɬɭɪɭ ɫɬɚɜɢɬɫɹ ɡɧɚɤ «->»
(ɫɬɪɟɥɤɚ), ɚ ɡɚ ɧɢɦ ɢɦɹ ɱɥɟɧɚ ɫɬɪɭɤɬɭɪɵ.
     ɉɭɫɬɶ ɜ ɩɪɨɝɪɚɦɦɟ ɨɛɴɹɜɥɟɧ ɫɬɪɭɤɬɭɪɧɵɣ ɬɢɩ AnyStruct, ɫɨɞɟɪɠɚ-
ɳɢɣ ɤɨɦɩɨɧɟɧɬɭ ɫ ɢɦɟɧɟɦ member ɬɢɩɚ int, ɢ ɨɛɴɹɜɥɟɧɵ ɩɟɪɟɦɟɧɧɵɟ
     AnyStruct s1;                  // Ⱦɚɧɧɨɟ s1 ɬɢɩɚ AnyStruct
     AnyStruct *ps1 = &s1;          /* ɍɤɚɡɚɬɟɥɶ ɧɚ ɞɚɧɧɨɟ ɬɢɩɚ
                                       AnyStruct */
    Ɍɨɝɞɚ ɤ ɱɥɟɧɭ ɫɬɪɭɤɬɭɪɵ member ɢɡ s1 ɦɨɠɧɨ ɨɛɪɚɬɢɬɶɫɹ ɤɚɤ ɤ
s1.member ɢɥɢ ɤɚɤ ɤ ps1->member.
    ɉɨɫɤɨɥɶɤɭ ɱɥɟɧɨɦ ɫɬɪɭɤɬɭɪɵ ɦɨɠɟɬ ɛɵɬɶ ɭɤɚɡɚɬɟɥɶ, ɜ ɋɢ++ ɢɦɟɸɬɫɹ
ɫɩɟɰɢɚɥɶɧɵɟ ɨɩɟɪɚɰɢɢ ɪɚɡɵɦɟɧɨɜɚɧɢɹ ɬɚɤɨɝɨ ɭɤɚɡɚɬɟɥɹ, ɨɩɟɪɚɰɢɢ
«.*» ɢ «->*». ɉɭɫɬɶ ɨɞɧɢɦ ɢɡ ɱɥɟɧɨɜ ɫɬɪɭɤɬɭɪɵ AnyStruct ɹɜɥɹɟɬɫɹ ɭɤɚ-
ɡɚɬɟɥɶ pp1 ɧɚ ɞɚɧɧɨɟ ɬɢɩɚ int. Ɍɨɝɞɚ ɜɵɪɚɠɟɧɢɹ s1.*pp1 ɢ ps1->*pp1
ɨɛɟɫɩɟɱɚɬ ɞɨɫɬɭɩ ɤ ɡɧɚɱɟɧɢɸ ɞɚɧɧɨɝɨ, ɧɚ ɤɨɬɨɪɨɟ ɭɤɚɡɵɜɚɟɬ pp1 ɢɡ s1.
    ȼɵɲɟ ɨɬɦɟɱɚɥɨɫɶ, ɱɬɨ ɢɦɹ ɦɚɫɫɢɜɚ ɜ ɋɢ/ɋɢ++ ɢɧɬɟɪɩɪɟɬɢɪɭɟɬɫɹ ɤɚɤ
ɭɤɚɡɚɬɟɥɶ-ɤɨɧɫɬɚɧɬɚ ɧɚ ɩɟɪɜɵɣ ɷɥɟɦɟɧɬ ɦɚɫɫɢɜɚ. Ⱦɥɹ ɪɚɡɵɦɟɧɨɜɚɧɢɹ ɭɤɚ-
ɡɚɬɟɥɹ, ɬ. ɟ. ɞɥɹ ɞɨɫɬɭɩɚ ɤ ɞɚɧɧɨɦɭ ɩɨ ɭɤɚɡɚɬɟɥɸ ɧɚ ɷɬɨ ɞɚɧɧɨɟ, ɫɥɭɠɢɬ
ɨɩɟɪɚɰɢɹ «*» (ɡɜɟɡɞɨɱɤɚ). ɋɥɟɞɨɜɚɬɟɥɶɧɨ, ɟɫɥɢ ɜ ɩɪɨɝɪɚɦɦɟ ɨɛɴɹɜɥɟɧ ɦɚɫ-
ɫɢɜ
     int Array1[10];
ɬɨ ɜɵɪɚɠɟɧɢɟ *Array1=0 ɫɥɭɠɢɬ ɞɥɹ ɩɪɢɫɜɨɟɧɢɹ ɧɭɥɟɜɨɝɨ ɡɧɚɱɟɧɢɹ ɩɟɪ-
ɜɨɦɭ ɷɥɟɦɟɧɬɭ ɦɚɫɫɢɜɚ. ɑɬɨɛɵ ɨɛɪɚɬɢɬɶɫɹ ɤ ɩɪɨɢɡɜɨɥɶɧɨɦɭ ɷɥɟɦɟɧɬɭ ɦɚɫ-
ɫɢɜɚ, ɧɭɠɧɨ ɭɤɚɡɚɬɶ ɢɧɞɟɤɫ ɷɥɟɦɟɧɬɚ, ɧɚɩɪɢɦɟɪ, Array1[3]. ɗɬɨ ɜɵɪɚɠɟ-
ɧɢɟ ɷɤɜɢɜɚɥɟɧɬɧɨ ɜɵɪɚɠɟɧɢɸ *(Array1 + 3), ɬɨ ɟɫɬɶ ɬɪɟɛɭɟɬɫɹ ɫɧɚɱɚɥɚ
ɭɜɟɥɢɱɢɬɶ ɭɤɚɡɚɬɟɥɶ Array1 ɧɚ 3 ɟɞɢɧɢɰɵ, ɚ ɡɚɬɟɦ ɪɚɡɵɦɟɧɨɜɚɬɶ ɩɨɥɭɱɟɧ-
ɧɵɣ ɭɤɚɡɚɬɟɥɶ. ɉɪɢ ɫɥɨɠɟɧɢɢ ɭɤɚɡɚɬɟɥɹ ɧɚ ɨɛɴɟɤɬ ɧɟɤɨɬɨɪɨɝɨ ɬɢɩɚ T ɫ ɰɟ-
ɥɵɦ ɱɢɫɥɨɦ N ɡɧɚɱɟɧɢɟ ɭɤɚɡɚɬɟɥɹ ɭɜɟɥɢɱɢɜɚɟɬɫɹ ɧɚ N, ɭɦɧɨɠɟɧɧɨɟ ɧɚ
ɞɥɢɧɭ ɞɚɧɧɨɝɨ ɬɢɩɚ T. Ɉɬɦɟɬɢɦ, ɱɬɨ ɢɧɞɟɤɫ ɦɨɠɧɨ ɡɚɞɚɜɚɬɶ ɧɟ ɬɨɥɶɤɨ ɞɥɹ
ɢɦɟɧ ɦɚɫɫɢɜɨɜ, ɧɨ ɢ ɞɥɹ ɥɸɛɨɝɨ ɬɢɩɚ ɭɤɚɡɚɬɟɥɹ, ɤɪɨɦɟ ɭɤɚɡɚɬɟɥɹ ɧɚ ɬɢɩ
void:
     int *pint=&Array[4];
     pint[2]=1;




                                  21