- DARS. SINFLARDA VORISLIK VA POLIMORFIZM

|
- DARS. SINFLARDA VORISLIK VA POLIMORFIZM
|
bet | 81/89 | Sana | 16.11.2023 | Hajmi | 313,45 Kb. | | #100010 |
Bog'liq @AKT Official C haqida o\'zbekcha darslar majmui78 - DARS. SINFLARDA VORISLIK VA POLIMORFIZM.
Sinflarda vorislik
Har hil sinflar ob'ektlar va sinflarning uzlari vorislik munosabatlarida bo’lishi mumkin. Bu munosabat obe'ktlar va sinflar ierarhiyasi hosil bo’lishiga olib keladi. Sinflar ierarhiyasi mavjud sinflar yordamida, ya'ngi sinf yaratishga imkon beradi. Mavjud sinflar asosiy (yoki yaratuvchi) bu sinflar asosida shakillangan sinflar hosilaviy (yoki yaratilgan), ba'zida sinf vorislari yoki meroshurlari deb ataladi.
Hosilaviy sinflar asosiy sinflarning ma'lumotlari va metodlarini merosga oladilar. Bundan tashqari ularning o’z ma'lumotlari va metodlari bo’lishi mumkin.
Me'roslik o’tuvchi pommosontalar hosilaviy sinflarga ko’chmaydi, balki asosiy sinflarda qoladi. Agar ahborotlarni qayta ishlash uchun hosilaviy sinfda yuq bo’lgan ma'lumotlar kerak bo’lsa ular avtomatik ravishda asosiy sinfda qidiriladi.
Sodda vorislik
Vorislik hosilaviy sinfning mavjud asosiy sinfning harakteristikalarini merosga olish hususiyatidir. Misol uchun asosiy sinf employee mavjud bo’lsin:
class employee
{
public:
employee(char *, char *, float);
void show_employee(void);
private:
char name[64];
char position[64];
float salary;
};
Dasturda yangi manager sinfi yaratib employee sinfining qo’yidagi elementlarini merosga olishi lozim bo’lsin:
float annual_bonus;
char company_car[64];
int stock_options;
U holda manager sinfi qo’yidagicha ta'riflanadi:
class manager : public employee
{
public:
manager(char *, char *, char *, float, float, int);
void show_manager(void);
private:
float annual_bonus;
char company_car[64];
int stock_options;
};
Hosilaviy sinf asosiy sinfning elementlariga to’g’ridan – to’g’ri nuqta operatori orqali murojaat qila olmaydi. Qo’yidagi MGR_EMP.CPP dasturda vorislikdan foydalanish ko’rsatiladi:
#include
#include
class employee
{
public:
employee(char *, char *, float);
void show_employee(void);
private:
char name [ 64 ];
char position[64];
float salary;
};
employee::employee(char *name, char *position,float salary)
{
strcpy(employee::name, name);
strcpy(employee::position, position);
employee::salary = salary;
}
void employee::show_employee(void)
{
cout << "Imya: " << name << endl;
cout << "Doljnost': " << position << endl;
cout << "Oklad: $" << salary << endl;
}
class manager : public employee
{
public:
manager(char *, char *, char *, float, float, int);
void show_manager(void);
private:
float annual_bonus;
char company_car[64];
int stock_options;
};
manager::manager(char *name, char *position, char *company_car, float salary, float bonus, int stock_options) : employee(name, position, salary)
{
strcpy(manager::company_car, company_car) ;
manager::annual_bonus = bonus ;
manager::stock_options = stock_options;
}
void manager::show_manager(void)
{
show_employee();
cout << "Mashina firmih: " << company_car << endl;
cout << "Ejegodnaya premiya: $" << annual_bonus << endl;
cout << "Fondovihy optsion: " << stock_options << endl;
}
void main(void)
{
employee worker("Djon Doy", "Programmist", 35000);
manager boss("Djeyn Doy", "Vitse-prezident ", "Lexus", 50000.0, 5000, 1000);
worker.show_employee() ;
boss.show_manager();
}
Bu misolda manager sinfi konstruktoriga e'tibor berish lozimdir. Asosiy sinfning konstruktorini qo’yidagicha chaqirish lozimdir:
manager::manager(char *name, char *position, char *company_car, float salary, float bonus, int stock_options) :
employee(name, position, salary) //————————————— Konstruktor bazovogo klassa
{
strcpy(manager::company_car, company_car);
manager::annual_bonus = bonus;
manager::stock_options = stock_options;
}
Yana shunga e'tibor berinki show_manager funktsiyasi show_employee funktsiyasini chaqirishi mumkin, chunki manager sinfi employee sinfining vorisi bo’lgani uchun, umumiy elemenlariga murojaat qilishi mumkindir. Ikkinchi misol
Misol uchsun book asosiy sinfi mavjud:
class book
{
public:
book (char *, char *, int);
void show_book(void);
private:
char title[64];
char author[b 4];
int pages;
};
Yangi library_card sinfi book sinfiga qo’yidagi elemenlarni qo’shishi lozim:
char catalog[64];
int checked_out; // 1, agar tekshirilgan bulsa, aks holda O
Dasturda bu sinf vorislik yordamida qo’yidagicha ta'riflanishmi lozim
class library_card : public book
{
public:
library_card(char *, char *, int, char *, int);
void show_card(void);
private:
char catalog[64] ;
int checked_out;
};
Qo’yidagi BOOKCARD.CPP dasturida bu sinflardan foydalanish ko’rsatilgan:
#include
#include
class book
{
public:
book(char *, char *, int);
void show_book(void);
private:
char title [64];
char author[64];
int pages;
};
book::book(char •title, char *author, int pages)
{
strcpy(book::title, title);
strcpy(book::author, author);
book::pages = pages;
}
void book::show_book(void)
{
cout << "Nazvanie: " << title << endl;
cout << "Avtor: " << author << endl;
cout << "Stranits: " << pages << endl;
}
class library_card : public book
{
public:
library_card(char *, char *, int, char *, int);
void show_card(void) ;
private:
char catalog[64];
int checked_out;
};
library_card::library_card(char *title, char *author, int pages, char *catalog, int checked_out) : book(title, author, pages)
{
strcpy(library_card::catalog, catalog) ;
library_card::checked_out = checked_out;
}
void 1ibrary_card::show_card(void)
{
show_book() ;
cout << "Katalog: " << catalog << endl;
if (checked_out) cout << "Status: proverena" << endl;
else cout << "Status: svobodna" << endl;
}
void main(void)
{
library_card card( "Uchimsya programmirovat' na yazihke C++", "Jamsa", 272, "101SRR", 1);
card.show_card();
}
Yana shunga e'tibor berinki library_card konstruktori book sinfi konstruktorini chaqiradi.
|
| |