cout<<"\n"<<"boss"<
boss.show_employee();
return 0;
}
Konstruktor va destruktor Konstruktorlar. Konstruktorlar bu sinf komponenta
funkstiyalari bo’lib, ob’ektlarni avtomatik inistializastiya qilish uchun ishlatiladi.
Konstruktorlar ko’rinishi quyidagicha bo’lishi mumkin:
Sinf nomi (formal parametrlar ro’yxati)
{konstruktor tanasi}
Bu komponenta funkstiya nomi sinf nomi bilan bir xil bo’lishi lozim.
Misol uchun complex sinfi uchun konstruktorni quyidagicha kiritish
mumkin :
complex (double re = 0.0; double im = 0.0 )
{real=re; imag=im;}
Konstruktorlar uchun qaytariluvchi tiplar, xatto void tipi ham ko’rsatilmaydi.
Dasturchi tomonidan ko’rsatilmagan holda ham ob’ekt yaratilganda konstruktor avtomatik
ravishda chaqiriladi.
Masalan ob’ekt complex cc; shaklida aniqlangan bo’lsa, konstruktor avtomatik
chaqirilib real va imag parametrlari avtomatik ravishda 0.0 qiymatlariga ega bo’ladi.
Ko’zda tutilgan holda parametrsiz konstruktor va quyidagi tipdagi nusxa olish
konstruktorlari yaratiladi: T :: T (const T&)
Misol uchun
class F
{...
public : F(const T&)
...
}
Sinfda bir nechta konstruktorlar bo’lishi mumkin, lekin ularning faqat bittasida
parametrlar qiymatlari oldindan ko’rsatilgan bo’lishi kerak.
Konstruktor adresini hisoblash mumkin emas. Konstruktor parametri sifatida o’z
sinfining nomini ishlatish mumkin emas, lekin bu nomga ko’rsatkichdan foydalanish
mumkin.
Konstruktorni oddiy komponenta funkstiya sifatida chakirib bo’lmaydi.
Konstruktorni ikki xil shaklda chaqirish mumkin :
Sinf_nomi. Ob’ekt_nomi (konstruktor_xaqiqiy_parametlari)
Sinf_nomi (konstruktor_xaqiqiy_parametlari)
Birinchi shakl ishlatilganda xaqiqiy parametrlar ro’yxati bush bulmasligi
lozim. Bu shakldan yangi ob’ekt ta’riflanganda foydalaniladi:
complex SS(10.3; 0.22)
// real=10.3; SS.imag= 0.22;
complex EE (2.3)
// EE . real= 2.3;
EE.imag= 0.0;
complex D() // xato
Konstruktorni ikkinchi shaklda chaqirish nomsiz ob’ekt yaratilishiga olib keladi. Bu
nomsiz ob’ektdan ifodalarda foydalanish mumkin.
Misol uchun :
complex ZZ= complex (4.0;5.0);
Bu ta’rif orkali ZZ ob’ekt yaratilib, unga nomsiz ob’ekt qiymatlari(real= 4.0; imag=
5.0) beriladi;
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 bulsa ob’ekt yaratilganda avtomatik chaqiriladi.
Quyidagi dasturda employee nomli sinf kiritilgandir:
class employee
{
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
using namespace std;
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
inistializastiya qilish mumkin
employee worker(101, 10101.0);
employee secretary(57, 20000.0);
employee manager(1022, 30000.0);
Satrli maydonga misol. Keyingi misolda satrli maydon string tipidagi o’zgaruvchi
sifatida beriladi.
#include
#include
using namespace std;
class employee
{
public:
employee(string , long, float);
void show_employee(void);
int change_salary(float) ;
long get_id(void);
private:
string name;
long employee_id;
float salary;
};
employee::employee(string name, long employee_id, float salary)
{
employee::name= name;
employee::employee_id = employee_id;
if (salary < 50000.0)
employee::salary = salary;
else
employee::salary = 0.0;
}
void employee::show_employee(void)
{
cout << "Ism: " << name << endl;
cout << "Nomer: " << employee_id << endl;
cout << "Maosh: " << salary << endl;
}
int main()
{
employee worker("Happy Jamsa", 101, 10101.0);
worker.show_employee();
return 0;
}
Natija:
Ism:
Happy Jamsa
Nomer: 101
Maosh: 10101
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;
}
Konstruktorlarni qo’shimcha yuklash. C++ tilida konstruktorlarni ham qo’shimcha
yuklash mumkindir. Quyidagi dasturda konstruktor employee qo’shimcha yuklangandir.
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:
#include
using namespace std;
class employee
{
public:
employee(long, float);
employee(long);
void show_employee(void);
private:
long employee_id;
float salary;
};
employee::employee(long employee_id, float salary)
{
employee::employee_id = employee_id;
if (salary < 50000.0) employee::salary = salary;
else
employee::salary = 0.0;
}
employee::employee(long employee_id)
{
employee::employee_id = employee_id;
do
{
cout << "Maosh kiriting $50000 dan kichik: ";
cin >> employee::salary;
}
while (salary >= 50000.0);
}
void employee::show_employee(void)
{
cout << "Nomer: " << employee_id << endl;
cout << "Maosh: " << salary << endl;
}
int main()
{
cout<<"ishchi"<
employee worker(101, 10101.0);
worker.show_employee();
cout<<"manager"<
employee manager(102);
manager.show_employee();
return 0;
}
Do'stlaringiz bilan baham: