|
- DARS. SINFLAR IERARHIYASINI QO’RISH
|
bet | 84/89 | Sana | 16.11.2023 | Hajmi | 313,45 Kb. | | #100010 |
Bog'liq @AKT Official C haqida o\'zbekcha darslar majmui81 - 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.
|
| |