employee(long, float); void show_employee(void); private:
long employee_id; float salary;
};
employee::employee(long empl_id, float sal)
ega boiadi. Agar dasturda konstruktor ta’rifi berilgan boisa, obyekt yaratilganda avtomatik chaqiriladi. Quyidagi dasturda employee nomli sinf kiritilgan:
class employee { ‘ public:
employee(long, float); void show_employee(void); private:
long employee_id; float salary;
};Konstruktor ta’rifi:emplovee::emplovee(long empl id, float sal){
employeeid = empl_id; if (salary <50000.0) salarv=sal; else
salarv=0.0;
}
Shu sinfdan foydalanilgan dastur:
#include < iostream > using namespace std; class employee {
public:
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" «endl; worker.show_employee(); return 0;
}
Konstruktordan foydalanib, obyekt ta’riflanganda parametr uzatish mumkin: employee worker(101, 10101.0);
Agar dasturda employee turidagi obyektlar mavjud boisa, har birini quyidagicha initsializatsiya qilish mumkin employee worker(101, 10101.0); employee secretary(57. 20000.0); employee manager(1022, 30000.0);
Do'stlaringiz bilan baham: |