9- ma’ruza mavzu: do’ST FUNKSIYA VA do’st sinflar
Download 50.06 Kb.
|
9- ma\'ruza matni
employee(long, float);
void show_employee(void); private: long employee_id; float salary; }; employee::employee(long empl_id, float sal) { employee_id = empl_id; if (salary < 50000.0) salary = sal; else salary = 0.0; } void employee::show_employee(void) { cout << "Nomer: " << employee_id << endl; cout << "Maosh: " << salary << endl; } int main() { employee worker(101, 10101.0); cout<<"ishchi"< worker.show_employee(); return 0; } Konstruktordan foydalanib, obyekt ta’rifilanganda parametr uzatish mumkin: employee worker(101, 10101.0); Agar dasturda employee tipidagi obyektlar mavjud bo‘lsa har birini quyidagicha initsializatsiya qilish mumkin: employee worker(101, 10101.0); employee secretary(57, 20000.0); employee manager(1022, 30000.0); Konstruktorlar va ko‘zda tutilgan qiymatlar. Konstruktorlarda ko‘zda tutilgan qiymatlardan ham foydalanish mumkindir. Misol uchun quyidagi konstruktor employee maoshi qiymatini dasturda ko‘rsatilmagan bo‘lsa 10000.0 teng qilib oladi:employee::employee(long empl_id, float sal = 100.00) { employee_id = empl_id; if (salary < 50000.0) salary = sal; else salary = 0.0; } C++ tilida konstruktorlarni ham qo‘shimcha yuklash mumkindir. Quyidagi dasturda konstruktor employee qo‘shimcha yuklangan. Birinchi konstruktor, dastur xizmatchi, nomer va oyligi ko‘rsatilishini talab qiladi. Ikkinchi konstruktor oylikni kiritilishini so‘raydi. Sinf ta’rifi ichida ikkala konstruktor prototipi ko‘rsatilishi lozim:Download 50.06 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling