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

UptoLike

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

ɉɨɥɧɨɟ ɨɩɢɫɚɧɢɟ ɮɭɧɤɰɢɢ ɫɨɞɟɪɠɢɬ ɬɚɤɠɟ ɬɟɥɨ ɮɭɧɤɰɢɢ, ɩɪɟɞɫɬɚɜ-
ɥɹɸɳɟɟ ɫɨɛɨɣ ɫɨɫɬɚɜɧɨɣ ɨɩɟɪɚɬɨɪ. ɋɨɫɬɚɜɧɨɣ ɨɩɟɪɚɬɨɪ - ɷɬɨ ɩɨɫɥɟɞɨɜɚ-
ɬɟɥɶɧɨɫɬɶ ɨɩɢɫɚɧɢɣ ɜɧɭɬɪɟɧɧɢɯ ɞɚɧɧɵɯ ɢ ɨɩɟɪɚɬɨɪɨɜ, ɡɚɤɥɸɱɟɧɧɚɹ ɜ ɮɢ-
ɝɭɪɧɵɟ ɫɤɨɛɤɢ. ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɜ ɋɢ/ɋɢ++ ɨɩɢɫɚɧɢɟ ɮɭɧɤɰɢɣ ɧɟ
ɦɨɠɟɬ ɛɵɬɶ ɜɥɨɠɟɧɧɵɦ, ɜ ɬɟɥɟ ɮɭɧɤɰɢɢ ɧɟɥɶɡɹ ɨɛɴɹɜɢɬɶ ɞɪɭɝɭɸ ɮɭɧɤ-
ɰɢɸ.
Ɏɭɧɤɰɢɹ, ɜɨɡɜɪɚɳɚɸɳɚɹ ɫɪɟɞɧɟɟ ɚɪɢɮɦɟɬɢɱɟɫɤɨɟ ɬɪɟɯ ɜɟɳɟɫɬɜɟɧɧɵɯ
ɞɚɧɧɵɯ, ɦɨɠɟɬ ɛɵɬɶ ɨɩɢɫɚɧɚ ɬɚɤ:
double sred(double x, double y, double z)
{
double s;
s=x+y+z;
return s/3;
}
Ⱦɥɹ ɜɵɡɨɜɚ ɬɚɤɨɣ ɮɭɧɤɰɢɢ ɩɪɢ ɭɫɥɨɜɢɢ, ɱɬɨ ɩɪɟɞɜɚɪɢɬɟɥɶɧɨ ɨɛɴɹɜɥɟ-
ɧɵ ɩɟɪɟɦɟɧɧɵɟ
p, a, b, ɢ c, ɦɨɠɧɨ ɡɚɩɢɫɚɬɶ ɨɩɟɪɚɬɨɪ:
p=sred(a, b, c);
Ɉɱɟɜɢɞɧɨ, ɱɬɨ ɮɭɧɤɰɢɹ ɞɨɥɠɧɚ ɛɵɬɶ ɨɩɢɫɚɧɚ ɞɨ ɬɨɝɨ, ɤɚɤ ɜɫɬɪɟɬɢɬɫɹ
ɟɟ ɩɟɪɜɵɣ ɜɵɡɨɜ. ɗɬɨ ɧɟ ɜɫɟɝɞɚ ɜɨɡɦɨɠɧɨ, ɧɚɩɪɢɦɟɪ, ɤɨɝɞɚ ɞɜɟ ɮɭɧɤɰɢɢ
ɜɵɡɵɜɚɸɬ ɞɪɭɝ ɞɪɭɝɚ. Ʉɨɝɞɚ ɩɪɨɝɪɚɦɦɚ ɫɨɫɬɨɢɬ ɢɡ ɧɟɫɤɨɥɶɤɢɯ ɮɚɣɥɨɜ,
ɩɨɥɧɨɟ ɨɩɢɫɚɧɢɟ ɮɭɧɤɰɢɢ ɞɨɥɠɧɨ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɬɨɥɶɤɨ ɜ ɨɞɧɨɦ ɮɚɣɥɟ,
ɧɨ ɜɵɡɨɜɵ ɮɭɧɤɰɢɢ ɜɨɡɦɨɠɧɵ ɢɡ ɪɚɡɧɵɯ ɮɚɣɥɨɜ ɩɪɨɝɪɚɦɦɵ. ɑɬɨɛɵ ɪɚɡ-
ɪɟɲɢɬɶ ɩɨɞɨɛɧɵɟ ɩɪɨɬɢɜɨɪɟɱɢɹ, ɩɪɟɞɭɫɦɨɬɪɟɧɵ ɞɜɟ ɮɨɪɦɵ ɨɩɢɫɚɧɢɹ
ɮɭɧɤɰɢɣ: ɩɨɥɧɚɹ ɮɨɪɦɚ, ɧɚɡɵɜɚɟɦɚɹ ɬɚɤɠɟ ɨɩɪɟɞɟɥɟɧɢɟɦ ɮɭɧɤɰɢɢ, ɢɫɨ-
ɤɪɚɳɟɧɧɚɹ, ɧɚɡɵɜɚɟɦɚɹ ɨɩɢɫɚɧɢɟɦ ɩɪɨɬɨɬɢɩɚ ɮɭɧɤɰɢɢ, ɢɥɢ ɩɪɨɫɬɨ ɩɪɨɬɨ-
ɬɢɩɨɦ. ɉɪɨɬɨɬɢɩ ɮɭɧɤɰɢɢ ɫɨɞɟɪɠɢɬ ɬɨɥɶɤɨ ɡɚɝɨɥɨɜɨɤ ɮɭɧɤɰɢɢ ɢ ɡɚɞɚɟɬ
ɢɦɹ ɮɭɧɤɰɢɢ, ɬɢɩ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɡɧɚɱɟɧɢɹ ɢ ɬɢɩɵ ɩɚɪɚɦɟɬɪɨɜ. ɉɨ ɷɬɨɣ
ɢɧɮɨɪɦɚɰɢɢ ɩɪɢ ɤɨɦɩɢɥɹɰɢɢ ɩɪɨɝɪɚɦɦɧɨɝɨ ɮɚɣɥɚ ɦɨɠɧɨ ɩɪɨɜɟɪɢɬɶ ɩɪɚ-
ɜɢɥɶɧɨɫɬɶ ɡɚɩɢɫɢ ɜɵɡɨɜɚ ɮɭɧɤɰɢɢ ɢ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɡɧɚɱɟ-
ɧɢɹ. ȼɋɢ++ ɬɪɟɛɭɟɬɫɹ, ɱɬɨɛɵ ɜɵɡɨɜɭ ɥɸɛɨɣ ɮɭɧɤɰɢɢ ɩɪɟɞɲɟɫɬɜɨɜɚɥɨ ɜ
ɬɨɦ ɠɟ ɮɚɣɥɟ ɥɢɛɨ ɩɨɥɧɨɟ ɨɩɪɟɞɟɥɟɧɢɟ ɮɭɧɤɰɢɢ, ɥɢɛɨ ɨɩɢɫɚɧɢɟ ɟɟ ɩɪɨɬɨ-
ɬɢɩɚ.
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ ɪɹɞ ɞɨɩɨɥɧɢɬɟɥɶɧɵɯ ɜɨɡɦɨɠɧɨɫɬɟɣ ɨɩɢɫɚɧɢɹ ɮɭɧɤ-
ɰɢɣ ɜ ɋɢ++:
1. ɉɪɢ ɨɩɢɫɚɧɢɢ ɩɪɨɬɨɬɢɩɚ ɮɭɧɤɰɢɢ ɦɨɠɧɨ ɧɟ ɭɤɚɡɵɜɚɬɶ ɢɦɟɧɚ ɩɚɪɚɦɟɬ-
ɪɨɜ, ɞɨɫɬɚɬɨɱɧɨ ɭɤɚɡɚɬɶ ɢɯ ɬɢɩɵ.
2. Ⱦɥɹ ɱɚɫɬɢ ɩɚɪɚɦɟɬɪɨɜ ɮɭɧɤɰɢɢ ɦɨɠɧɨ ɡɚɞɚɜɚɬɶ ɡɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ,
ɱɬɨ ɩɨɡɜɨɥɹɟɬ ɜɵɡɵɜɚɬɶ ɮɭɧɤɰɢɸ ɫ ɦɟɧɶɲɢɦ ɱɢɫɥɨɦ ɚɪɝɭɦɟɧɬɨɜ, ɱɟɦ ɩɪɟ-
ɞɭɫɦɨɬɪɟɧɨ ɨɩɢɫɚɧɢɟɦ ɮɭɧɤɰɢɢ. Ɂɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ ɦɨɠɧɨ ɭɤɚɡɵ-
ɜɚɬɶ ɬɨɥɶɤɨ ɞɥɹ ɩɨɫɥɟɞɧɢɯ ɩɚɪɚɦɟɬɪɨɜ ɜ ɫɩɢɫɤɟ. ɇɚɩɪɢɦɟɪ, ɮɭɧɤɰɢɹ
sred
ɦɨɝɥɚ ɛɵ ɛɵɬɶ ɨɩɢɫɚɧɚ ɬɚɤ:
double sred(double x, double y, double z=0)
{
double s;
30
s = x+y+z;
    ɉɨɥɧɨɟ ɨɩɢɫɚɧɢɟ ɮɭɧɤɰɢɢ ɫɨɞɟɪɠɢɬ ɬɚɤɠɟ ɬɟɥɨ ɮɭɧɤɰɢɢ, ɩɪɟɞɫɬɚɜ-
ɥɹɸɳɟɟ ɫɨɛɨɣ ɫɨɫɬɚɜɧɨɣ ɨɩɟɪɚɬɨɪ. ɋɨɫɬɚɜɧɨɣ ɨɩɟɪɚɬɨɪ - ɷɬɨ ɩɨɫɥɟɞɨɜɚ-
ɬɟɥɶɧɨɫɬɶ ɨɩɢɫɚɧɢɣ ɜɧɭɬɪɟɧɧɢɯ ɞɚɧɧɵɯ ɢ ɨɩɟɪɚɬɨɪɨɜ, ɡɚɤɥɸɱɟɧɧɚɹ ɜ ɮɢ-
ɝɭɪɧɵɟ ɫɤɨɛɤɢ. ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɜ ɋɢ/ɋɢ++ ɨɩɢɫɚɧɢɟ ɮɭɧɤɰɢɣ ɧɟ
ɦɨɠɟɬ ɛɵɬɶ ɜɥɨɠɟɧɧɵɦ, ɜ ɬɟɥɟ ɮɭɧɤɰɢɢ ɧɟɥɶɡɹ ɨɛɴɹɜɢɬɶ ɞɪɭɝɭɸ ɮɭɧɤ-
ɰɢɸ.
    Ɏɭɧɤɰɢɹ, ɜɨɡɜɪɚɳɚɸɳɚɹ ɫɪɟɞɧɟɟ ɚɪɢɮɦɟɬɢɱɟɫɤɨɟ ɬɪɟɯ ɜɟɳɟɫɬɜɟɧɧɵɯ
ɞɚɧɧɵɯ, ɦɨɠɟɬ ɛɵɬɶ ɨɩɢɫɚɧɚ ɬɚɤ:
     double sred(double x, double y, double z)
        {
        double s;
        s=x+y+z;
        return s/3;
        }
    Ⱦɥɹ ɜɵɡɨɜɚ ɬɚɤɨɣ ɮɭɧɤɰɢɢ ɩɪɢ ɭɫɥɨɜɢɢ, ɱɬɨ ɩɪɟɞɜɚɪɢɬɟɥɶɧɨ ɨɛɴɹɜɥɟ-
ɧɵ ɩɟɪɟɦɟɧɧɵɟ p, a, b, ɢ c, ɦɨɠɧɨ ɡɚɩɢɫɚɬɶ ɨɩɟɪɚɬɨɪ:
     p=sred(a, b, c);
     Ɉɱɟɜɢɞɧɨ, ɱɬɨ ɮɭɧɤɰɢɹ ɞɨɥɠɧɚ ɛɵɬɶ ɨɩɢɫɚɧɚ ɞɨ ɬɨɝɨ, ɤɚɤ ɜɫɬɪɟɬɢɬɫɹ
ɟɟ ɩɟɪɜɵɣ ɜɵɡɨɜ. ɗɬɨ ɧɟ ɜɫɟɝɞɚ ɜɨɡɦɨɠɧɨ, ɧɚɩɪɢɦɟɪ, ɤɨɝɞɚ ɞɜɟ ɮɭɧɤɰɢɢ
ɜɵɡɵɜɚɸɬ ɞɪɭɝ ɞɪɭɝɚ. Ʉɨɝɞɚ ɩɪɨɝɪɚɦɦɚ ɫɨɫɬɨɢɬ ɢɡ ɧɟɫɤɨɥɶɤɢɯ ɮɚɣɥɨɜ,
ɩɨɥɧɨɟ ɨɩɢɫɚɧɢɟ ɮɭɧɤɰɢɢ ɞɨɥɠɧɨ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɬɨɥɶɤɨ ɜ ɨɞɧɨɦ ɮɚɣɥɟ,
ɧɨ ɜɵɡɨɜɵ ɮɭɧɤɰɢɢ ɜɨɡɦɨɠɧɵ ɢɡ ɪɚɡɧɵɯ ɮɚɣɥɨɜ ɩɪɨɝɪɚɦɦɵ. ɑɬɨɛɵ ɪɚɡ-
ɪɟɲɢɬɶ ɩɨɞɨɛɧɵɟ ɩɪɨɬɢɜɨɪɟɱɢɹ, ɩɪɟɞɭɫɦɨɬɪɟɧɵ ɞɜɟ ɮɨɪɦɵ ɨɩɢɫɚɧɢɹ
ɮɭɧɤɰɢɣ: ɩɨɥɧɚɹ ɮɨɪɦɚ, ɧɚɡɵɜɚɟɦɚɹ ɬɚɤɠɟ ɨɩɪɟɞɟɥɟɧɢɟɦ ɮɭɧɤɰɢɢ, ɢ ɫɨ-
ɤɪɚɳɟɧɧɚɹ, ɧɚɡɵɜɚɟɦɚɹ ɨɩɢɫɚɧɢɟɦ ɩɪɨɬɨɬɢɩɚ ɮɭɧɤɰɢɢ, ɢɥɢ ɩɪɨɫɬɨ ɩɪɨɬɨ-
ɬɢɩɨɦ. ɉɪɨɬɨɬɢɩ ɮɭɧɤɰɢɢ ɫɨɞɟɪɠɢɬ ɬɨɥɶɤɨ ɡɚɝɨɥɨɜɨɤ ɮɭɧɤɰɢɢ ɢ ɡɚɞɚɟɬ
ɢɦɹ ɮɭɧɤɰɢɢ, ɬɢɩ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɡɧɚɱɟɧɢɹ ɢ ɬɢɩɵ ɩɚɪɚɦɟɬɪɨɜ. ɉɨ ɷɬɨɣ
ɢɧɮɨɪɦɚɰɢɢ ɩɪɢ ɤɨɦɩɢɥɹɰɢɢ ɩɪɨɝɪɚɦɦɧɨɝɨ ɮɚɣɥɚ ɦɨɠɧɨ ɩɪɨɜɟɪɢɬɶ ɩɪɚ-
ɜɢɥɶɧɨɫɬɶ ɡɚɩɢɫɢ ɜɵɡɨɜɚ ɮɭɧɤɰɢɢ ɢ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɡɧɚɱɟ-
ɧɢɹ. ȼ ɋɢ++ ɬɪɟɛɭɟɬɫɹ, ɱɬɨɛɵ ɜɵɡɨɜɭ ɥɸɛɨɣ ɮɭɧɤɰɢɢ ɩɪɟɞɲɟɫɬɜɨɜɚɥɨ ɜ
ɬɨɦ ɠɟ ɮɚɣɥɟ ɥɢɛɨ ɩɨɥɧɨɟ ɨɩɪɟɞɟɥɟɧɢɟ ɮɭɧɤɰɢɢ, ɥɢɛɨ ɨɩɢɫɚɧɢɟ ɟɟ ɩɪɨɬɨ-
ɬɢɩɚ.
     ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ ɪɹɞ ɞɨɩɨɥɧɢɬɟɥɶɧɵɯ ɜɨɡɦɨɠɧɨɫɬɟɣ ɨɩɢɫɚɧɢɹ ɮɭɧɤ-
ɰɢɣ ɜ ɋɢ++:
1. ɉɪɢ ɨɩɢɫɚɧɢɢ ɩɪɨɬɨɬɢɩɚ ɮɭɧɤɰɢɢ ɦɨɠɧɨ ɧɟ ɭɤɚɡɵɜɚɬɶ ɢɦɟɧɚ ɩɚɪɚɦɟɬ-
ɪɨɜ, ɞɨɫɬɚɬɨɱɧɨ ɭɤɚɡɚɬɶ ɢɯ ɬɢɩɵ.
2. Ⱦɥɹ ɱɚɫɬɢ ɩɚɪɚɦɟɬɪɨɜ ɮɭɧɤɰɢɢ ɦɨɠɧɨ ɡɚɞɚɜɚɬɶ ɡɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ,
ɱɬɨ ɩɨɡɜɨɥɹɟɬ ɜɵɡɵɜɚɬɶ ɮɭɧɤɰɢɸ ɫ ɦɟɧɶɲɢɦ ɱɢɫɥɨɦ ɚɪɝɭɦɟɧɬɨɜ, ɱɟɦ ɩɪɟ-
ɞɭɫɦɨɬɪɟɧɨ ɨɩɢɫɚɧɢɟɦ ɮɭɧɤɰɢɢ. Ɂɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ ɦɨɠɧɨ ɭɤɚɡɵ-
ɜɚɬɶ ɬɨɥɶɤɨ ɞɥɹ ɩɨɫɥɟɞɧɢɯ ɩɚɪɚɦɟɬɪɨɜ ɜ ɫɩɢɫɤɟ. ɇɚɩɪɢɦɟɪ, ɮɭɧɤɰɢɹ sred
ɦɨɝɥɚ ɛɵ ɛɵɬɶ ɨɩɢɫɚɧɚ ɬɚɤ:
     double sred(double x, double y, double z=0)
        {
        double s;
        s = x+y+z;
                                  30