Konstruktor nomi sinf nomi bilan bir xil bo‘lishi lozimdir. Misol uchun siz employee sinfdan foydalansangiz, konstruktor ham employee nomga ega bo‘ladi. Agar dasturda konstruktor ta’rifi berilgan bo‘lsa ob’ekt yaratilganda avtomatik chaqiriladi. Quyidagi dasturda employee nomli sinf kiritilgandir: { public: employee(long, float); void show_employee(void); private: long employee_id; float salary; }; Konstruktor ta’rifi: employee::employee(long empl_id, float sal) {employee_id = empl_id; if (salary < 50000.0) salary = sal; Else salary = 0.0; } Shu sinfdan foydalanilgan dastur: #include class employee { public: 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 ob’ekt ta’rifilanganda parametr uzatish mumkin: employee worker(101, 10101.0); Agar dasturda employee tipidagi ob’ektlar mavjud bo‘lsa har birini quyidagicha initsializatsiya qilish mumkin employee worker(101, 10101.0); employee secretary(57, 20000.0); employee manager(1022, 30000.0); 5.Mavzu.Abstrakt sinflarni tashkil etish. Serial Key Generator foydalanuvchini ushbu kalitni yaratishda ishtirok etadigan belgilarni sozlashni taklif qiladi. Masalan, siz faqat katta yoki kichik harflarni ko'rsatishingiz, shuningdek raqamlarni qo'shishingiz yoki olib tashlashingiz mumkin. Bundan tashqari, bitta koddagi ustunlar soni va ulardagi belgilar soni sozlangan.
Do'stlaringiz bilan baham: |