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

UptoLike

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

int i, j, k; /* Ɍɪɢ ɩɟɪɟɦɟɧɧɵɯ ɬɢɩɚ int ɛɟɡ ɹɜɧɨɣ
ɢɧɢɰɢɚɥɢɡɚɰɢɢ */
double x=1, y=2; /* Ⱦɜɟ ɩɟɪɟɦɟɧɧɵɯ ɬɢɩɚ double
ɫ ɧɚɱɚɥɶɧɵɦɢ ɡɧɚɱɟɧɢɹɦɢ 1 ɢ 2 */
char c1='0'; /* ɉɟɪɟɦɟɧɧɚɹ ɬɢɩɚ char, ɟɟ ɡɧɚɱɟɧɢɟ
ɤɨɞ ɥɢɬɟɪɵ 0 */
Ɍɟɤɫɬ, ɡɚɩɢɫɚɧɧɵɣ ɜ ɷɬɢɯ ɩɪɢɦɟɪɚɯ ɩɨɫɥɟ ɡɧɚɤɨɜ //, ɹɜɥɹɟɬɫɹ ɤɨɦ-
ɦɟɧɬɚɪɢɟɦ ɢ ɫɥɭɠɢɬ ɬɨɥɶɤɨ ɞɥɹ ɞɨɤɭɦɟɧɬɢɪɨɜɚɧɢɹ ɩɪɨɝɪɚɦɦɵ. Ɍɚɤɨɣ
ɤɨɦɦɟɧɬɚɪɢɣ ɦɨɠɟɬ ɡɚɧɢɦɚɬɶ ɬɨɥɶɤɨ ɨɞɧɭ ɫɬɪɨɤɭ ɬɟɤɫɬɚ ɢ ɞɨɩɭɫɤɚɟɬɫɹ ɜ
ɬɟɤɫɬɚɯ ɩɪɨɝɪɚɦɦ ɧɚ ɋɢ++. Ʉɨɦɦɟɧɬɚɪɢɣ, ɡɚɧɢɦɚɸɳɢɣ ɧɟɫɤɨɥɶɤɨ ɫɬɪɨɤ,
ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɫɩɟɰɢɚɥɶɧɵɟ ɫɤɨɛɤɢ
/* ɢ */.
ȼ ɤɚɱɟɫɬɜɟ ɫɩɟɰɢɮɢɤɚɬɨɪɨɜ ɤɥɚɫɫɚ ɩɚɦɹɬɢ ɜɨ ɜɧɟɲɧɟɦ ɨɩɪɟɞɟɥɟɧɢɢ
ɦɨɠɟɬ ɭɤɚɡɵɜɚɬɶɫɹ ɨɞɧɨ ɢɡ ɤɥɸɱɟɜɵɯ ɫɥɨɜ
extern, static ɢɥɢ typedef.
ɋɩɟɰɢɮɢɤɚɬɨɪ
extern ɨɡɧɚɱɚɟɬ, ɱɬɨ ɨɛɴɹɜɥɹɟɦɵɣ ɨɛɴɟɤɬ ɩɪɢɧɚɞɥɟɠɢɬ
ɞɪɭɝɨɦɭ ɩɪɨɝɪɚɦɦɧɨɦɭ ɮɚɣɥɭ, ɚ ɜ ɞɚɧɧɨɦ ɮɚɣɥɟ ɫɨɞɟɪɠɢɬɫɹ ɬɨɥɶɤɨ ɢɧ-
ɮɨɪɦɚɰɢɹ ɨ ɟɝɨ ɬɢɩɟ ɢ ɢɦɟɧɢ ɢ ɧɟ ɞɨɥɠɧɨ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɢɧɢɰɢɚɥɢɡɢ-
ɪɭɸɳɟɟ ɜɵɪɚɠɟɧɢɟ. ɋɩɟɰɢɮɢɤɚɬɨɪ
static ɨɝɪɚɧɢɱɢɜɚɟɬ ɨɛɥɚɫɬɶ ɞɟɣɫɬɜɢɹ
ɨɛɴɹɜɥɹɟɦɨɝɨ ɢɦɟɧɢ ɞɚɧɧɵɦ ɮɚɣɥɨɦ ɢɥɢ ɛɥɨɤɨɦ, ɟɫɥɢ ɨɛɴɹɜɥɟɧɢɟ ɫɨɞɟɪ-
ɠɢɬɫɹ ɜ ɛɥɨɤɟ.
ȿɫɥɢ ɨɛɴɹɜɥɟɧɢɟ ɞɚɧɧɨɝɨ ɫɨɞɟɪɠɢɬɫɹ ɜɧɭɬɪɢ ɬɟɥɚ ɮɭɧɤɰɢɢ (ɥɨɤɚɥɶɧɨɟ
ɨɛɴɹɜɥɟɧɢɟ), ɬɨ ɦɨɠɧɨ ɭɤɚɡɵɜɚɬɶ ɫɩɟɰɢɮɢɤɚɬɨɪɵ ɤɥɚɫɫɚ ɩɚɦɹɬɢ
register
ɢɥɢ
auto. ɋɩɟɰɢɮɢɤɚɬɨɪ register ɧɨɫɢɬ ɪɟɤɨɦɟɧɞɚɬɟɥɶɧɵɣ ɯɚɪɚɤɬɟɪ,
ɤɨɦɩɢɥɹɬɨɪ ɩɵɬɚɟɬɫɹ ɪɚɡɦɟɫɬɢɬɶ ɞɚɧɧɵɟ ɷɬɨɝɨ ɤɥɚɫɫɚ ɜ ɪɟɝɢɫɬɪɟ ɩɪɨɰɟɫ-
ɫɨɪɚ, ɟɫɥɢ ɜ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɢɦɟɸɬɫɹ ɫɜɨɛɨɞɧɵɟ ɪɟɝɢɫɬɪɵ. ɋɩɟɰɢɮɢɤɚɬɨɪ
auto ɩɪɢɧɢɦɚɟɬɫɹ ɩɨ ɭɦɨɥɱɚɧɢɸ ɢ ɩɨɷɬɨɦɭ ɹɜɧɨ ɧɟ ɭɤɚɡɵɜɚɟɬɫɹ, ɨɧ ɨɡɧɚ-
ɱɚɟɬ, ɱɬɨ ɞɚɧɧɵɟ ɤɥɚɫɫɚ
auto ɞɨɥɠɧɵ ɪɚɡɦɟɳɚɬɶɫɹ ɜ ɩɪɨɝɪɚɦɦɧɨɦ ɫɬɟɤɟ
ɩɪɢ ɜɵɡɨɜɟ ɮɭɧɤɰɢɢ.
ɋɩɟɰɢɮɢɤɚɬɨɪ
typedef ɫɥɭɠɢɬ ɞɥɹ ɩɪɢɫɜɨɟɧɢɹ ɢɦɟɧɢ ɨɩɢɫɵɜɚɟɦɨɦɭ
ɬɢɩɭ ɞɚɧɧɵɯ ɢ ɛɭɞɟɬ ɪɚɫɫɦɨɬɪɟɧ ɩɨɞɪɨɛɧɟɟ ɜ ɫɥɟɞɭɸɳɟɦ ɩɚɪɚɝɪɚɮɟ.
ɇɚɪɹɞɭ ɫ ɩɨɤɚɡɚɧɧɵɦɢ ɜɵɲɟ ɤɨɧɫɬɚɧɬɚɦɢ-ɥɢɬɟɪɚɥɚɦɢ, ɡɧɚɱɟɧɢɹ ɤɨɬɨ-
ɪɵɯ ɨɩɪɟɞɟɥɹɸɬɫɹ ɢɯ ɩɪɟɞɫɬɚɜɥɟɧɢɟɦ ɜ ɩɪɨɝɪɚɦɦɟ, ɜɋɢɢɋɢ++ ɩɪɟɞɭ-
ɫɦɨɬɪɟɧɵ ɤɨɧɫɬɚɧɬɵ, ɤɨɬɨɪɵɦ ɩɪɢɫɜɚɢɜɚɸɬɫɹ ɫɨɛɫɬɜɟɧɧɵɟ ɢɦɟɧɚ - ɢɦɟɧɨ-
ɜɚɧɧɵɟ ɤɨɧɫɬɚɧɬɵ. ȼ ɨɩɢɫɚɧɢɢ ɢɦɟɧɨɜɚɧɧɨɣ ɤɨɧɫɬɚɧɬɵ ɩɪɢɫɭɬɫɬɜɭɟɬ ɨɩɢ-
ɫɚɬɟɥɶ
const, ɧɚɩɪɢɦɟɪ,
const double Pi=3.141592653;
ɉɟɪɟɦɟɧɧɨɣ, ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɤɨɬɨɪɨɣ ɨɛɴɹɜɥɟɧ ɫ ɨɩɢɫɚɬɟɥɟɦ const,
ɧɟɥɶɡɹ ɩɪɢɫɜɨɢɬɶ ɢɧɨɟ ɡɧɚɱɟɧɢɟ, ɱɟɦ ɛɵɥɨ ɭɫɬɚɧɨɜɥɟɧɨ ɩɪɢ ɨɛɴɹɜɥɟɧɢɢ
ɢɞɟɧɬɢɮɢɤɚɬɨɪɚ. ɂɧɢɰɢɚɥɢɡɢɪɭɸɳɟɟ ɡɧɚɱɟɧɢɟ ɩɪɢ ɨɛɴɹɜɥɟɧɢɢ ɤɨɧɫɬɚɧɬɵ
ɹɜɥɹɟɬɫɹ ɨɛɹɡɚɬɟɥɶɧɵɦ.
ɇɚɪɹɞɭ ɫ ɛɚɡɨɜɵɦɢ ɰɟɥɵɦɢ ɢ ɜɟɳɟɫɬɜɟɧɧɵɦɢ ɬɢɩɚɦɢ ɪɚɡɥɢɱɧɵɯ ɪɚɡ-
ɦɟɪɨɜ ɜ ɩɪɨɝɪɚɦɦɟ ɦɨɝɭɬ ɨɛɴɹɜɥɹɬɶɫɹ ɢ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɞɚɧɧɵɟ ɬɢɩɨɜ, ɨɩ-
ɪɟɞɟɥɹɟɦɵɯ ɩɪɨɝɪɚɦɦɢɫɬɨɦ: ɭɤɚɡɚɬɟɥɢ, ɫɫɵɥɤɢ, ɚɝɪɟɝɚɬɵ ɞɚɧɧɵɯ ɢ ɞɚɧɧɵɟ
ɩɟɪɟɱɢɫɥɢɦɨɝɨ ɬɢɩɚ.
11
     int i, j, k;         /* Ɍɪɢ ɩɟɪɟɦɟɧɧɵɯ ɬɢɩɚ int ɛɟɡ ɹɜɧɨɣ
                             ɢɧɢɰɢɚɥɢɡɚɰɢɢ */
     double x=1, y=2;     /* Ⱦɜɟ ɩɟɪɟɦɟɧɧɵɯ ɬɢɩɚ double
                             ɫ ɧɚɱɚɥɶɧɵɦɢ ɡɧɚɱɟɧɢɹɦɢ 1 ɢ 2 */
     char c1='0';         /* ɉɟɪɟɦɟɧɧɚɹ ɬɢɩɚ char, ɟɟ ɡɧɚɱɟɧɢɟ –
                             ɤɨɞ ɥɢɬɟɪɵ 0 */
    Ɍɟɤɫɬ, ɡɚɩɢɫɚɧɧɵɣ ɜ ɷɬɢɯ ɩɪɢɦɟɪɚɯ ɩɨɫɥɟ ɡɧɚɤɨɜ //, ɹɜɥɹɟɬɫɹ ɤɨɦ-
ɦɟɧɬɚɪɢɟɦ ɢ ɫɥɭɠɢɬ ɬɨɥɶɤɨ ɞɥɹ ɞɨɤɭɦɟɧɬɢɪɨɜɚɧɢɹ ɩɪɨɝɪɚɦɦɵ. Ɍɚɤɨɣ
ɤɨɦɦɟɧɬɚɪɢɣ ɦɨɠɟɬ ɡɚɧɢɦɚɬɶ ɬɨɥɶɤɨ ɨɞɧɭ ɫɬɪɨɤɭ ɬɟɤɫɬɚ ɢ ɞɨɩɭɫɤɚɟɬɫɹ ɜ
ɬɟɤɫɬɚɯ ɩɪɨɝɪɚɦɦ ɧɚ ɋɢ++. Ʉɨɦɦɟɧɬɚɪɢɣ, ɡɚɧɢɦɚɸɳɢɣ ɧɟɫɤɨɥɶɤɨ ɫɬɪɨɤ,
ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɫɩɟɰɢɚɥɶɧɵɟ ɫɤɨɛɤɢ /* ɢ */.
    ȼ ɤɚɱɟɫɬɜɟ ɫɩɟɰɢɮɢɤɚɬɨɪɨɜ ɤɥɚɫɫɚ ɩɚɦɹɬɢ ɜɨ ɜɧɟɲɧɟɦ ɨɩɪɟɞɟɥɟɧɢɢ
ɦɨɠɟɬ ɭɤɚɡɵɜɚɬɶɫɹ ɨɞɧɨ ɢɡ ɤɥɸɱɟɜɵɯ ɫɥɨɜ extern, static ɢɥɢ typedef.
ɋɩɟɰɢɮɢɤɚɬɨɪ extern ɨɡɧɚɱɚɟɬ, ɱɬɨ ɨɛɴɹɜɥɹɟɦɵɣ ɨɛɴɟɤɬ ɩɪɢɧɚɞɥɟɠɢɬ
ɞɪɭɝɨɦɭ ɩɪɨɝɪɚɦɦɧɨɦɭ ɮɚɣɥɭ, ɚ ɜ ɞɚɧɧɨɦ ɮɚɣɥɟ ɫɨɞɟɪɠɢɬɫɹ ɬɨɥɶɤɨ ɢɧ-
ɮɨɪɦɚɰɢɹ ɨ ɟɝɨ ɬɢɩɟ ɢ ɢɦɟɧɢ ɢ ɧɟ ɞɨɥɠɧɨ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɢɧɢɰɢɚɥɢɡɢ-
ɪɭɸɳɟɟ ɜɵɪɚɠɟɧɢɟ. ɋɩɟɰɢɮɢɤɚɬɨɪ static ɨɝɪɚɧɢɱɢɜɚɟɬ ɨɛɥɚɫɬɶ ɞɟɣɫɬɜɢɹ
ɨɛɴɹɜɥɹɟɦɨɝɨ ɢɦɟɧɢ ɞɚɧɧɵɦ ɮɚɣɥɨɦ ɢɥɢ ɛɥɨɤɨɦ, ɟɫɥɢ ɨɛɴɹɜɥɟɧɢɟ ɫɨɞɟɪ-
ɠɢɬɫɹ ɜ ɛɥɨɤɟ.
    ȿɫɥɢ ɨɛɴɹɜɥɟɧɢɟ ɞɚɧɧɨɝɨ ɫɨɞɟɪɠɢɬɫɹ ɜɧɭɬɪɢ ɬɟɥɚ ɮɭɧɤɰɢɢ (ɥɨɤɚɥɶɧɨɟ
ɨɛɴɹɜɥɟɧɢɟ), ɬɨ ɦɨɠɧɨ ɭɤɚɡɵɜɚɬɶ ɫɩɟɰɢɮɢɤɚɬɨɪɵ ɤɥɚɫɫɚ ɩɚɦɹɬɢ register
ɢɥɢ auto. ɋɩɟɰɢɮɢɤɚɬɨɪ register ɧɨɫɢɬ ɪɟɤɨɦɟɧɞɚɬɟɥɶɧɵɣ ɯɚɪɚɤɬɟɪ,
ɤɨɦɩɢɥɹɬɨɪ ɩɵɬɚɟɬɫɹ ɪɚɡɦɟɫɬɢɬɶ ɞɚɧɧɵɟ ɷɬɨɝɨ ɤɥɚɫɫɚ ɜ ɪɟɝɢɫɬɪɟ ɩɪɨɰɟɫ-
ɫɨɪɚ, ɟɫɥɢ ɜ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɢɦɟɸɬɫɹ ɫɜɨɛɨɞɧɵɟ ɪɟɝɢɫɬɪɵ. ɋɩɟɰɢɮɢɤɚɬɨɪ
auto ɩɪɢɧɢɦɚɟɬɫɹ ɩɨ ɭɦɨɥɱɚɧɢɸ ɢ ɩɨɷɬɨɦɭ ɹɜɧɨ ɧɟ ɭɤɚɡɵɜɚɟɬɫɹ, ɨɧ ɨɡɧɚ-
ɱɚɟɬ, ɱɬɨ ɞɚɧɧɵɟ ɤɥɚɫɫɚ auto ɞɨɥɠɧɵ ɪɚɡɦɟɳɚɬɶɫɹ ɜ ɩɪɨɝɪɚɦɦɧɨɦ ɫɬɟɤɟ
ɩɪɢ ɜɵɡɨɜɟ ɮɭɧɤɰɢɢ.
    ɋɩɟɰɢɮɢɤɚɬɨɪ typedef ɫɥɭɠɢɬ ɞɥɹ ɩɪɢɫɜɨɟɧɢɹ ɢɦɟɧɢ ɨɩɢɫɵɜɚɟɦɨɦɭ
ɬɢɩɭ ɞɚɧɧɵɯ ɢ ɛɭɞɟɬ ɪɚɫɫɦɨɬɪɟɧ ɩɨɞɪɨɛɧɟɟ ɜ ɫɥɟɞɭɸɳɟɦ ɩɚɪɚɝɪɚɮɟ.
    ɇɚɪɹɞɭ ɫ ɩɨɤɚɡɚɧɧɵɦɢ ɜɵɲɟ ɤɨɧɫɬɚɧɬɚɦɢ-ɥɢɬɟɪɚɥɚɦɢ, ɡɧɚɱɟɧɢɹ ɤɨɬɨ-
ɪɵɯ ɨɩɪɟɞɟɥɹɸɬɫɹ ɢɯ ɩɪɟɞɫɬɚɜɥɟɧɢɟɦ ɜ ɩɪɨɝɪɚɦɦɟ, ɜ ɋɢ ɢ ɋɢ++ ɩɪɟɞɭ-
ɫɦɨɬɪɟɧɵ ɤɨɧɫɬɚɧɬɵ, ɤɨɬɨɪɵɦ ɩɪɢɫɜɚɢɜɚɸɬɫɹ ɫɨɛɫɬɜɟɧɧɵɟ ɢɦɟɧɚ - ɢɦɟɧɨ-
ɜɚɧɧɵɟ ɤɨɧɫɬɚɧɬɵ. ȼ ɨɩɢɫɚɧɢɢ ɢɦɟɧɨɜɚɧɧɨɣ ɤɨɧɫɬɚɧɬɵ ɩɪɢɫɭɬɫɬɜɭɟɬ ɨɩɢ-
ɫɚɬɟɥɶ const, ɧɚɩɪɢɦɟɪ,
     const double Pi=3.141592653;
    ɉɟɪɟɦɟɧɧɨɣ, ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɤɨɬɨɪɨɣ ɨɛɴɹɜɥɟɧ ɫ ɨɩɢɫɚɬɟɥɟɦ const,
ɧɟɥɶɡɹ ɩɪɢɫɜɨɢɬɶ ɢɧɨɟ ɡɧɚɱɟɧɢɟ, ɱɟɦ ɛɵɥɨ ɭɫɬɚɧɨɜɥɟɧɨ ɩɪɢ ɨɛɴɹɜɥɟɧɢɢ
ɢɞɟɧɬɢɮɢɤɚɬɨɪɚ. ɂɧɢɰɢɚɥɢɡɢɪɭɸɳɟɟ ɡɧɚɱɟɧɢɟ ɩɪɢ ɨɛɴɹɜɥɟɧɢɢ ɤɨɧɫɬɚɧɬɵ
ɹɜɥɹɟɬɫɹ ɨɛɹɡɚɬɟɥɶɧɵɦ.
    ɇɚɪɹɞɭ ɫ ɛɚɡɨɜɵɦɢ ɰɟɥɵɦɢ ɢ ɜɟɳɟɫɬɜɟɧɧɵɦɢ ɬɢɩɚɦɢ ɪɚɡɥɢɱɧɵɯ ɪɚɡ-
ɦɟɪɨɜ ɜ ɩɪɨɝɪɚɦɦɟ ɦɨɝɭɬ ɨɛɴɹɜɥɹɬɶɫɹ ɢ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɞɚɧɧɵɟ ɬɢɩɨɜ, ɨɩ-
ɪɟɞɟɥɹɟɦɵɯ ɩɪɨɝɪɚɦɦɢɫɬɨɦ: ɭɤɚɡɚɬɟɥɢ, ɫɫɵɥɤɢ, ɚɝɪɟɝɚɬɵ ɞɚɧɧɵɯ ɢ ɞɚɧɧɵɟ
ɩɟɪɟɱɢɫɥɢɦɨɝɨ ɬɢɩɚ.

                                  11