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.
Dostları ilə paylaş: |