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

UptoLike

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

ɡɜɨɥɹɟɬ ɪɚɡɪɚɛɚɬɵɜɚɬɶ ɤɚɤ 16-ɪɚɡɪɹɞɧɵɟ, ɬɚɤ ɢ 32-ɪɚɡɪɹɞɧɵɟ ɜɚɪɢɚɧɬɵ
ɩɪɨɝɪɚɦɦ ɞɥɹ ɉɗȼɆ ɫ ɩɪɨɰɟɫɫɨɪɚɦɢ i486 ɢ Pentium.
ɉɪɨɝɪɚɦɦɚ ɧɚ ɋɢ/ɋɢ++ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɨɞɢɧ ɢɥɢ ɧɟɫɤɨɥɶɤɨ ɢɫ-
ɯɨɞɧɵɯ ɮɚɣɥɨɜ, ɤɨɬɨɪɵɟ ɦɨɝɭɬ ɬɪɚɧɫɥɢɪɨɜɚɬɶɫɹ ɪɚɡɞɟɥɶɧɨ. Ɋɟɡɭɥɶɬɚɬɵ
ɬɪɚɧɫɥɹɰɢɢ (ɨɛɴɟɤɬɧɵɟ ɮɚɣɥɵ) ɨɛɴɟɞɢɧɹɸɬɫɹ ɜ ɢɫɩɨɥɧɹɟɦɵɣ ɮɚɣɥ ɪɟɞɚɤ-
ɬɨɪɨɦ ɫɜɹɡɟɣ (ɤɨɦɩɨɧɨɜɳɢɤɨɦ). Ɉɛɵɱɧɨ ɪɚɡɥɢɱɚɸɬ ɞɜɚ ɬɢɩɚ ɢɫɯɨɞɧɵɯ
ɮɚɣɥɨɜ: ɮɚɣɥɵ ɡɚɝɨɥɨɜɤɨɜ ɢ ɩɪɨɝɪɚɦɦɧɵɟ ɮɚɣɥɵ. Ɏɚɣɥɵ ɡɚɝɨɥɨɜɤɨɜ ɫɨ-
ɞɟɪɠɚɬ ɨɩɢɫɚɧɢɹ ɬɢɩɨɜ ɞɚɧɧɵɯ ɢ ɩɪɨɬɨɬɢɩɨɜ ɮɭɧɤɰɢɣ ɢ ɩɪɟɞɧɚɡɧɚɱɟɧɵ
ɞɥɹ ɜɤɥɸɱɟɧɢɹ ɜ ɩɪɨɝɪɚɦɦɧɵɟ ɮɚɣɥɵ ɩɟɪɟɞ ɢɯ ɤɨɦɩɢɥɹɰɢɟɣ. ɂɯ ɢɦɟɧɚ,
ɤɚɤ ɩɪɚɜɢɥɨ, ɢɦɟɸɬ ɪɚɫɲɢɪɟɧɢɟ
.h, ɧɚɩɪɢɦɟɪ, stdio.h. ɉɪɨɝɪɚɦɦɧɵɟ
ɮɚɣɥɵ ɫɨɞɟɪɠɚɬ ɨɩɢɫɚɧɢɹ ɮɭɧɤɰɢɣ ɢ, ɜɨɡɦɨɠɧɨ, ɝɥɨɛɚɥɶɧɵɯ ɩɟɪɟɦɟɧɧɵɯ
ɢ ɤɨɧɫɬɚɧɬ, ɢɯ ɢɦɟɧɚ ɩɪɢɧɹɬɨ ɡɚɩɢɫɵɜɚɬɶ ɫ ɪɚɫɲɢɪɟɧɢɹɦɢ
.c ɢɥɢ .cpp,
ɧɚɩɪɢɦɟɪ,
myprog.cpp. Ɉɞɢɧ ɢ ɬɨɬ ɠɟ ɮɚɣɥ ɡɚɝɨɥɨɜɤɨɜ ɦɨɠɟɬ ɜɤɥɸɱɚɬɶɫɹ
ɜ ɧɟɫɤɨɥɶɤɨ ɩɪɨɝɪɚɦɦɧɵɯ ɮɚɣɥɨɜ.
Ʉɚɠɞɵɣ ɮɚɣɥ ɫɨɞɟɪɠɢɬ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɬɚɤ ɧɚɡɵɜɚɟɦɵɯ «ɜɧɟɲ-
ɧɢɯ ɨɩɪɟɞɟɥɟɧɢɣ», ɨɩɢɫɵɜɚɸɳɢɯ ɬɢɩɵ ɞɚɧɧɵɯ, ɩɟɪɟɦɟɧɧɵɟ, ɤɨɧɫɬɚɧɬɵ ɢ
ɮɭɧɤɰɢɢ.
ȼ ɩɨɫɥɟɞɭɸɳɢɯ ɩɚɪɚɝɪɚɮɚɯ ɷɬɨɝɨ ɪɚɡɞɟɥɚ ɩɪɢɜɟɞɟɧ ɨɛɡɨɪ ɫɪɟɞɫɬɜ
ɋɢ/ɋɢ++, ɧɟ ɫɜɹɡɚɧɧɵɯ ɫ ɨɛɴɟɤɬɧɨɣ ɨɪɢɟɧɬɚɰɢɟɣ ɋɢ++.
2.2. Ⱥɥɮɚɜɢɬ, ɛɚɡɨɜɵɟ ɬɢɩɵ ɢ ɨɩɢɫɚɧɢɟ ɞɚɧɧɵɯ
Ⱥɥɮɚɜɢɬ ɹɡɵɤɚ ɜɤɥɸɱɚɟɬ ɩɪɚɤɬɢɱɟɫɤɢ ɜɫɟ ɫɢɦɜɨɥɵ, ɢɦɟɸɳɢɟɫɹ ɧɚ
ɫɬɚɧɞɚɪɬɧɨɣ ɤɥɚɜɢɚɬɭɪɟ ɉɗȼɆ:
- ɥɚɬɢɧɫɤɢɟ ɛɭɤɜɵ
A...Z, a...z;
- ɰɢɮɪɵ
0...9;
- ɡɧɚɤɢ ɨɩɟɪɚɰɢɣ ɢ ɪɚɡɞɟɥɢɬɟɥɢ:
{ } [ ] ( ) . , -> & * + - ~ ! / % ? : ; = < > | # ^
ɇɟɤɨɬɨɪɵɟ ɨɩɟɪɚɰɢɢ ɨɛɨɡɧɚɱɚɸɬɫɹ ɤɨɦɛɢɧɚɰɢɹɦɢ ɫɢɦɜɨɥɨɜ, ɡɧɚɱɟɧɢɹ
ɫɢɦɜɨɥɨɜ ɨɩɟɪɚɰɢɣ ɜ ɪɹɞɟ ɫɥɭɱɚɟɜ ɡɚɜɢɫɹɬ ɨɬ ɤɨɧɬɟɤɫɬɚ, ɜ ɤɨɬɨɪɨɦ ɨɧɢ
ɭɩɨɬɪɟɛɥɟɧɵ.
Ȼɚɡɨɜɵɟ (ɩɪɟɞɨɩɪɟɞɟɥɟɧɧɵɟ) ɬɢɩɵ ɞɚɧɧɵɯ ɨɛɴɟɞɢɧɟɧɵ ɜ ɞɜɟ ɝɪɭɩɩɵ:
ɞɚɧɧɵɟ ɰɟɥɵɯ ɬɢɩɨɜ ɢ ɞɚɧɧɵɟ ɜɟɳɟɫɬɜɟɧɧɵɯ ɬɢɩɨɜ.
Ⱦɚɧɧɵɟ ɰɟɥɵɯ ɬɢɩɨɜ ɦɨɝɭɬ ɛɵɬɶ ɨɛɵɱɧɵɦɢ ɰɟɥɵɦɢ ɫɨ ɡɧɚɤɨɦ
(
signed) ɢ ɰɟɥɵɦɢ ɛɟɡ ɡɧɚɤɚ (unsigned). ɉɨ ɱɢɫɥɭ ɪɚɡɪɹɞɨɜ, ɢɫɩɨɥɶɡɭɟɦɵɯ
ɞɥɹ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɞɚɧɧɵɯ (ɞɢɚɩɚɡɨɧɭ ɡɧɚɱɟɧɢɣ), ɪɚɡɥɢɱɚɸɬ ɨɛɵɱɧɵɟ ɰɟ-
ɥɵɟ (
int), ɤɨɪɨɬɤɢɟ ɰɟɥɵɟ (short int) ɢ ɞɥɢɧɧɵɟ ɰɟɥɵɟ (long int). ɋɢɦ-
ɜɨɥɶɧɵɟ ɞɚɧɧɵɟ (
char) ɬɚɤɠɟ ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɤɚɤ ɰɟɥɵɟ.
Ʉɨɧɫɬɚɧɬɵ ɰɟɥɨɝɨ ɬɢɩɚ ɡɚɩɢɫɵɜɚɸɬɫɹ ɤɚɤ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɞɟɫɹ-
ɬɢɱɧɵɯ ɰɢɮɪ, ɬɢɩ ɤɨɧɫɬɚɧɬɵ ɡɚɜɢɫɢɬ ɨɬ ɱɢɫɥɚ ɰɢɮɪ ɜ ɡɚɩɢɫɢ ɤɨɧɫɬɚɧɬɵ ɢ
ɦɨɠɟɬ ɛɵɬɶ ɭɬɨɱɧɟɧ ɞɨɛɚɜɥɟɧɢɟɦ ɜ ɤɨɧɰɟ ɤɨɧɫɬɚɧɬɵ ɛɭɤɜ
L ɢɥɢ l (long), U
ɢɥɢ
u (unsigned) ɢɥɢ ɢɯ ɫɨɱɟɬɚɧɢɹ:
9
ɡɜɨɥɹɟɬ ɪɚɡɪɚɛɚɬɵɜɚɬɶ ɤɚɤ 16-ɪɚɡɪɹɞɧɵɟ, ɬɚɤ ɢ 32-ɪɚɡɪɹɞɧɵɟ ɜɚɪɢɚɧɬɵ
ɩɪɨɝɪɚɦɦ ɞɥɹ ɉɗȼɆ ɫ ɩɪɨɰɟɫɫɨɪɚɦɢ i486 ɢ Pentium.
     ɉɪɨɝɪɚɦɦɚ ɧɚ ɋɢ/ɋɢ++ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɨɞɢɧ ɢɥɢ ɧɟɫɤɨɥɶɤɨ ɢɫ-
ɯɨɞɧɵɯ ɮɚɣɥɨɜ, ɤɨɬɨɪɵɟ ɦɨɝɭɬ ɬɪɚɧɫɥɢɪɨɜɚɬɶɫɹ ɪɚɡɞɟɥɶɧɨ. Ɋɟɡɭɥɶɬɚɬɵ
ɬɪɚɧɫɥɹɰɢɢ (ɨɛɴɟɤɬɧɵɟ ɮɚɣɥɵ) ɨɛɴɟɞɢɧɹɸɬɫɹ ɜ ɢɫɩɨɥɧɹɟɦɵɣ ɮɚɣɥ ɪɟɞɚɤ-
ɬɨɪɨɦ ɫɜɹɡɟɣ (ɤɨɦɩɨɧɨɜɳɢɤɨɦ). Ɉɛɵɱɧɨ ɪɚɡɥɢɱɚɸɬ ɞɜɚ ɬɢɩɚ ɢɫɯɨɞɧɵɯ
ɮɚɣɥɨɜ: ɮɚɣɥɵ ɡɚɝɨɥɨɜɤɨɜ ɢ ɩɪɨɝɪɚɦɦɧɵɟ ɮɚɣɥɵ. Ɏɚɣɥɵ ɡɚɝɨɥɨɜɤɨɜ ɫɨ-
ɞɟɪɠɚɬ ɨɩɢɫɚɧɢɹ ɬɢɩɨɜ ɞɚɧɧɵɯ ɢ ɩɪɨɬɨɬɢɩɨɜ ɮɭɧɤɰɢɣ ɢ ɩɪɟɞɧɚɡɧɚɱɟɧɵ
ɞɥɹ ɜɤɥɸɱɟɧɢɹ ɜ ɩɪɨɝɪɚɦɦɧɵɟ ɮɚɣɥɵ ɩɟɪɟɞ ɢɯ ɤɨɦɩɢɥɹɰɢɟɣ. ɂɯ ɢɦɟɧɚ,
ɤɚɤ ɩɪɚɜɢɥɨ, ɢɦɟɸɬ ɪɚɫɲɢɪɟɧɢɟ .h, ɧɚɩɪɢɦɟɪ, stdio.h. ɉɪɨɝɪɚɦɦɧɵɟ
ɮɚɣɥɵ ɫɨɞɟɪɠɚɬ ɨɩɢɫɚɧɢɹ ɮɭɧɤɰɢɣ ɢ, ɜɨɡɦɨɠɧɨ, ɝɥɨɛɚɥɶɧɵɯ ɩɟɪɟɦɟɧɧɵɯ
ɢ ɤɨɧɫɬɚɧɬ, ɢɯ ɢɦɟɧɚ ɩɪɢɧɹɬɨ ɡɚɩɢɫɵɜɚɬɶ ɫ ɪɚɫɲɢɪɟɧɢɹɦɢ .c ɢɥɢ .cpp,
ɧɚɩɪɢɦɟɪ, myprog.cpp. Ɉɞɢɧ ɢ ɬɨɬ ɠɟ ɮɚɣɥ ɡɚɝɨɥɨɜɤɨɜ ɦɨɠɟɬ ɜɤɥɸɱɚɬɶɫɹ
ɜ ɧɟɫɤɨɥɶɤɨ ɩɪɨɝɪɚɦɦɧɵɯ ɮɚɣɥɨɜ.
     Ʉɚɠɞɵɣ ɮɚɣɥ ɫɨɞɟɪɠɢɬ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɬɚɤ ɧɚɡɵɜɚɟɦɵɯ «ɜɧɟɲ-
ɧɢɯ ɨɩɪɟɞɟɥɟɧɢɣ», ɨɩɢɫɵɜɚɸɳɢɯ ɬɢɩɵ ɞɚɧɧɵɯ, ɩɟɪɟɦɟɧɧɵɟ, ɤɨɧɫɬɚɧɬɵ ɢ
ɮɭɧɤɰɢɢ.
     ȼ ɩɨɫɥɟɞɭɸɳɢɯ ɩɚɪɚɝɪɚɮɚɯ ɷɬɨɝɨ ɪɚɡɞɟɥɚ ɩɪɢɜɟɞɟɧ ɨɛɡɨɪ ɫɪɟɞɫɬɜ
ɋɢ/ɋɢ++, ɧɟ ɫɜɹɡɚɧɧɵɯ ɫ ɨɛɴɟɤɬɧɨɣ ɨɪɢɟɧɬɚɰɢɟɣ ɋɢ++.

2.2. Ⱥɥɮɚɜɢɬ, ɛɚɡɨɜɵɟ ɬɢɩɵ ɢ ɨɩɢɫɚɧɢɟ ɞɚɧɧɵɯ

     Ⱥɥɮɚɜɢɬ ɹɡɵɤɚ ɜɤɥɸɱɚɟɬ ɩɪɚɤɬɢɱɟɫɤɢ ɜɫɟ ɫɢɦɜɨɥɵ, ɢɦɟɸɳɢɟɫɹ ɧɚ
ɫɬɚɧɞɚɪɬɧɨɣ ɤɥɚɜɢɚɬɭɪɟ ɉɗȼɆ:
- ɥɚɬɢɧɫɤɢɟ ɛɭɤɜɵ A...Z, a...z;
- ɰɢɮɪɵ 0...9;
- ɡɧɚɤɢ ɨɩɟɪɚɰɢɣ ɢ ɪɚɡɞɟɥɢɬɟɥɢ:
     { } [ ] ( ) . , -> & * + - ~ ! / % ? : ; = < > | # ^
    ɇɟɤɨɬɨɪɵɟ ɨɩɟɪɚɰɢɢ ɨɛɨɡɧɚɱɚɸɬɫɹ ɤɨɦɛɢɧɚɰɢɹɦɢ ɫɢɦɜɨɥɨɜ, ɡɧɚɱɟɧɢɹ
ɫɢɦɜɨɥɨɜ ɨɩɟɪɚɰɢɣ ɜ ɪɹɞɟ ɫɥɭɱɚɟɜ ɡɚɜɢɫɹɬ ɨɬ ɤɨɧɬɟɤɫɬɚ, ɜ ɤɨɬɨɪɨɦ ɨɧɢ
ɭɩɨɬɪɟɛɥɟɧɵ.
    Ȼɚɡɨɜɵɟ (ɩɪɟɞɨɩɪɟɞɟɥɟɧɧɵɟ) ɬɢɩɵ ɞɚɧɧɵɯ ɨɛɴɟɞɢɧɟɧɵ ɜ ɞɜɟ ɝɪɭɩɩɵ:
ɞɚɧɧɵɟ ɰɟɥɵɯ ɬɢɩɨɜ ɢ ɞɚɧɧɵɟ ɜɟɳɟɫɬɜɟɧɧɵɯ ɬɢɩɨɜ.
    Ⱦɚɧɧɵɟ ɰɟɥɵɯ ɬɢɩɨɜ ɦɨɝɭɬ ɛɵɬɶ ɨɛɵɱɧɵɦɢ ɰɟɥɵɦɢ ɫɨ ɡɧɚɤɨɦ
(signed) ɢ ɰɟɥɵɦɢ ɛɟɡ ɡɧɚɤɚ (unsigned). ɉɨ ɱɢɫɥɭ ɪɚɡɪɹɞɨɜ, ɢɫɩɨɥɶɡɭɟɦɵɯ
ɞɥɹ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɞɚɧɧɵɯ (ɞɢɚɩɚɡɨɧɭ ɡɧɚɱɟɧɢɣ), ɪɚɡɥɢɱɚɸɬ ɨɛɵɱɧɵɟ ɰɟ-
ɥɵɟ (int), ɤɨɪɨɬɤɢɟ ɰɟɥɵɟ (short int) ɢ ɞɥɢɧɧɵɟ ɰɟɥɵɟ (long int). ɋɢɦ-
ɜɨɥɶɧɵɟ ɞɚɧɧɵɟ (char) ɬɚɤɠɟ ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɤɚɤ ɰɟɥɵɟ.
    Ʉɨɧɫɬɚɧɬɵ ɰɟɥɨɝɨ ɬɢɩɚ ɡɚɩɢɫɵɜɚɸɬɫɹ ɤɚɤ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɞɟɫɹ-
ɬɢɱɧɵɯ ɰɢɮɪ, ɬɢɩ ɤɨɧɫɬɚɧɬɵ ɡɚɜɢɫɢɬ ɨɬ ɱɢɫɥɚ ɰɢɮɪ ɜ ɡɚɩɢɫɢ ɤɨɧɫɬɚɧɬɵ ɢ
ɦɨɠɟɬ ɛɵɬɶ ɭɬɨɱɧɟɧ ɞɨɛɚɜɥɟɧɢɟɦ ɜ ɤɨɧɰɟ ɤɨɧɫɬɚɧɬɵ ɛɭɤɜ L ɢɥɢ l (long), U
ɢɥɢ u (unsigned) ɢɥɢ ɢɯ ɫɨɱɟɬɚɧɢɹ:

                                   9