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


- DARS. SINFLAR IERARHIYASINI QO’RISH



Yüklə 0,66 Mb.
səhifə83/88
tarix22.02.2023
ölçüsü0,66 Mb.
#101187
1   ...   80   81   82   83   84   85   86   87   88
1 – dars. C Dasturlash tilining kelib chiqishi xaqida ma’lumot

81 - DARS. SINFLAR IERARHIYASINI QO’RISH.

C++ tilida bir sinf uchun ajdod sinf o’z o’rnida boshqa sinfning avlodi bo’lishi mumkin. Misol uchun somputer sinfi workstation sinfi uchun ajdod sinf bo’lsin:


class work_station : public computer
{
public:
work_station (char *operating_system, char *name, int hard_disk, float floppy, char *screen, long colors, int x_res, int y_res,int processor, int speed, int RAM);
void show_work_station(void);
private:
char operating_system[64];
};
Bu misolda workstation sinfi konstruktori computer sinfining konstruktorini chaqiradi u bo’lsa somruter_screen va mother_board sinfi konstruktorlarini chaqiradi:
work_station::work_station( char *operating_system, char *name, int hard_disk, float floppy, char *screen, long colors, int x_res, int y_res, int processor, int speed, int RAM) : computer (name, hard_disk, floppy, screen, colors, x_res, y_res, processor, speed, RAM)
{
strcpy(work_station::operating_system, operating_system);
}
Bu misolda computer sinfi asosiy sinfdir. Lekin computer sinfi computer_screen va mother_board sinflarining avlodidir. Natijada work_station sinfi hama uch sinf harakteristikalarini merosga oladi.
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,66 Mb.

Dostları ilə paylaş:
1   ...   80   81   82   83   84   85   86   87   88




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

    Ana səhifə