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



Download 0,66 Mb.
bet113/121
Sana29.12.2021
Hajmi0,66 Mb.
#83735
1   ...   109   110   111   112   113   114   115   116   ...   121
Bog'liq
1 – dars. C Dasturlash tilining kelib chiqishi xaqida ma’lumot

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.



Download 0,66 Mb.

Do'stlaringiz bilan baham:
1   ...   109   110   111   112   113   114   115   116   ...   121




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish