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

UptoLike

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

ɧɢɹ ɛɭɞɟɬ ɜɵɪɚɠɟɧɢɟ1, ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ - ɜɵɪɚɠɟɧɢɟ2. ȼɬɨɪɨɣ ɢ ɬɪɟ-
ɬɢɣ ɨɩɟɪɚɧɞɵ ɭɫɥɨɜɧɨɝɨ ɜɵɪɚɠɟɧɢɹ ɥɢɛɨ ɨɛɚ ɞɨɥɠɧɵ ɛɵɬɶ ɜɵɪɚɠɟɧɢɹɦɢ
ɚɪɢɮɦɟɬɢɱɟɫɤɨɝɨ ɬɢɩɚ, ɥɢɛɨ ɨɞɧɨɬɢɩɧɵɦɢ ɫɬɪɭɤɬɭɪɚɦɢ ɢɥɢ ɨɛɴɟɞɢɧɟɧɢɹ-
ɦɢ, ɥɢɛɨ ɭɤɚɡɚɬɟɥɹɦɢ ɨɞɢɧɚɤɨɜɨɝɨ ɬɢɩɚ, ɥɢɛɨ ɨɞɢɧ ɢɡ ɧɢɯ ɞɨɥɠɟɧ ɛɵɬɶ
ɭɤɚɡɚɬɟɥɟɦ ɧɚ ɤɚɤɨɣ-ɥɢɛɨ ɬɢɩ, ɚ ɞɪɭɝɨɣ ɢɦɟɬɶ ɡɚɧɱɟɧɢɟ
NULL ɢɥɢ ɛɵɬɶ ɭɤɚ-
ɡɚɬɟɥɟɦ ɬɢɩɚ
void*. ȼɵɪɚɠɟɧɢɟ
x>0?1:0;
ɜɨɡɜɪɚɳɚɟɬ 1, ɟɫɥɢ
x ɛɨɥɶɲɟ 0, ɢ 0 ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ.
ȼɵɪɚɠɟɧɢɟ ɦɨɠɟɬ ɛɵɬɶ ɩɪɟɞɫɬɚɜɥɟɧɨ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶɸ ɜɵɪɚɠɟ-
ɧɢɣ, ɪɚɡɞɟɥɟɧɧɵɯ ɡɚɩɹɬɵɦɢ, ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɜɵɱɢɫɥɹɸɬɫɹ ɜɫɟ ɜɵɪɚɠɟɧɢɹ
ɫɥɟɜɚ ɧɚɩɪɚɜɨ ɢ ɜɨɡɜɪɚɳɚɟɬɫɹ ɡɧɚɱɟɧɢɟ ɩɨɫɥɟɞɧɟɝɨ ɜɵɪɚɠɟɧɢɹ ɜ ɫɩɢɫɤɟ.
ɇɚɩɪɢɦɟɪ, ɜ ɪɟɡɭɥɶɬɚɬɟ ɜɵɱɢɫɥɟɧɢɹ ɜɵɪɚɠɟɧɢɹ
x=2, e*3, x+1;
ɛɭɞɟɬ ɩɨɥɭɱɟɧɨ ɡɧɚɱɟɧɢɟ 3, ɚ ɩɟɪɟɦɟɧɧɚɹ x ɩɨɥɭɱɢɬ ɡɧɚɱɟɧɢɟ 2. Ɋɟɡɭɥɶɬɚɬ
ɭɦɧɨɠɟɧɢɹ
e*3 ɧɢɤɚɤ ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɢɫɩɨɥɶɡɨɜɚɧ.
2.5. Ɉɩɟɪɚɬɨɪɵ ɋɢ++
Ɉɩɟɪɚɬɨɪɵ - ɷɬɨ ɫɢɧɬɚɤɫɢɱɟɫɤɢɟ ɤɨɧɫɬɪɭɤɰɢɢ, ɨɩɪɟɞɟɥɹɸɳɢɟ ɞɟɣɫɬ-
ɜɢɹ, ɜɵɩɨɥɧɹɟɦɵɟ ɩɪɨɝɪɚɦɦɨɣ. ȼɋɢ/ɋɢ++ ɢɦɟɸɬɫɹ ɫɥɟɞɭɸɳɢɟ ɬɢɩɵ ɨɩɟ-
ɪɚɬɨɪɨɜ: ɨɩɟɪɚɬɨɪɵ-ɜɵɪɚɠɟɧɢɹ, ɨɩɟɪɚɬɨɪɵ ɜɵɛɨɪɚ, ɨɩɟɪɚɬɨɪɵ ɰɢɤɥɚ ɢ
ɨɩɟɪɚɬɨɪ ɩɟɪɟɯɨɞɚ. ɋɢɧɬɚɤɫɢɫ ɧɟɤɨɬɨɪɵɯ ɨɩɟɪɚɬɨɪɨɜ ɫɨɞɟɪɠɢɬ ɜɵɪɚɠɟ-
ɧɢɹ, ɢɝɪɚɸɳɢɟ ɪɨɥɶ ɭɫɥɨɜɢɣ, ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɜɵɩɨɥɧɟɧɢɹ ɢɥɢ ɧɟɜɵɩɨɥ-
ɧɟɧɢɹ ɤɨɬɨɪɵɯ ɜɵɛɢɪɚɟɬɫɹ ɬɚ ɢɥɢ ɢɧɚɹ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɞɟɣɫɬɜɢɣ. ɉɨ-
ɫɤɨɥɶɤɭ ɜ ɋɢ ɧɟɬ ɛɭɥɟɜɵɯ ɜɵɪɚɠɟɧɢɣ, ɜ ɤɚɱɟɫɬɜɟ ɭɫɥɨɜɢɣ ɢɫɩɨɥɶɡɭɸɬɫɹ
ɥɸɛɵɟ ɜɵɪɚɠɟɧɢɹ, ɞɚɸɳɢɟ ɫɤɚɥɹɪɧɵɟ ɡɧɚɱɟɧɢɹ, ɢ ɭɫɥɨɜɢɟ ɫɱɢɬɚɟɬɫɹ ɜɵ-
ɩɨɥɧɟɧɵɦ, ɟɫɥɢ ɷɬɨ ɡɧɚɱɟɧɢɟ ɨɬɥɢɱɧɨ ɨɬ ɧɭɥɹ, ɢ ɧɟɜɵɩɨɥɧɟɧɧɵɦ, ɟɫɥɢ ɨɧɨ
ɪɚɜɧɨ ɧɭɥɸ. ɇɟɫɤɨɥɶɤɨ ɨɩɟɪɚɬɨɪɨɜ ɦɨɝɭɬ ɛɵɬɶ ɨɛɴɟɞɢɧɟɧɵ ɜ ɫɨɫɬɚɜɧɨɣ
ɨɩɟɪɚɬɨɪ ɩɭɬɟɦ ɡɚɤɥɸɱɟɧɢɹ ɢɯ ɜ ɮɢɝɭɪɧɵɟ (ɨɩɟɪɚɬɨɪɧɵɟ) ɫɤɨɛɤɢ. ɉɪɢɡɧɚ-
ɤɨɦ ɤɨɧɰɚ ɨɩɟɪɚɬɨɪɚ (ɤɪɨɦɟ ɫɨɫɬɚɜɧɨɝɨ ɨɩɟɪɚɬɨɪɚ) ɫɥɭɠɢɬ ɬɨɱɤɚ ɫ ɡɚɩɹɬɨɣ,
ɹɜɥɹɸɳɚɹɫɹ ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɱɚɫɬɶɸ ɨɩɟɪɚɬɨɪɚ.
ɉɟɪɟɞ ɥɸɛɵɦ ɨɩɟɪɚɬɨɪɨɦ ɦɨɠɟɬ ɛɵɬɶ ɡɚɩɢɫɚɧɚ ɦɟɬɤɚ ɜ ɜɢɞɟ ɢɞɟɧɬɢ-
ɮɢɤɚɬɨɪɚ, ɨɬɞɟɥɟɧɧɨɝɨ ɨɬ ɩɨɦɟɱɚɟɦɨɝɨ ɨɩɟɪɚɬɨɪɚ ɞɜɨɟɬɨɱɢɟɦ. Ɇɟɬɤɚ ɫɥɭ-
ɠɢɬ ɬɨɥɶɤɨ ɞɥɹ ɭɤɚɡɚɧɢɹ ɟɟ ɜ ɨɩɟɪɚɬɨɪɟ ɩɟɪɟɯɨɞɚ.
ɇɚɢɛɨɥɟɟ ɩɪɨɫɬɵɦ ɹɜɥɹɟɬɫɹ ɨɩɟɪɚɬɨɪ-ɜɵɪɚɠɟɧɢɟ, ɩɪɟɞɫɬɚɜɥɹɸɳɢɣ
ɫɨɛɨɣ ɩɨɥɧɨɟ ɜɵɪɚɠɟɧɢɟ, ɡɚɤɧɱɢɜɚɸɳɟɟɫɹ ɬɨɱɤɨɣ ɫ ɡɚɩɹɬɨɣ, ɧɚɩɪɢɦɟɪ,
x=3; y=(x+1)*t; i++;
ȼɵɪɚɠɟɧɢɟ, ɨɮɨɪɦɥɟɧɧɨɟ ɤɚɤ ɨɩɟɪɚɬɨɪ, ɜɵɱɢɫɥɹɟɬɫɹ, ɧɨ ɟɝɨ ɡɧɚɱɟɧɢɟ
ɬɟɪɹɟɬɫɹ, ɢ ɞɟɣɫɬɜɢɟ ɨɩɟɪɚɬɨɪɚ-ɜɵɪɚɠɟɧɢɹ ɫɨɫɬɨɢɬ ɜ ɩɨɛɨɱɧɵɯ ɷɮɮɟɤɬɚɯ,
ɫɨɩɪɨɜɨɠɞɚɸɳɢɯ ɜɵɱɢɫɥɟɧɢɟ, ɧɚɩɪɢɦɟɪ, ɩɪɢ ɜɵɩɨɥɧɟɧɢɢ ɨɩɟɪɚɰɢɣ ɩɪɢ-
ɫɜɚɢɜɚɧɢɹ, ɚɜɬɨɭɜɟɥɢɱɟɧɢɹ ɢ ɚɜɬɨɭɦɟɧɶɲɟɧɢɹ.
Ɉɩɟɪɚɬɨɪɵ ɜɵɛɨɪɚ ɜ ɋɢ/ɋɢ++ ɩɪɟɞɫɬɚɜɥɟɧɵ ɭɫɥɨɜɧɵɦ ɨɩɟɪɚɬɨɪɨɦ ɢ
ɩɟɪɟɤɥɸɱɚɬɟɥɟɦ. ɍɫɥɨɜɧɵɣ ɨɩɟɪɚɬɨɪ ɪɚɛɨɬɚɟɬ ɚɧɚɥɨɝɢɱɧɨ ɭɫɥɨɜɧɵɦ ɨɩɟ-
26
ɧɢɹ ɛɭɞɟɬ ɜɵɪɚɠɟɧɢɟ1, ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ - ɜɵɪɚɠɟɧɢɟ2. ȼɬɨɪɨɣ ɢ ɬɪɟ-
ɬɢɣ ɨɩɟɪɚɧɞɵ ɭɫɥɨɜɧɨɝɨ ɜɵɪɚɠɟɧɢɹ ɥɢɛɨ ɨɛɚ ɞɨɥɠɧɵ ɛɵɬɶ ɜɵɪɚɠɟɧɢɹɦɢ
ɚɪɢɮɦɟɬɢɱɟɫɤɨɝɨ ɬɢɩɚ, ɥɢɛɨ ɨɞɧɨɬɢɩɧɵɦɢ ɫɬɪɭɤɬɭɪɚɦɢ ɢɥɢ ɨɛɴɟɞɢɧɟɧɢɹ-
ɦɢ, ɥɢɛɨ ɭɤɚɡɚɬɟɥɹɦɢ ɨɞɢɧɚɤɨɜɨɝɨ ɬɢɩɚ, ɥɢɛɨ ɨɞɢɧ ɢɡ ɧɢɯ ɞɨɥɠɟɧ ɛɵɬɶ
ɭɤɚɡɚɬɟɥɟɦ ɧɚ ɤɚɤɨɣ-ɥɢɛɨ ɬɢɩ, ɚ ɞɪɭɝɨɣ ɢɦɟɬɶ ɡɚɧɱɟɧɢɟ NULL ɢɥɢ ɛɵɬɶ ɭɤɚ-
ɡɚɬɟɥɟɦ ɬɢɩɚ void*. ȼɵɪɚɠɟɧɢɟ
     x>0?1:0;
ɜɨɡɜɪɚɳɚɟɬ 1, ɟɫɥɢ x ɛɨɥɶɲɟ 0, ɢ 0 ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ.
    ȼɵɪɚɠɟɧɢɟ ɦɨɠɟɬ ɛɵɬɶ ɩɪɟɞɫɬɚɜɥɟɧɨ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶɸ ɜɵɪɚɠɟ-
ɧɢɣ, ɪɚɡɞɟɥɟɧɧɵɯ ɡɚɩɹɬɵɦɢ, ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɜɵɱɢɫɥɹɸɬɫɹ ɜɫɟ ɜɵɪɚɠɟɧɢɹ
ɫɥɟɜɚ ɧɚɩɪɚɜɨ ɢ ɜɨɡɜɪɚɳɚɟɬɫɹ ɡɧɚɱɟɧɢɟ ɩɨɫɥɟɞɧɟɝɨ ɜɵɪɚɠɟɧɢɹ ɜ ɫɩɢɫɤɟ.
ɇɚɩɪɢɦɟɪ, ɜ ɪɟɡɭɥɶɬɚɬɟ ɜɵɱɢɫɥɟɧɢɹ ɜɵɪɚɠɟɧɢɹ
     x=2, e*3, x+1;
ɛɭɞɟɬ ɩɨɥɭɱɟɧɨ ɡɧɚɱɟɧɢɟ 3, ɚ ɩɟɪɟɦɟɧɧɚɹ x ɩɨɥɭɱɢɬ ɡɧɚɱɟɧɢɟ 2. Ɋɟɡɭɥɶɬɚɬ
ɭɦɧɨɠɟɧɢɹ e*3 ɧɢɤɚɤ ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɢɫɩɨɥɶɡɨɜɚɧ.

2.5. Ɉɩɟɪɚɬɨɪɵ ɋɢ++

     Ɉɩɟɪɚɬɨɪɵ - ɷɬɨ ɫɢɧɬɚɤɫɢɱɟɫɤɢɟ ɤɨɧɫɬɪɭɤɰɢɢ, ɨɩɪɟɞɟɥɹɸɳɢɟ ɞɟɣɫɬ-
ɜɢɹ, ɜɵɩɨɥɧɹɟɦɵɟ ɩɪɨɝɪɚɦɦɨɣ. ȼ ɋɢ/ɋɢ++ ɢɦɟɸɬɫɹ ɫɥɟɞɭɸɳɢɟ ɬɢɩɵ ɨɩɟ-
ɪɚɬɨɪɨɜ: ɨɩɟɪɚɬɨɪɵ-ɜɵɪɚɠɟɧɢɹ, ɨɩɟɪɚɬɨɪɵ ɜɵɛɨɪɚ, ɨɩɟɪɚɬɨɪɵ ɰɢɤɥɚ ɢ
ɨɩɟɪɚɬɨɪ ɩɟɪɟɯɨɞɚ. ɋɢɧɬɚɤɫɢɫ ɧɟɤɨɬɨɪɵɯ ɨɩɟɪɚɬɨɪɨɜ ɫɨɞɟɪɠɢɬ ɜɵɪɚɠɟ-
ɧɢɹ, ɢɝɪɚɸɳɢɟ ɪɨɥɶ ɭɫɥɨɜɢɣ, ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɜɵɩɨɥɧɟɧɢɹ ɢɥɢ ɧɟɜɵɩɨɥ-
ɧɟɧɢɹ ɤɨɬɨɪɵɯ ɜɵɛɢɪɚɟɬɫɹ ɬɚ ɢɥɢ ɢɧɚɹ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɞɟɣɫɬɜɢɣ. ɉɨ-
ɫɤɨɥɶɤɭ ɜ ɋɢ ɧɟɬ ɛɭɥɟɜɵɯ ɜɵɪɚɠɟɧɢɣ, ɜ ɤɚɱɟɫɬɜɟ ɭɫɥɨɜɢɣ ɢɫɩɨɥɶɡɭɸɬɫɹ
ɥɸɛɵɟ ɜɵɪɚɠɟɧɢɹ, ɞɚɸɳɢɟ ɫɤɚɥɹɪɧɵɟ ɡɧɚɱɟɧɢɹ, ɢ ɭɫɥɨɜɢɟ ɫɱɢɬɚɟɬɫɹ ɜɵ-
ɩɨɥɧɟɧɵɦ, ɟɫɥɢ ɷɬɨ ɡɧɚɱɟɧɢɟ ɨɬɥɢɱɧɨ ɨɬ ɧɭɥɹ, ɢ ɧɟɜɵɩɨɥɧɟɧɧɵɦ, ɟɫɥɢ ɨɧɨ
ɪɚɜɧɨ ɧɭɥɸ. ɇɟɫɤɨɥɶɤɨ ɨɩɟɪɚɬɨɪɨɜ ɦɨɝɭɬ ɛɵɬɶ ɨɛɴɟɞɢɧɟɧɵ ɜ ɫɨɫɬɚɜɧɨɣ
ɨɩɟɪɚɬɨɪ ɩɭɬɟɦ ɡɚɤɥɸɱɟɧɢɹ ɢɯ ɜ ɮɢɝɭɪɧɵɟ (ɨɩɟɪɚɬɨɪɧɵɟ) ɫɤɨɛɤɢ. ɉɪɢɡɧɚ-
ɤɨɦ ɤɨɧɰɚ ɨɩɟɪɚɬɨɪɚ (ɤɪɨɦɟ ɫɨɫɬɚɜɧɨɝɨ ɨɩɟɪɚɬɨɪɚ) ɫɥɭɠɢɬ ɬɨɱɤɚ ɫ ɡɚɩɹɬɨɣ,
ɹɜɥɹɸɳɚɹɫɹ ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɱɚɫɬɶɸ ɨɩɟɪɚɬɨɪɚ.
     ɉɟɪɟɞ ɥɸɛɵɦ ɨɩɟɪɚɬɨɪɨɦ ɦɨɠɟɬ ɛɵɬɶ ɡɚɩɢɫɚɧɚ ɦɟɬɤɚ ɜ ɜɢɞɟ ɢɞɟɧɬɢ-
ɮɢɤɚɬɨɪɚ, ɨɬɞɟɥɟɧɧɨɝɨ ɨɬ ɩɨɦɟɱɚɟɦɨɝɨ ɨɩɟɪɚɬɨɪɚ ɞɜɨɟɬɨɱɢɟɦ. Ɇɟɬɤɚ ɫɥɭ-
ɠɢɬ ɬɨɥɶɤɨ ɞɥɹ ɭɤɚɡɚɧɢɹ ɟɟ ɜ ɨɩɟɪɚɬɨɪɟ ɩɟɪɟɯɨɞɚ.
     ɇɚɢɛɨɥɟɟ ɩɪɨɫɬɵɦ ɹɜɥɹɟɬɫɹ ɨɩɟɪɚɬɨɪ-ɜɵɪɚɠɟɧɢɟ, ɩɪɟɞɫɬɚɜɥɹɸɳɢɣ
ɫɨɛɨɣ ɩɨɥɧɨɟ ɜɵɪɚɠɟɧɢɟ, ɡɚɤɧɱɢɜɚɸɳɟɟɫɹ ɬɨɱɤɨɣ ɫ ɡɚɩɹɬɨɣ, ɧɚɩɪɢɦɟɪ,
     x=3; y=(x+1)*t; i++;
    ȼɵɪɚɠɟɧɢɟ, ɨɮɨɪɦɥɟɧɧɨɟ ɤɚɤ ɨɩɟɪɚɬɨɪ, ɜɵɱɢɫɥɹɟɬɫɹ, ɧɨ ɟɝɨ ɡɧɚɱɟɧɢɟ
ɬɟɪɹɟɬɫɹ, ɢ ɞɟɣɫɬɜɢɟ ɨɩɟɪɚɬɨɪɚ-ɜɵɪɚɠɟɧɢɹ ɫɨɫɬɨɢɬ ɜ ɩɨɛɨɱɧɵɯ ɷɮɮɟɤɬɚɯ,
ɫɨɩɪɨɜɨɠɞɚɸɳɢɯ ɜɵɱɢɫɥɟɧɢɟ, ɧɚɩɪɢɦɟɪ, ɩɪɢ ɜɵɩɨɥɧɟɧɢɢ ɨɩɟɪɚɰɢɣ ɩɪɢ-
ɫɜɚɢɜɚɧɢɹ, ɚɜɬɨɭɜɟɥɢɱɟɧɢɹ ɢ ɚɜɬɨɭɦɟɧɶɲɟɧɢɹ.
    Ɉɩɟɪɚɬɨɪɵ ɜɵɛɨɪɚ ɜ ɋɢ/ɋɢ++ ɩɪɟɞɫɬɚɜɥɟɧɵ ɭɫɥɨɜɧɵɦ ɨɩɟɪɚɬɨɪɨɦ ɢ
ɩɟɪɟɤɥɸɱɚɬɟɥɟɦ. ɍɫɥɨɜɧɵɣ ɨɩɟɪɚɬɨɪ ɪɚɛɨɬɚɟɬ ɚɧɚɥɨɝɢɱɧɨ ɭɫɥɨɜɧɵɦ ɨɩɟ-

                                   26