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

UptoLike

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

Ⱦɚɧɧɵɟ, ɪɚɡɦɟɳɟɧɧɵɟ ɜ ɞɢɧɚɦɢɱɟɫɤɨɣ ɩɚɦɹɬɢ ɨɩɟɪɚɰɢɟɣ new, ɭɞɚɥɹ-
ɸɬɫɹ ɢɡ ɩɚɦɹɬɢ ɨɩɟɪɚɰɢɟɣ
delete ɫ ɨɩɟɪɚɧɞɨɦ-ɭɤɚɡɚɬɟɥɟɦ, ɡɧɚɱɟɧɢɟ ɤɨɬɨ-
ɪɨɝɨ ɩɨɥɭɱɟɧɨ ɨɩɟɪɚɰɢɟɣ
new, ɧɚɩɪɢɦɟɪ,
delete intArray;
delete ip2;
Ɉɩɟɪɚɰɢɹ delete ɬɨɥɶɤɨ ɨɫɜɨɛɨɠɞɚɟɬ ɞɢɧɚɦɢɱɟɫɤɭɸ ɩɚɦɹɬɶ, ɧɨ ɧɟ
ɢɡɦɟɧɹɟɬ ɡɧɚɱɟɧɢɟ ɭɤɚɡɚɬɟɥɹ-ɨɩɟɪɚɧɞɚ. ɉɪɨɝɪɚɦɦɢɫɬ ɞɨɥɠɟɧ ɩɨɦɧɢɬɶ, ɱɬɨ
ɩɨɫɥɟ ɨɫɜɨɛɨɠɞɟɧɢɹ ɩɚɦɹɬɢ ɢɫɩɨɥɶɡɨɜɚɬɶ ɷɬɨɬ ɭɤɚɡɚɬɟɥɶ ɞɥɹ ɨɛɪɚɳɟɧɢɹ ɤ
ɞɚɧɧɵɦ ɧɟɥɶɡɹ.
Ɋɚɡɦɟɪ ɞɚɧɧɨɝɨ ɢɥɢ ɬɢɩɚ ɞɚɧɧɨɝɨ ɜ ɛɚɣɬɚɯ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɩɨ ɨɩɟɪɚ-
ɰɢɢ
sizeof. Ɉɩɟɪɚɧɞ ɦɨɠɟɬ ɛɵɬɶ ɥɸɛɨɝɨ ɬɢɩɚ, ɤɪɨɦɟ ɬɢɩɚ ɮɭɧɤɰɢɢ ɢ ɛɢ-
ɬɨɜɨɝɨ ɩɨɥɹ. ȿɫɥɢ ɨɩɟɪɚɧɞɨɦ ɹɜɥɹɟɬɫɹ ɢɦɹ ɬɢɩɚ, ɨɧɨ ɞɨɥɠɧɨ ɡɚɤɥɸɱɚɬɶɫɹ ɜ
ɫɤɨɛɤɢ. ȼɨɡɜɪɚɳɚɟɦɨɟ ɡɧɚɱɟɧɢɟ ɢɦɟɟɬ ɩɪɟɞɨɩɪɟɞɟɥɟɧɧɵɣ ɬɢɩ
size_t -
ɰɟɥɵɣ ɬɢɩ, ɪɚɡɦɟɪ ɤɨɬɨɪɨɝɨ ɨɩɪɟɞɟɥɹɟɬɫɹ ɪɟɚɥɢɡɚɰɢɟɣ ɤɨɦɩɢɥɹɬɨɪɚ.
Ɉɛɵɱɧɨ ɬɢɩɭ
size_t ɫɨɨɬɜɟɬɫɬɜɭɟɬ unsigned int. Ɋɚɡɦɟɪ ɦɚɫɫɢɜɚ ɪɚɜɟɧ
ɱɢɫɥɭ ɛɚɣɬ, ɡɚɧɢɦɚɟɦɵɯ ɦɚɫɫɢɜɨɦ ɜ ɩɚɦɹɬɢ, ɪɚɡɦɟɪ ɫɬɪɨɤɨɜɨɝɨ ɥɢɬɟɪɚɥɚ -
ɷɬɨ «ɱɢɫɥɨ ɡɧɚɤɨɜ ɜ ɥɢɬɟɪɚɥɟ + 1», ɬɨ ɟɫɬɶ ɡɚɜɟɪɲɚɸɳɢɣ ɧɭɥɟɜɨɣ ɛɚɣɬ
ɭɱɢɬɵɜɚɟɬɫɹ ɩɪɢ ɨɩɪɟɞɟɥɟɧɢɢ ɞɥɢɧɵ ɥɢɬɟɪɚɥɚ. Ɂɧɚɱɟɧɢɟ, ɜɨɡɜɪɚɳɚɟɦɨɟ
sizeof, ɹɜɥɹɟɬɫɹ ɤɨɧɫɬɚɧɬɨɣ.
Ȼɢɧɚɪɧɵɟ ɚɪɢɮɦɟɬɢɱɟɫɤɢɟ ɨɩɟɪɚɰɢɢ ɭɦɧɨɠɟɧɢɹ (
«*»), ɞɟɥɟɧɢɹ («/»),
ɩɨɥɭɱɟɧɢɹ ɨɫɬɚɬɤɚ ɨɬ ɞɟɥɟɧɢɹ ɧɚɰɟɥɨ (
«%»), ɫɥɨɠɟɧɢɹ («+») ɢ ɜɵɱɢ-
ɬɚɧɢɹ (
«-») ɢɦɟɸɬ ɨɛɵɱɧɵɣ ɫɦɵɫɥ ɢ ɨɛɵɱɧɵɣ ɨɬɧɨɫɢɬɟɥɶɧɵɣ ɩɪɢɨɪɢɬɟɬ.
ȿɫɥɢ ɨɩɟɪɚɧɞɵ ɚɪɢɮɦɟɬɢɱɟɫɤɨɣ ɨɩɟɪɚɰɢɢ ɢɦɟɸɬ ɪɚɡɧɵɟ ɬɢɩɵ, ɩɪɟɞɜɚɪɢ-
ɬɟɥɶɧɨ ɜɵɩɨɥɧɹɸɬɫɹ ɫɬɚɧɞɚɪɬɧɵɟ ɚɪɢɮɦɟɬɢɱɟɫɤɢɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ, ɢ ɬɢɩ
ɪɟɡɭɥɶɬɚɬɚ ɨɩɟɪɚɰɢɢ ɨɩɪɟɞɟɥɹɟɬɫɹ ɨɛɳɢɦ ɬɢɩɨɦ ɨɩɟɪɚɧɞɨɜ ɩɨɫɥɟ ɫɬɚɧ-
ɞɚɪɬɧɵɯ ɩɪɟɨɛɪɚɡɨɜɚɧɢɣ. ɋɥɟɞɨɜɚɬɟɥɶɧɨ, ɜɵɪɚɠɟɧɢɟ
7/2 ɛɭɞɟɬ ɢɦɟɬɶ ɡɧɚ-
ɱɟɧɢɟ
3 ɬɢɩɚ int, ɬɚɤ ɤɚɤ ɨɛɚ ɨɩɟɪɚɧɞɚ ɢɦɟɸɬ ɬɢɩ int, ɚ ɜɵɪɚɠɟɧɢɟ
7.0/2 ɞɚɫɬ ɪɟɡɭɥɶɬɚɬ 3.5 ɬɢɩɚ double, ɩɨɫɤɨɥɶɤɭ ɷɬɨɬ ɬɢɩ ɢɦɟɟɬ ɩɟɪɜɵɣ
ɨɩɟɪɚɧɞ.
Ɉɩɟɪɚɰɢɢ ɨɬɧɨɲɟɧɢɹ ɞɜɭɯ ɜɵɪɚɠɟɧɢɣ (
«<», «<=», «>», «>=») ɬɪɟ-
ɛɭɸɬ ɨɩɟɪɚɧɞɨɜ ɚɪɢɮɦɟɬɢɱɟɫɤɨɝɨ ɬɢɩɚ ɢɥɢ ɠɟ ɨɛɚ ɨɩɟɪɚɧɞɚ ɞɨɥɠɧɵ ɛɵɬɶ
ɭɤɚɡɚɬɟɥɹɦɢ ɧɚ ɨɞɢɧɚɤɨɜɵɣ ɬɢɩ. ȼ ɫɥɭɱɚɟ ɨɩɟɪɚɧɞɨɜ ɚɪɢɮɦɟɬɢɱɟɫɤɨɝɨ ɬɢ-
ɩɚ ɜɵɱɢɫɥɹɸɬɫɹ ɡɧɚɱɟɧɢɹ ɨɩɟɪɚɧɞɨɜ, ɜɵɩɨɥɧɹɸɬɫɹ ɫɬɚɧɞɚɪɬɧɵɟ ɚɪɢɮɦɟ-
ɬɢɱɟɫɤɢɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɢ ɜɨɡɜɪɚɳɚɟɬɫɹ
1 ɬɢɩɚ int, ɟɫɥɢ ɨɬɧɨɲɟɧɢɟ ɜɵ-
ɩɨɥɧɹɟɬɫɹ (ɢɫɬɢɧɧɨ), ɢɥɢ
0, ɟɫɥɢ ɨɬɧɨɲɟɧɢɟ ɧɟ ɜɵɩɨɥɧɹɟɬɫɹ (ɥɨɠɧɨ). Ʉɨ-
ɝɞɚ ɫɪɚɜɧɢɜɚɸɬɫɹ ɞɜɚ ɭɤɚɡɚɬɟɥɹ, ɪɟɡɭɥɶɬɚɬ ɡɚɜɢɫɢɬ ɨɬ ɨɬɧɨɫɢɬɟɥɶɧɨɝɨ ɪɚɡ-
ɦɟɳɟɧɢɹ ɜ ɩɚɦɹɬɢ ɨɛɴɟɤɬɨɜ, ɧɚ ɤɨɬɨɪɵɟ ɫɫɵɥɚɸɬɫɹ ɭɤɚɡɚɬɟɥɢ.
Ɉɩɟɪɚɰɢɢ ɫɪɚɜɧɟɧɢɹ «==» (ɪɚɜɧɨ) ɢ «!=» (ɧɟ ɪɚɜɧɨ) ɜɵɩɨɥɧɹɸɬɫɹ ɚɧɚ-
ɥɨɝɢɱɧɵɦ ɨɛɪɚɡɨɦ, ɧɨ ɢɦɟɸɬ ɦɟɧɶɲɢɣ ɩɪɢɨɪɢɬɟɬ.
24
ȼɵɪɚɠɟɧɢɹ ɨɬɧɨɲɟɧɢɣ ɦɨɝɭɬ ɫɨɟɞɢɧɹɬɶɫɹ ɥɨɝɢɱɟɫɤɢɦɢ ɫɜɹɡɤɚɦɢ «&&»
(ɤɨɧɴɸɧɤɰɢɹ, ɥɨɝɢɱɟɫɤɨɟ ɭɦɧɨɠɟɧɢɟ) ɢ «
||» (ɞɢɡɴɸɧɤɰɢɹ, ɥɨɝɢɱɟɫɤɨɟ
ɫɥɨɠɟɧɢɟ). ȼ ɨɛɳɟɦ ɫɥɭɱɚɟ ɨɩɟɪɚɧɞɚɦɢ ɥɨɝɢɱɟɫɤɢɯ ɫɜɹɡɨɤ ɦɨɝɭɬ ɛɵɬɶ ɥɸ-
ɛɵɟ ɫɤɚɥɹɪɧɵɟ ɡɧɚɱɟɧɢɹ ɢ ɨɩɟɪɚɰɢɹ «
&&» ɞɚɟɬ ɪɟɡɭɥɶɬɚɬ, ɪɚɜɧɵɣ 1 ɬɢɩɚ
    Ⱦɚɧɧɵɟ, ɪɚɡɦɟɳɟɧɧɵɟ ɜ ɞɢɧɚɦɢɱɟɫɤɨɣ ɩɚɦɹɬɢ ɨɩɟɪɚɰɢɟɣ new, ɭɞɚɥɹ-
ɸɬɫɹ ɢɡ ɩɚɦɹɬɢ ɨɩɟɪɚɰɢɟɣ delete ɫ ɨɩɟɪɚɧɞɨɦ-ɭɤɚɡɚɬɟɥɟɦ, ɡɧɚɱɟɧɢɟ ɤɨɬɨ-
ɪɨɝɨ ɩɨɥɭɱɟɧɨ ɨɩɟɪɚɰɢɟɣ new, ɧɚɩɪɢɦɟɪ,
     delete intArray;
     delete ip2;
    Ɉɩɟɪɚɰɢɹ delete ɬɨɥɶɤɨ ɨɫɜɨɛɨɠɞɚɟɬ ɞɢɧɚɦɢɱɟɫɤɭɸ ɩɚɦɹɬɶ, ɧɨ ɧɟ
ɢɡɦɟɧɹɟɬ ɡɧɚɱɟɧɢɟ ɭɤɚɡɚɬɟɥɹ-ɨɩɟɪɚɧɞɚ. ɉɪɨɝɪɚɦɦɢɫɬ ɞɨɥɠɟɧ ɩɨɦɧɢɬɶ, ɱɬɨ
ɩɨɫɥɟ ɨɫɜɨɛɨɠɞɟɧɢɹ ɩɚɦɹɬɢ ɢɫɩɨɥɶɡɨɜɚɬɶ ɷɬɨɬ ɭɤɚɡɚɬɟɥɶ ɞɥɹ ɨɛɪɚɳɟɧɢɹ ɤ
ɞɚɧɧɵɦ ɧɟɥɶɡɹ.
     Ɋɚɡɦɟɪ ɞɚɧɧɨɝɨ ɢɥɢ ɬɢɩɚ ɞɚɧɧɨɝɨ ɜ ɛɚɣɬɚɯ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɩɨ ɨɩɟɪɚ-
ɰɢɢ sizeof. Ɉɩɟɪɚɧɞ ɦɨɠɟɬ ɛɵɬɶ ɥɸɛɨɝɨ ɬɢɩɚ, ɤɪɨɦɟ ɬɢɩɚ ɮɭɧɤɰɢɢ ɢ ɛɢ-
ɬɨɜɨɝɨ ɩɨɥɹ. ȿɫɥɢ ɨɩɟɪɚɧɞɨɦ ɹɜɥɹɟɬɫɹ ɢɦɹ ɬɢɩɚ, ɨɧɨ ɞɨɥɠɧɨ ɡɚɤɥɸɱɚɬɶɫɹ ɜ
ɫɤɨɛɤɢ. ȼɨɡɜɪɚɳɚɟɦɨɟ ɡɧɚɱɟɧɢɟ ɢɦɟɟɬ ɩɪɟɞɨɩɪɟɞɟɥɟɧɧɵɣ ɬɢɩ size_t -
ɰɟɥɵɣ ɬɢɩ, ɪɚɡɦɟɪ ɤɨɬɨɪɨɝɨ ɨɩɪɟɞɟɥɹɟɬɫɹ ɪɟɚɥɢɡɚɰɢɟɣ ɤɨɦɩɢɥɹɬɨɪɚ.
Ɉɛɵɱɧɨ ɬɢɩɭ size_t ɫɨɨɬɜɟɬɫɬɜɭɟɬ unsigned int. Ɋɚɡɦɟɪ ɦɚɫɫɢɜɚ ɪɚɜɟɧ
ɱɢɫɥɭ ɛɚɣɬ, ɡɚɧɢɦɚɟɦɵɯ ɦɚɫɫɢɜɨɦ ɜ ɩɚɦɹɬɢ, ɪɚɡɦɟɪ ɫɬɪɨɤɨɜɨɝɨ ɥɢɬɟɪɚɥɚ -
ɷɬɨ «ɱɢɫɥɨ ɡɧɚɤɨɜ ɜ ɥɢɬɟɪɚɥɟ + 1», ɬɨ ɟɫɬɶ ɡɚɜɟɪɲɚɸɳɢɣ ɧɭɥɟɜɨɣ ɛɚɣɬ
ɭɱɢɬɵɜɚɟɬɫɹ ɩɪɢ ɨɩɪɟɞɟɥɟɧɢɢ ɞɥɢɧɵ ɥɢɬɟɪɚɥɚ. Ɂɧɚɱɟɧɢɟ, ɜɨɡɜɪɚɳɚɟɦɨɟ
sizeof, ɹɜɥɹɟɬɫɹ ɤɨɧɫɬɚɧɬɨɣ.
     Ȼɢɧɚɪɧɵɟ ɚɪɢɮɦɟɬɢɱɟɫɤɢɟ ɨɩɟɪɚɰɢɢ ɭɦɧɨɠɟɧɢɹ («*»), ɞɟɥɟɧɢɹ («/»),
ɩɨɥɭɱɟɧɢɹ ɨɫɬɚɬɤɚ ɨɬ ɞɟɥɟɧɢɹ ɧɚɰɟɥɨ («%»), ɫɥɨɠɟɧɢɹ («+») ɢ ɜɵɱɢ-
ɬɚɧɢɹ («-») ɢɦɟɸɬ ɨɛɵɱɧɵɣ ɫɦɵɫɥ ɢ ɨɛɵɱɧɵɣ ɨɬɧɨɫɢɬɟɥɶɧɵɣ ɩɪɢɨɪɢɬɟɬ.
ȿɫɥɢ ɨɩɟɪɚɧɞɵ ɚɪɢɮɦɟɬɢɱɟɫɤɨɣ ɨɩɟɪɚɰɢɢ ɢɦɟɸɬ ɪɚɡɧɵɟ ɬɢɩɵ, ɩɪɟɞɜɚɪɢ-
ɬɟɥɶɧɨ ɜɵɩɨɥɧɹɸɬɫɹ ɫɬɚɧɞɚɪɬɧɵɟ ɚɪɢɮɦɟɬɢɱɟɫɤɢɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ, ɢ ɬɢɩ
ɪɟɡɭɥɶɬɚɬɚ ɨɩɟɪɚɰɢɢ ɨɩɪɟɞɟɥɹɟɬɫɹ ɨɛɳɢɦ ɬɢɩɨɦ ɨɩɟɪɚɧɞɨɜ ɩɨɫɥɟ ɫɬɚɧ-
ɞɚɪɬɧɵɯ ɩɪɟɨɛɪɚɡɨɜɚɧɢɣ. ɋɥɟɞɨɜɚɬɟɥɶɧɨ, ɜɵɪɚɠɟɧɢɟ 7/2 ɛɭɞɟɬ ɢɦɟɬɶ ɡɧɚ-
ɱɟɧɢɟ 3 ɬɢɩɚ int, ɬɚɤ ɤɚɤ ɨɛɚ ɨɩɟɪɚɧɞɚ ɢɦɟɸɬ ɬɢɩ int, ɚ ɜɵɪɚɠɟɧɢɟ
7.0/2 ɞɚɫɬ ɪɟɡɭɥɶɬɚɬ 3.5 ɬɢɩɚ double, ɩɨɫɤɨɥɶɤɭ ɷɬɨɬ ɬɢɩ ɢɦɟɟɬ ɩɟɪɜɵɣ
ɨɩɟɪɚɧɞ.
     Ɉɩɟɪɚɰɢɢ ɨɬɧɨɲɟɧɢɹ ɞɜɭɯ ɜɵɪɚɠɟɧɢɣ («<», «<=», «>», «>=») ɬɪɟ-
ɛɭɸɬ ɨɩɟɪɚɧɞɨɜ ɚɪɢɮɦɟɬɢɱɟɫɤɨɝɨ ɬɢɩɚ ɢɥɢ ɠɟ ɨɛɚ ɨɩɟɪɚɧɞɚ ɞɨɥɠɧɵ ɛɵɬɶ
ɭɤɚɡɚɬɟɥɹɦɢ ɧɚ ɨɞɢɧɚɤɨɜɵɣ ɬɢɩ. ȼ ɫɥɭɱɚɟ ɨɩɟɪɚɧɞɨɜ ɚɪɢɮɦɟɬɢɱɟɫɤɨɝɨ ɬɢ-
ɩɚ ɜɵɱɢɫɥɹɸɬɫɹ ɡɧɚɱɟɧɢɹ ɨɩɟɪɚɧɞɨɜ, ɜɵɩɨɥɧɹɸɬɫɹ ɫɬɚɧɞɚɪɬɧɵɟ ɚɪɢɮɦɟ-
ɬɢɱɟɫɤɢɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɢ ɜɨɡɜɪɚɳɚɟɬɫɹ 1 ɬɢɩɚ int, ɟɫɥɢ ɨɬɧɨɲɟɧɢɟ ɜɵ-
ɩɨɥɧɹɟɬɫɹ (ɢɫɬɢɧɧɨ), ɢɥɢ 0, ɟɫɥɢ ɨɬɧɨɲɟɧɢɟ ɧɟ ɜɵɩɨɥɧɹɟɬɫɹ (ɥɨɠɧɨ). Ʉɨ-
ɝɞɚ ɫɪɚɜɧɢɜɚɸɬɫɹ ɞɜɚ ɭɤɚɡɚɬɟɥɹ, ɪɟɡɭɥɶɬɚɬ ɡɚɜɢɫɢɬ ɨɬ ɨɬɧɨɫɢɬɟɥɶɧɨɝɨ ɪɚɡ-
ɦɟɳɟɧɢɹ ɜ ɩɚɦɹɬɢ ɨɛɴɟɤɬɨɜ, ɧɚ ɤɨɬɨɪɵɟ ɫɫɵɥɚɸɬɫɹ ɭɤɚɡɚɬɟɥɢ.
     Ɉɩɟɪɚɰɢɢ ɫɪɚɜɧɟɧɢɹ «==» (ɪɚɜɧɨ) ɢ «!=» (ɧɟ ɪɚɜɧɨ) ɜɵɩɨɥɧɹɸɬɫɹ ɚɧɚ-
ɥɨɝɢɱɧɵɦ ɨɛɪɚɡɨɦ, ɧɨ ɢɦɟɸɬ ɦɟɧɶɲɢɣ ɩɪɢɨɪɢɬɟɬ.
     ȼɵɪɚɠɟɧɢɹ ɨɬɧɨɲɟɧɢɣ ɦɨɝɭɬ ɫɨɟɞɢɧɹɬɶɫɹ ɥɨɝɢɱɟɫɤɢɦɢ ɫɜɹɡɤɚɦɢ «&&»
(ɤɨɧɴɸɧɤɰɢɹ, ɥɨɝɢɱɟɫɤɨɟ ɭɦɧɨɠɟɧɢɟ) ɢ «||» (ɞɢɡɴɸɧɤɰɢɹ, ɥɨɝɢɱɟɫɤɨɟ
ɫɥɨɠɟɧɢɟ). ȼ ɨɛɳɟɦ ɫɥɭɱɚɟ ɨɩɟɪɚɧɞɚɦɢ ɥɨɝɢɱɟɫɤɢɯ ɫɜɹɡɨɤ ɦɨɝɭɬ ɛɵɬɶ ɥɸ-
ɛɵɟ ɫɤɚɥɹɪɧɵɟ ɡɧɚɱɟɧɢɹ ɢ ɨɩɟɪɚɰɢɹ «&&» ɞɚɟɬ ɪɟɡɭɥɶɬɚɬ, ɪɚɜɧɵɣ 1 ɬɢɩɚ
                                   24