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



Yüklə 0,69 Mb.
səhifə108/121
tarix02.01.2022
ölçüsü0,69 Mb.
#37187
1   ...   104   105   106   107   108   109   110   111   ...   121
aaaaKitob majmuasiiii

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.



Yüklə 0,69 Mb.

Dostları ilə paylaş:
1   ...   104   105   106   107   108   109   110   111   ...   121




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

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin