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

UptoLike

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

ȼ ɷɬɨɦ ɩɪɢɦɟɪɟ ɭɤɚɡɚɬɟɥɶ pint ɢɧɢɰɢɚɥɢɡɢɪɨɜɚɧ ɚɞɪɟɫɨɦ ɩɹɬɨɝɨ ɷɥɟ-
ɦɟɧɬɚ ɦɚɫɫɢɜɚ
Array, ɚ ɡɚɬɟɦ ɫɟɞɶɦɨɦɭ ɷɥɟɦɟɧɬɭ ɷɬɨɝɨ ɦɚɫɫɢɜɚ ɩɪɢɫɜɨɟɧɨ
ɡɧɚɱɟɧɢɟ
1.
ȼ ɤɚɱɟɫɬɜɟ ɢɧɞɟɤɫɚ ɦɨɠɟɬ ɡɚɞɚɜɚɬɶɫɹ ɥɸɛɨɟ ɜɵɪɚɠɟɧɢɟ ɫɨ ɡɧɚɱɟɧɢɟɦ
ɰɟɥɨɝɨ ɬɢɩɚ.
ɉɨɫɤɨɥɶɤɭ ɋɢ++ ɹɜɥɹɟɬɫɹ ɬɢɩɢɡɢɪɨɜɚɧɧɵɦ ɹɡɵɤɨɦ, ɜ ɧɟɦ ɨɩɪɟɞɟɥɟɧɵ
ɹɜɧɵɟ ɢ ɧɟɹɜɧɵɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɬɢɩɨɜ ɞɚɧɧɵɯ. ɇɟɹɜɧɵɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ
ɜɵɩɨɥɧɹɸɬɫɹ ɩɪɢ ɛɢɧɚɪɧɵɯ ɚɪɢɮɦɟɬɢɱɟɫɤɢɯ ɨɩɟɪɚɰɢɹɯ ɢ ɨɩɟɪɚɰɢɹɯ ɩɪɢ-
ɫɜɚɢɜɚɧɢɹ. ɗɬɢ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ, ɧɚɡɵɜɚɟɦɵɟ ɫɬɚɧɞɚɪɬɧɵɦɢ ɚɪɢɮɦɟɬɢɱɟ-
ɫɤɢɦɢ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹɦɢ, ɜɵɩɨɥɧɹɸɬɫɹ ɜ ɫɥɟɞɭɸɳɟɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ:
x ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ
long double, ɞɪɭɝɨɣ ɨɩɟɪɚɧɞ ɩɪɟɨɛ-
ɪɚɡɭɟɬɫɹ ɜ ɬɢɩ
long double;
x ɢɧɚɱɟ, ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ double, ɞɪɭɝɨɣ ɨɩɟɪɚɧɞ ɩɪɟɨɛ-
ɪɚɡɭɟɬɫɹ ɜ ɬɢɩ
double;
x ɢɧɚɱɟ, ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ float, ɞɪɭɝɨɣ ɨɩɟɪɚɧɞ ɩɪɟɨɛ-
ɪɚɡɭɟɬɫɹ ɜ ɬɢɩ
float;
x ɢɧɚɱɟ, ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ unsigned long int, ɞɪɭɝɨɣ
ɨɩɟɪɚɧɞ ɩɪɟɨɛɪɚɡɭɟɬɫɹ ɜ ɬɢɩ
unsigned long int;
x ɢɧɚɱɟ, ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ long int, ɞɪɭɝɨɣ ɨɩɟɪɚɧɞ ɩɪɟ-
ɨɛɪɚɡɭɟɬɫɹ ɜ ɬɢɩ
long int;
x ɢɧɚɱɟ ɜɵɩɨɥɧɹɸɬɫɹ ɫɬɚɧɞɚɪɬɧɵɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɞɥɹ ɰɟɥɵɯ, ɩɪɢ
ɷɬɨɦ ɬɢɩɵ
char, short int ɢ ɛɢɬɨɜɵɟ ɩɨɥɹ ɬɢɩɚ int ɩɪɟɨɛɪɚɡɭɸɬ-
ɫɹ ɜ ɬɢɩ
int;
x ɜ ɨɫɬɚɥɶɧɵɯ ɫɥɭɱɚɹɯ ɨɩɟɪɚɧɞɵ ɢɦɟɸɬ ɬɢɩ
int.
əɜɧɨɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɟ ɬɢɩɨɜ ɦɨɠɟɬ ɛɵɬɶ ɡɚɞɚɧɨ ɜ ɞɜɭɯ ɮɨɪɦɚɯ. ɉɟɪ-
ɜɚɹ ɮɨɪɦɚ ɫɨɜɦɟɫɬɢɦɚ ɫ ɋɢ, ɜ ɧɟɣ ɡɚ ɢɦɟɧɟɦ ɬɢɩɚ ɜ ɤɪɭɝɥɵɯ ɫɤɨɛɤɚɯ ɡɚɩɢ-
ɫɵɜɚɟɬɫɹ ɩɪɟɨɛɪɚɡɭɟɦɨɟ ɡɧɚɱɟɧɢɟ, ɤɨɬɨɪɨɟ ɦɨɠɟɬ ɛɵɬɶ ɩɟɪɜɢɱɧɵɦ ɜɵɪɚ-
ɠɟɧɢɟɦ ɢɥɢ ɜɵɪɚɠɟɧɢɟɦ ɫ ɭɧɚɪɧɨɣ ɨɩɟɪɚɰɢɟɣ. ɂɦɹ ɬɢɩɚ ɜ ɷɬɨɦ ɫɥɭɱɚɟ
ɦɨɠɟɬ ɛɵɬɶ ɩɪɟɞɫɬɚɜɥɟɧɨ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶɸ ɨɩɢɫɚɬɟɥɟɣ, ɧɚɩɪɢɦɟɪ,
(long int*)pp
ɢ ɨɩɪɟɞɟɥɹɟɬ ɩɪɟɨɛɪɚɡɨɜɚɧɢɟ ɧɟɤɨɬɨɪɨɝɨ ɞɚɧɧɨɝɨ
pp ɜ ɬɢɩ ɭɤɚɡɚɬɟɥɹ ɧɚ
long int. ȼɬɨɪɚɹ ɮɨɪɦɚ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɬɢɩɚ ɡɚɩɢɫɵɜɚɟɬɫɹ ɤɚɤ ɜɵɡɨɜ
ɮɭɧɤɰɢɢ, ɩɪɢ ɷɬɨɦ ɢɦɹ ɬɢɩɚ ɞɨɥɠɧɨ ɡɚɞɚɜɚɬɶɫɹ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɦ, ɧɚɩɪɢ-
ɦɟɪ,
int(x);
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɪɟɡɭɥɶɬɚɬ ɹɜɧɨɝɨ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɧɟ ɹɜɥɹɟɬɫɹ
L-ɡɧɚɱɟɧɢɟɦ.
Ɉɩɟɪɚɰɢɢ ɚɜɬɨɭɜɟɥɢɱɟɧɢɹ ɢ ɚɜɬɨɭɦɟɧɶɲɟɧɢɹ («++» ɢ «--») ɦɨɝɭɬ
ɛɵɬɶ ɩɪɟɮɢɤɫɧɵɦɢ ɢ ɩɨɫɬɮɢɤɫɧɵɦɢ ɢ ɜɵɡɵɜɚɸɬ ɭɜɟɥɢɱɟɧɢɟ (ɭɦɟɧɶɲɟ-
ɧɢɟ) ɫɜɨɟɝɨ ɨɩɟɪɚɧɞɚ ɧɚ ɟɞɢɧɢɰɭ, ɬɨ ɟɫɬɶ ɜɵɪɚɠɟɧɢɟ
++x ɷɤɜɢɜɚɥɟɧɬɧɨ
x=x+1, ɚ --x ɷɤɜɢɜɚɥɟɧɬɧɨ x=x-1. ɉɪɟɮɢɤɫɧɚɹ ɨɩɟɪɚɰɢɹ ɜɵɩɨɥɧɹɟɬɫɹ ɞɨ
ɬɨɝɨ, ɤɚɤ ɟɟ ɨɩɟɪɚɧɞ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɧ ɜ ɜɵɱɢɫɥɟɧɢɢ ɜɵɪɚɠɟɧɢɹ, ɚ ɩɨɫɬ-
22
    ȼ ɷɬɨɦ ɩɪɢɦɟɪɟ ɭɤɚɡɚɬɟɥɶ pint ɢɧɢɰɢɚɥɢɡɢɪɨɜɚɧ ɚɞɪɟɫɨɦ ɩɹɬɨɝɨ ɷɥɟ-
ɦɟɧɬɚ ɦɚɫɫɢɜɚ Array, ɚ ɡɚɬɟɦ ɫɟɞɶɦɨɦɭ ɷɥɟɦɟɧɬɭ ɷɬɨɝɨ ɦɚɫɫɢɜɚ ɩɪɢɫɜɨɟɧɨ
ɡɧɚɱɟɧɢɟ 1.
    ȼ ɤɚɱɟɫɬɜɟ ɢɧɞɟɤɫɚ ɦɨɠɟɬ ɡɚɞɚɜɚɬɶɫɹ ɥɸɛɨɟ ɜɵɪɚɠɟɧɢɟ ɫɨ ɡɧɚɱɟɧɢɟɦ
ɰɟɥɨɝɨ ɬɢɩɚ.
    ɉɨɫɤɨɥɶɤɭ ɋɢ++ ɹɜɥɹɟɬɫɹ ɬɢɩɢɡɢɪɨɜɚɧɧɵɦ ɹɡɵɤɨɦ, ɜ ɧɟɦ ɨɩɪɟɞɟɥɟɧɵ
ɹɜɧɵɟ ɢ ɧɟɹɜɧɵɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɬɢɩɨɜ ɞɚɧɧɵɯ. ɇɟɹɜɧɵɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ
ɜɵɩɨɥɧɹɸɬɫɹ ɩɪɢ ɛɢɧɚɪɧɵɯ ɚɪɢɮɦɟɬɢɱɟɫɤɢɯ ɨɩɟɪɚɰɢɹɯ ɢ ɨɩɟɪɚɰɢɹɯ ɩɪɢ-
ɫɜɚɢɜɚɧɢɹ. ɗɬɢ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ, ɧɚɡɵɜɚɟɦɵɟ ɫɬɚɧɞɚɪɬɧɵɦɢ ɚɪɢɮɦɟɬɢɱɟ-
ɫɤɢɦɢ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹɦɢ, ɜɵɩɨɥɧɹɸɬɫɹ ɜ ɫɥɟɞɭɸɳɟɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ:
   x ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ long double, ɞɪɭɝɨɣ ɨɩɟɪɚɧɞ ɩɪɟɨɛ-
      ɪɚɡɭɟɬɫɹ ɜ ɬɢɩ long double;
   x ɢɧɚɱɟ, ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ double, ɞɪɭɝɨɣ ɨɩɟɪɚɧɞ ɩɪɟɨɛ-
      ɪɚɡɭɟɬɫɹ ɜ ɬɢɩ double;
   x ɢɧɚɱɟ, ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ float, ɞɪɭɝɨɣ ɨɩɟɪɚɧɞ ɩɪɟɨɛ-
      ɪɚɡɭɟɬɫɹ ɜ ɬɢɩ float;
   x ɢɧɚɱɟ, ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ unsigned long int, ɞɪɭɝɨɣ
      ɨɩɟɪɚɧɞ ɩɪɟɨɛɪɚɡɭɟɬɫɹ ɜ ɬɢɩ unsigned long int;
   x ɢɧɚɱɟ, ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ long int, ɞɪɭɝɨɣ ɨɩɟɪɚɧɞ ɩɪɟ-
      ɨɛɪɚɡɭɟɬɫɹ ɜ ɬɢɩ long int;
   x ɢɧɚɱɟ ɜɵɩɨɥɧɹɸɬɫɹ ɫɬɚɧɞɚɪɬɧɵɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɞɥɹ ɰɟɥɵɯ, ɩɪɢ
      ɷɬɨɦ ɬɢɩɵ char, short int ɢ ɛɢɬɨɜɵɟ ɩɨɥɹ ɬɢɩɚ int ɩɪɟɨɛɪɚɡɭɸɬ-
      ɫɹ ɜ ɬɢɩ int;
   x ɜ ɨɫɬɚɥɶɧɵɯ ɫɥɭɱɚɹɯ ɨɩɟɪɚɧɞɵ ɢɦɟɸɬ ɬɢɩ int.
    əɜɧɨɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɟ ɬɢɩɨɜ ɦɨɠɟɬ ɛɵɬɶ ɡɚɞɚɧɨ ɜ ɞɜɭɯ ɮɨɪɦɚɯ. ɉɟɪ-
ɜɚɹ ɮɨɪɦɚ ɫɨɜɦɟɫɬɢɦɚ ɫ ɋɢ, ɜ ɧɟɣ ɡɚ ɢɦɟɧɟɦ ɬɢɩɚ ɜ ɤɪɭɝɥɵɯ ɫɤɨɛɤɚɯ ɡɚɩɢ-
ɫɵɜɚɟɬɫɹ ɩɪɟɨɛɪɚɡɭɟɦɨɟ ɡɧɚɱɟɧɢɟ, ɤɨɬɨɪɨɟ ɦɨɠɟɬ ɛɵɬɶ ɩɟɪɜɢɱɧɵɦ ɜɵɪɚ-
ɠɟɧɢɟɦ ɢɥɢ ɜɵɪɚɠɟɧɢɟɦ ɫ ɭɧɚɪɧɨɣ ɨɩɟɪɚɰɢɟɣ. ɂɦɹ ɬɢɩɚ ɜ ɷɬɨɦ ɫɥɭɱɚɟ
ɦɨɠɟɬ ɛɵɬɶ ɩɪɟɞɫɬɚɜɥɟɧɨ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶɸ ɨɩɢɫɚɬɟɥɟɣ, ɧɚɩɪɢɦɟɪ,
     (long int*)pp
ɢ ɨɩɪɟɞɟɥɹɟɬ ɩɪɟɨɛɪɚɡɨɜɚɧɢɟ ɧɟɤɨɬɨɪɨɝɨ ɞɚɧɧɨɝɨ pp ɜ ɬɢɩ ɭɤɚɡɚɬɟɥɹ ɧɚ
long int. ȼɬɨɪɚɹ ɮɨɪɦɚ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɬɢɩɚ ɡɚɩɢɫɵɜɚɟɬɫɹ ɤɚɤ ɜɵɡɨɜ
ɮɭɧɤɰɢɢ, ɩɪɢ ɷɬɨɦ ɢɦɹ ɬɢɩɚ ɞɨɥɠɧɨ ɡɚɞɚɜɚɬɶɫɹ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɦ, ɧɚɩɪɢ-
ɦɟɪ,
     int(x);
    ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɪɟɡɭɥɶɬɚɬ ɹɜɧɨɝɨ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɧɟ ɹɜɥɹɟɬɫɹ
L-ɡɧɚɱɟɧɢɟɦ.
    Ɉɩɟɪɚɰɢɢ ɚɜɬɨɭɜɟɥɢɱɟɧɢɹ ɢ ɚɜɬɨɭɦɟɧɶɲɟɧɢɹ («++» ɢ «--») ɦɨɝɭɬ
ɛɵɬɶ ɩɪɟɮɢɤɫɧɵɦɢ ɢ ɩɨɫɬɮɢɤɫɧɵɦɢ ɢ ɜɵɡɵɜɚɸɬ ɭɜɟɥɢɱɟɧɢɟ (ɭɦɟɧɶɲɟ-
ɧɢɟ) ɫɜɨɟɝɨ ɨɩɟɪɚɧɞɚ ɧɚ ɟɞɢɧɢɰɭ, ɬɨ ɟɫɬɶ ɜɵɪɚɠɟɧɢɟ ++x ɷɤɜɢɜɚɥɟɧɬɧɨ
x=x+1, ɚ --x ɷɤɜɢɜɚɥɟɧɬɧɨ x=x-1. ɉɪɟɮɢɤɫɧɚɹ ɨɩɟɪɚɰɢɹ ɜɵɩɨɥɧɹɟɬɫɹ ɞɨ
ɬɨɝɨ, ɤɚɤ ɟɟ ɨɩɟɪɚɧɞ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɧ ɜ ɜɵɱɢɫɥɟɧɢɢ ɜɵɪɚɠɟɧɢɹ, ɚ ɩɨɫɬ-

                                  22