9- ma’ruza mavzu: do’ST FUNKSIYA VA do’st sinflar


Download 50.06 Kb.
bet4/12
Sana10.02.2023
Hajmi50.06 Kb.
#1186380
1   2   3   4   5   6   7   8   9   ...   12
Bog'liq
9- ma\'ruza matni

#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;
}


Obyektlar massivlari.
Obyektlar massivi ta’riflash uchun sinf ko‘zda tutilgan (parametrsiz) konstruktorga ega bo‘lishi kerak.
Obyektlar massivi ko‘zda tutilgan konstruktor tomonidan, yoki har bir element uchun konstruktor chaqirish yo‘li bilan initsializatsiya qilinishi mumkin.
class complex a[20]; //ko‘zda tutilgan parametrsiz konstruktorni chaqirish
class complex b[2]={complex(10),complex (100)}; //oshkor chaqirish
Quyidagi misolda player, sinfi kiritiladi. Dasturda sinf funksiyasi show_player va konstruktor tashqarisida ta’riflanadi. So‘ngra player tipidagi ikki massiv yaratilib, har biri hakidagi ma’lumot ekranga chiqariladi.
#include
#include
using namespace std;
class player
{
public:
player();
player (string name,int weight, int age);
void show_player (void);
private:
string name;
int weight;
int age;
};
player::player()
{
name="";
weight = 0;
age = 0;
};
player::player(string name,int weight, int age)
{
player::name=name;
player::weight = weight;
player::age = age;
};
void player::show_player (void)
{
cout<<"Ism: " << name << endl;
cout<<"Vazn: " << weight << endl;
cout<<"Yosh: " << age << endl;
}
class array_player
{ public:
void show_array(player a[],int n)
{ for(int i=0;i
{a[i].show_player();cout<
void input_array(player a[],int n)
{string name;int weight,age;
for(int i=0;i
{cin>>name>>weight>>age;
a[i]=player(name,weight,age); } }
};


int main()
{
array_player arr;
Player happy[]={
player("Olimov",58,24),
player("Alimov",72,35)
};
arr.show_array(happy,2);
player matt[2];
arr.input_array(matt,2);
arr.show_array(matt,2);
return 0;
}
Initsializatorlar ro‘yxati. Konstruktor yordamida obyekt ma’lumotlarni initsiyalizatsiyalashni ikkita usuli mavjud.
Birinchi usulda parametrlar qiymatlari konstruktor tanasiga uzatiladi. Ikinchi usulda esa ushbu sinfdagi initsializatorlar ro‘yxatidan foydalanish nazarda tutilgan. Bu ro‘yxat parametrlar ro‘yxati va konstruktor tanasi orasiga joylashadi. Ro‘yxatdagi har bir initsializator konkret aniq komponentaga bog‘liq va quyidagi ko‘rinishga ega: ()



  1. Destruktorlar

Sinfning biror obyekti uchun ajratilgan xotira obyekt yo’qotilgandan so‘ng bo‘shatilishi lozimdir.
Sinflarning maxsus komponentalari destruktorlar, bu vazifani avtomatik bajarish imkonini yaratadi.
Destruktorni standart shakli quyidagicha :
~ sinf_nomi ( ) {destruktor tanasi}
Destruktor parametri yoki qaytariluvchi qiymatga ega bo‘lishi mumkin emas (xatto void tipidagi).
Agar sinfda oshkor destruktor mavjud bo‘lmasa, ko‘zda tutilgan destruktor chaqiriladi.
Dastur obyektni o‘chirganda destruktor avtomatik chaqiriladi.
Misol:
#include
using namespace std;
class Person{
public:
Person (){
cout<<"Yaratidi"<

Download 50.06 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   12




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling