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

UptoLike

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

Ɉɩɢɫɚɧɢɹ ɬɢɩɨɜ, ɨɛɴɹɜɥɹɟɦɵɯ ɩɪɨɝɪɚɦɦɢɫɬɨɦ, ɜ ɬɨɦ ɱɢɫɥɟ ɫɬɪɭɤɬɭɪ ɢ
ɨɛɴɟɞɢɧɟɧɢɣ, ɦɨɝɭɬ ɛɵɬɶ ɞɨɫɬɚɬɨɱɧɨ ɛɨɥɶɲɢɦɢ, ɩɨɷɬɨɦɭ ɜ ɋɢ/ɋɢ++ ɩɪɟ-
ɞɭɫɦɨɬɪɟɧɚ ɜɨɡɦɨɠɧɨɫɬɶ ɩɪɢɫɜɚɢɜɚɧɢɹ ɬɢɩɚɦ ɫɨɛɫɬɜɟɧɧɵɯ ɢɦɟɧ (ɫɢɧɨɧɢ-
ɦɨɜ), ɞɨɫɬɢɝɚɹ ɩɪɢ ɷɬɨɦ ɩɨɜɵɲɟɧɢɹ ɧɚɝɥɹɞɧɨɫɬɢ ɩɪɨɝɪɚɦɦɧɵɯ ɬɟɤɫɬɨɜ.
ɋɢɧɨɧɢɦ ɢɦɟɧɢ ɬɢɩɚ ɜɜɨɞɢɬɫɹ ɫ ɤɥɸɱɟɜɵɦ ɫɥɨɜɨɦ
typedef ɢ ɫɬɪɨɢɬɫɹ ɤɚɤ
ɨɛɵɱɧɨɟ ɨɛɴɹɜɥɟɧɢɟ, ɧɨ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɜ ɞɟɤɥɚɪɚɬɨɪɚɯ ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɢɧ-
ɬɟɪɩɪɟɬɢɪɭɸɬɫɹ ɤɚɤ ɫɢɧɨɧɢɦɵ ɨɩɢɫɚɧɧɵɯ ɬɢɩɨɜ. ɋɢɧɨɧɢɦɵ ɢɦɟɧ ɬɢɩɨɜ
ɩɪɢɧɹɬɨ ɡɚɩɢɫɵɜɚɬɶ ɩɪɨɩɢɫɧɵɦɢ ɛɭɤɜɚɦɢ, ɱɬɨɛɵ ɨɬɥɢɱɚɬɶ ɢɯ ɨɬ ɢɞɟɧɬɢ-
ɮɢɤɚɬɨɪɨɜ ɩɟɪɟɦɟɧɧɵɯ. ɇɢɠɟ ɩɪɢɜɟɞɟɧɨ ɧɟɫɤɨɥɶɤɨ ɩɪɢɦɟɪɨɜ ɨɛɴɹɜɥɟɧɢɹ
ɫɢɧɨɧɢɦɨɜ ɢɦɟɧ ɬɢɩɨɜ.
typedef struct {double re, im} COMPLEX;
typedef int *PINT;
ɉɨɫɥɟ ɬɚɤɢɯ ɨɛɴɹɜɥɟɧɢɣ ɫɢɧɨɧɢɦ ɢɦɟɧɢ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɤɚɤ ɫɩɟɰɢ-
ɮɢɤɚɬɨɪ ɬɢɩɚ:
COMPLEX ca, *pca; /* ɩɟɪɟɦɟɧɧɚɹ ɬɢɩɚ COMPLEX ɢ ɭɤɚɡɚɬɟɥɶ
ɧɚ COMPLEX */
PINT pi; // ɭɤɚɡɚɬɟɥɶ ɧɚ int
ɉɪɢɜɟɞɟɧɧɨɟ ɜɵɲɟ ɨɩɢɫɚɧɢɟ ɫɬɪɭɤɬɭɪ ɢ ɨɛɴɟɞɢɧɟɧɢɣ ɜ ɨɫɧɨɜɧɨɦ ɫɨ-
ɨɬɜɟɬɫɬɜɭɟɬ ɢɯ ɩɨɫɬɪɨɟɧɢɸ ɜ ɹɡɵɤɟ ɋɢ. ȼɋɢ++ ɫɬɪɭɤɬɭɪɵ ɢ ɨɛɴɟɞɢɧɟɧɢɹ
ɹɜɥɹɸɬɫɹ ɱɚɫɬɧɵɦɢ ɫɥɭɱɚɹɦɢ ɨɛɴɟɤɬɧɵɯ ɬɢɩɨɜ ɞɚɧɧɵɯ. Ⱦɨɩɨɥɧɢɬɟɥɶɧɵɟ
ɫɜɟɞɟɧɢɹ ɨɛ ɷɬɨɦ ɛɭɞɭɬ ɩɪɢɜɟɞɟɧɵ ɩɪɢ ɪɚɫɫɦɨɬɪɟɧɢɢ ɨɛɴɟɤɬɧɨ-
ɨɪɢɟɧɬɢɪɨɜɚɧɧɵɯ ɫɪɟɞɫɬɜ ɋɢ++.
2.4. Ɉɩɟɪɚɰɢɢ ɢ ɜɵɪɚɠɟɧɢɹ
ɇɟɫɦɨɬɪɹ ɧɚ ɨɝɪɚɧɢɱɟɧɧɵɣ ɧɚɛɨɪ ɛɚɡɨɜɵɯ ɬɢɩɨɜ ɞɚɧɧɵɯ (ɰɟɥɵɟ ɢ ɜɟ-
ɳɟɫɬɜɟɧɧɵɟ ɚɪɢɮɦɟɬɢɱɟɫɤɢɟ ɞɚɧɧɵɟ ɢ ɫɬɪɨɤɨɜɵɟ ɥɢɬɟɪɚɥɵ) ɜ ɹɡɵɤɟ ɋɢ++
ɨɩɪɟɞɟɥɟɧ ɨɛɲɢɪɧɵɣ ɧɚɛɨɪ ɨɩɟɪɚɰɢɣ ɧɚɞ ɞɚɧɧɵɦɢ, ɱɚɫɬɶ ɢɡ ɤɨɬɨɪɵɯ ɧɟ-
ɩɨɫɪɟɞɫɬɜɟɧɧɨ ɫɨɨɬɜɟɬɫɬɜɭɟɬ ɦɚɲɢɧɧɵɦ ɤɨɦɚɧɞɚɦ. Ʉɚɤ ɢ ɜɨ ɜɫɟɯ ɹɡɵɤɚɯ
ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɨɩɟɪɚɰɢɢ ɫɥɭɠɚɬ ɞɥɹ ɩɨɫɬɪɨɟɧɢɹ ɜɵɪɚɠɟɧɢɣ. ȼɵɪɚɠɟ-
ɧɢɟ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɨɩɟɪɚɧɞɨɜ ɢ ɡɧɚɤɨɜ ɨɩɟɪɚɰɢɣ
ɢ ɫɥɭɠɢɬ ɞɥɹ ɜɵɱɢɫɥɟɧɢɹ ɧɟɤɨɬɨɪɨɝɨ ɡɧɚɱɟɧɢɹ.
ȼ ɤɚɱɟɫɬɜɟ ɨɩɟɪɚɧɞɨɜ ɜ ɜɵɪɚɠɟɧɢɢ ɜɵɫɬɭɩɚɸɬ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɩɟɪɟ-
ɦɟɧɧɵɯ, ɤɨɧɫɬɚɧɬɵ ɢ ɫɬɪɨɤɨɜɵɟ ɥɢɬɟɪɚɥɵ, ɹɜɥɹɸɳɢɟɫɹ ɩɟɪɜɢɱɧɵɦɢ ɜɵ-
ɪɚɠɟɧɢɹɦɢ. ȼɵɪɚɠɟɧɢɟ, ɡɚɤɥɸɱɟɧɧɨɟ ɜ ɤɪɭɝɥɵɟ ɫɤɨɛɤɢ, ɬɚɤɠɟ ɪɚɫɫɦɚɬɪɢ-
ɜɚɟɬɫɹ ɤɚɤ ɩɟɪɜɢɱɧɨɟ. Ʉɚɠɞɚɹ ɨɩɟɪɚɰɢɹ ɩɪɟɞɩɨɥɚɝɚɟɬ ɢɫɩɨɥɶɡɨɜɚɧɢɟ ɨɩɪɟ-
ɞɟɥɟɧɧɵɯ ɬɢɩɨɜ ɨɩɟɪɚɧɞɨɜ (ɰɟɥɵɯ, ɜɟɳɟɫɬɜɟɧɧɵɯ, ɭɤɚɡɚɬɟɥɟɣ). Ɉɩɟɪɚɰɢɹ
ɩɪɢɫɜɚɢɜɚɧɢɹ ɜ ɋɢ++ ɬɚɤɠɟ ɹɜɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟɦ, ɜ ɫɜɹɡɢ ɫ ɷɬɢɦ ɪɚɡɥɢɱɚ-
ɸɬɫɹ ɨɩɟɪɚɧɞɵ, ɤɨɬɨɪɵɦ ɦɨɠɧɨ ɩɪɢɫɜɨɢɬɶ ɧɨɜɨɟ ɡɧɚɱɟɧɢɟ, ɢ ɨɩɟɪɚɧɞɵ,
ɡɧɚɱɟɧɢɟ ɤɨɬɨɪɵɯ ɧɟ ɦɨɠɟɬ ɦɟɧɹɬɶɫɹ. ɑɬɨɛɵ ɨɩɟɪɚɧɞɭ ɦɨɠɧɨ ɛɵɥɨ ɩɪɢ-
ɫɜɨɢɬɶ ɡɧɚɱɟɧɢɟ, ɟɦɭ ɞɨɥɠɧɚ ɫɨɨɬɜɟɬɫɬɜɨɜɚɬɶ ɨɛɥɚɫɬɶ ɩɚɦɹɬɢ ɢ ɤɨɦɩɢɥɹ-
ɬɨɪɭ ɞɨɥɠɟɧ ɛɵɬɶ ɢɡɜɟɫɬɟɧ ɚɞɪɟɫ ɷɬɨɣ ɩɚɦɹɬɢ. Ɍɚɤɢɟ ɨɩɟɪɚɧɞɵ ɧɚɡɵɜɚɸɬ
16
    Ɉɩɢɫɚɧɢɹ ɬɢɩɨɜ, ɨɛɴɹɜɥɹɟɦɵɯ ɩɪɨɝɪɚɦɦɢɫɬɨɦ, ɜ ɬɨɦ ɱɢɫɥɟ ɫɬɪɭɤɬɭɪ ɢ
ɨɛɴɟɞɢɧɟɧɢɣ, ɦɨɝɭɬ ɛɵɬɶ ɞɨɫɬɚɬɨɱɧɨ ɛɨɥɶɲɢɦɢ, ɩɨɷɬɨɦɭ ɜ ɋɢ/ɋɢ++ ɩɪɟ-
ɞɭɫɦɨɬɪɟɧɚ ɜɨɡɦɨɠɧɨɫɬɶ ɩɪɢɫɜɚɢɜɚɧɢɹ ɬɢɩɚɦ ɫɨɛɫɬɜɟɧɧɵɯ ɢɦɟɧ (ɫɢɧɨɧɢ-
ɦɨɜ), ɞɨɫɬɢɝɚɹ ɩɪɢ ɷɬɨɦ ɩɨɜɵɲɟɧɢɹ ɧɚɝɥɹɞɧɨɫɬɢ ɩɪɨɝɪɚɦɦɧɵɯ ɬɟɤɫɬɨɜ.
ɋɢɧɨɧɢɦ ɢɦɟɧɢ ɬɢɩɚ ɜɜɨɞɢɬɫɹ ɫ ɤɥɸɱɟɜɵɦ ɫɥɨɜɨɦ typedef ɢ ɫɬɪɨɢɬɫɹ ɤɚɤ
ɨɛɵɱɧɨɟ ɨɛɴɹɜɥɟɧɢɟ, ɧɨ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɜ ɞɟɤɥɚɪɚɬɨɪɚɯ ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɢɧ-
ɬɟɪɩɪɟɬɢɪɭɸɬɫɹ ɤɚɤ ɫɢɧɨɧɢɦɵ ɨɩɢɫɚɧɧɵɯ ɬɢɩɨɜ. ɋɢɧɨɧɢɦɵ ɢɦɟɧ ɬɢɩɨɜ
ɩɪɢɧɹɬɨ ɡɚɩɢɫɵɜɚɬɶ ɩɪɨɩɢɫɧɵɦɢ ɛɭɤɜɚɦɢ, ɱɬɨɛɵ ɨɬɥɢɱɚɬɶ ɢɯ ɨɬ ɢɞɟɧɬɢ-
ɮɢɤɚɬɨɪɨɜ ɩɟɪɟɦɟɧɧɵɯ. ɇɢɠɟ ɩɪɢɜɟɞɟɧɨ ɧɟɫɤɨɥɶɤɨ ɩɪɢɦɟɪɨɜ ɨɛɴɹɜɥɟɧɢɹ
ɫɢɧɨɧɢɦɨɜ ɢɦɟɧ ɬɢɩɨɜ.

     typedef struct {double re, im} COMPLEX;
     typedef int *PINT;
ɉɨɫɥɟ ɬɚɤɢɯ ɨɛɴɹɜɥɟɧɢɣ ɫɢɧɨɧɢɦ ɢɦɟɧɢ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɤɚɤ ɫɩɟɰɢ-
ɮɢɤɚɬɨɪ ɬɢɩɚ:
     COMPLEX ca, *pca;     /* ɩɟɪɟɦɟɧɧɚɹ ɬɢɩɚ COMPLEX ɢ ɭɤɚɡɚɬɟɥɶ
                              ɧɚ COMPLEX */
     PINT pi;              // ɭɤɚɡɚɬɟɥɶ ɧɚ int
    ɉɪɢɜɟɞɟɧɧɨɟ ɜɵɲɟ ɨɩɢɫɚɧɢɟ ɫɬɪɭɤɬɭɪ ɢ ɨɛɴɟɞɢɧɟɧɢɣ ɜ ɨɫɧɨɜɧɨɦ ɫɨ-
ɨɬɜɟɬɫɬɜɭɟɬ ɢɯ ɩɨɫɬɪɨɟɧɢɸ ɜ ɹɡɵɤɟ ɋɢ. ȼ ɋɢ++ ɫɬɪɭɤɬɭɪɵ ɢ ɨɛɴɟɞɢɧɟɧɢɹ
ɹɜɥɹɸɬɫɹ ɱɚɫɬɧɵɦɢ ɫɥɭɱɚɹɦɢ ɨɛɴɟɤɬɧɵɯ ɬɢɩɨɜ ɞɚɧɧɵɯ. Ⱦɨɩɨɥɧɢɬɟɥɶɧɵɟ
ɫɜɟɞɟɧɢɹ ɨɛ ɷɬɨɦ ɛɭɞɭɬ ɩɪɢɜɟɞɟɧɵ ɩɪɢ ɪɚɫɫɦɨɬɪɟɧɢɢ ɨɛɴɟɤɬɧɨ-
ɨɪɢɟɧɬɢɪɨɜɚɧɧɵɯ ɫɪɟɞɫɬɜ ɋɢ++.

2.4. Ɉɩɟɪɚɰɢɢ ɢ ɜɵɪɚɠɟɧɢɹ

    ɇɟɫɦɨɬɪɹ ɧɚ ɨɝɪɚɧɢɱɟɧɧɵɣ ɧɚɛɨɪ ɛɚɡɨɜɵɯ ɬɢɩɨɜ ɞɚɧɧɵɯ (ɰɟɥɵɟ ɢ ɜɟ-
ɳɟɫɬɜɟɧɧɵɟ ɚɪɢɮɦɟɬɢɱɟɫɤɢɟ ɞɚɧɧɵɟ ɢ ɫɬɪɨɤɨɜɵɟ ɥɢɬɟɪɚɥɵ) ɜ ɹɡɵɤɟ ɋɢ++
ɨɩɪɟɞɟɥɟɧ ɨɛɲɢɪɧɵɣ ɧɚɛɨɪ ɨɩɟɪɚɰɢɣ ɧɚɞ ɞɚɧɧɵɦɢ, ɱɚɫɬɶ ɢɡ ɤɨɬɨɪɵɯ ɧɟ-
ɩɨɫɪɟɞɫɬɜɟɧɧɨ ɫɨɨɬɜɟɬɫɬɜɭɟɬ ɦɚɲɢɧɧɵɦ ɤɨɦɚɧɞɚɦ. Ʉɚɤ ɢ ɜɨ ɜɫɟɯ ɹɡɵɤɚɯ
ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɨɩɟɪɚɰɢɢ ɫɥɭɠɚɬ ɞɥɹ ɩɨɫɬɪɨɟɧɢɹ ɜɵɪɚɠɟɧɢɣ. ȼɵɪɚɠɟ-
ɧɢɟ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɨɩɟɪɚɧɞɨɜ ɢ ɡɧɚɤɨɜ ɨɩɟɪɚɰɢɣ
ɢ ɫɥɭɠɢɬ ɞɥɹ ɜɵɱɢɫɥɟɧɢɹ ɧɟɤɨɬɨɪɨɝɨ ɡɧɚɱɟɧɢɹ.
    ȼ ɤɚɱɟɫɬɜɟ ɨɩɟɪɚɧɞɨɜ ɜ ɜɵɪɚɠɟɧɢɢ ɜɵɫɬɭɩɚɸɬ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɩɟɪɟ-
ɦɟɧɧɵɯ, ɤɨɧɫɬɚɧɬɵ ɢ ɫɬɪɨɤɨɜɵɟ ɥɢɬɟɪɚɥɵ, ɹɜɥɹɸɳɢɟɫɹ ɩɟɪɜɢɱɧɵɦɢ ɜɵ-
ɪɚɠɟɧɢɹɦɢ. ȼɵɪɚɠɟɧɢɟ, ɡɚɤɥɸɱɟɧɧɨɟ ɜ ɤɪɭɝɥɵɟ ɫɤɨɛɤɢ, ɬɚɤɠɟ ɪɚɫɫɦɚɬɪɢ-
ɜɚɟɬɫɹ ɤɚɤ ɩɟɪɜɢɱɧɨɟ. Ʉɚɠɞɚɹ ɨɩɟɪɚɰɢɹ ɩɪɟɞɩɨɥɚɝɚɟɬ ɢɫɩɨɥɶɡɨɜɚɧɢɟ ɨɩɪɟ-
ɞɟɥɟɧɧɵɯ ɬɢɩɨɜ ɨɩɟɪɚɧɞɨɜ (ɰɟɥɵɯ, ɜɟɳɟɫɬɜɟɧɧɵɯ, ɭɤɚɡɚɬɟɥɟɣ). Ɉɩɟɪɚɰɢɹ
ɩɪɢɫɜɚɢɜɚɧɢɹ ɜ ɋɢ++ ɬɚɤɠɟ ɹɜɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟɦ, ɜ ɫɜɹɡɢ ɫ ɷɬɢɦ ɪɚɡɥɢɱɚ-
ɸɬɫɹ ɨɩɟɪɚɧɞɵ, ɤɨɬɨɪɵɦ ɦɨɠɧɨ ɩɪɢɫɜɨɢɬɶ ɧɨɜɨɟ ɡɧɚɱɟɧɢɟ, ɢ ɨɩɟɪɚɧɞɵ,
ɡɧɚɱɟɧɢɟ ɤɨɬɨɪɵɯ ɧɟ ɦɨɠɟɬ ɦɟɧɹɬɶɫɹ. ɑɬɨɛɵ ɨɩɟɪɚɧɞɭ ɦɨɠɧɨ ɛɵɥɨ ɩɪɢ-
ɫɜɨɢɬɶ ɡɧɚɱɟɧɢɟ, ɟɦɭ ɞɨɥɠɧɚ ɫɨɨɬɜɟɬɫɬɜɨɜɚɬɶ ɨɛɥɚɫɬɶ ɩɚɦɹɬɢ ɢ ɤɨɦɩɢɥɹ-
ɬɨɪɭ ɞɨɥɠɟɧ ɛɵɬɶ ɢɡɜɟɫɬɟɧ ɚɞɪɟɫ ɷɬɨɣ ɩɚɦɹɬɢ. Ɍɚɤɢɟ ɨɩɟɪɚɧɞɵ ɧɚɡɵɜɚɸɬ

                                  16