1 – dars. C++ Dasturlash tilining kelib chiqishi xaqida ma’lumot



Yüklə 0,69 Mb.
səhifə113/121
tarix02.01.2022
ölçüsü0,69 Mb.
#37187
1   ...   109   110   111   112   113   114   115   116   ...   121
aaaaKitob majmuasiiii

82 - DARS. POLIMORFIZM.
Polimorf ob'ekt bu dastur bajarilishi davomida shaklini o’zgartirishi mumkin bo’lgan ob'ektdir. Misol uchun telenfon sinfi kiritilgan bo’lsin:

class phone

{

public:


void dial(char "number) { cout << "Nabor nomera " << number << endl; }

void answer(void) { cout << "Ojidanie otveta" << endl; }

void hangup(void) { cout << "Zvonok vihpolnen - povesit' trubku" << endl; }

void ring(void) { cout << "Zvonok, zvonok, zvonok" << endl;)

phone(char *number) { strcpy(phone::number, number); };

private:


char number[13];

);

Kuyidagi PHONEONE.CPP dasturi phone sinfidan foydalanadi:



#include

#include

class phone

{

public:



void dial(char *number) { cout << "Nabor nomera " << number << endl; }

void answer(void) { cout << "Ojidanie otveta" << endl; }

void hangup(void) { cout << "Zvonok vihpolnen - povesit' trubku" << endl; }

void ring(void) { cout << "Zvonok, zvonok, zvonok" << endl; }

phone(char *number) { strcpy(phone::number, number); };

private:


char number[13];

};

void main(void)



{

phone telephone("555-1212");

telephone.dial("212-555-1212");

}

Agar tugmali va diskli telefon yaratish lozim bo’lsa va qo’ng’iroq qilish uchun 25 tsent to’lash lozim bo’lsa vorislik yordamida touch_tone va pay_phone schinflarini yaratish mumkin:



class touch_tone : phone

{

public:



void dial(char * number) { cout << "Pik pik Nabor nomera " << number << endl; }

touch_tone(char *number) : phone(number) { }

};

class pay_phone : phone



{

public:


void dial(char * number)

{

cout << "Pojaluysta, oplatite " << amount << " tsentov" << endl;



cout << "Nabor nomera " << number << endl;

}

pay_phone(char *number, int amount) : phone(number) { pay_phone::amount = amount; }



private:

int amount;

};

Yangi touch_tone va pay__phone sinflari hususiy dial usulidan foydalanadi. Qo’yidagi NEWPHONE.CPP dasturida shu sinflardan foydalanilgan



#include

#include

class phone

{

public:



void dial(char *number) { cout << "Nabor nomera " << number << endl; }

void answer(void) { cout << "Ojidanie otveta" << endl; }

void hangup(void) { cout << "Zvonok vihpolnen - povesit' trubku" << endl; }

void ring(void) { cout << "Zvonok, zvonok, zvonok" << endl; }

phone(char *number) { strcpy(phone::number, number); };

protected:

char number[13];

);

class touch_tone : phone



{

public:


void dial(char *number) { cout << "Pik pik Nabor nomera " << number << endl; }

touch_tone(char *number) : phone(number) { }

};

class pay_phone : phone



{

public:


void dial(char * number) { cout << "Pojaluysta, oplatite " << amount << " tsentov" << endl; cout << "Nabor nomera " << number << endl; }

pay_phone(char * number, int amount) : phone(number) { pay_phone::amount = amount; }

private:

int amount ;

};

void main (void)



{

phone rotary("303-555-1212");

rotary.dial("602-555-1212");

touch_tone telephone("555-1212");

telephone.dial("212-555-1212");

pay_phone city_phone("555-1111", 25);

city_phone.dial("212-555-1212");

}

Dastur bajarilganda ekranga qo’yidagi ma'lumotlar chiqadi:



S:\> NEWPHONE

Nabor nomera 602-555-1212

Pik pik Nabor nomera 212-555-1212

Pojaluysta, oplatite 25 tsentov

Nabor nomera 212-555-1212

Bu misolda polimorf ob'ektlarpdan foydalanilmagan.



Yüklə 0,69 Mb.

Dostları ilə paylaş:
1   ...   109   110   111   112   113   114   115   116   ...   121




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin