Konstruktorlarning chaqirilish tartibi. Sinf konstruktori chaqirilganda, avval asosiy sinflarning konstruktorlari qayta ishlanadi, shundan keyingina hosil bo'lganlarning konstruktorlari. Masalan, quyidagi sinflarni olamiz:
class Person { string name; int age; public Person(string name) { this.name = name; Console.WriteLine("Person(string name)"); } public Person(string name, int age) : this(name) { this.age = age; Console.WriteLine("Person(string name, int age)"); } } class Employee : Person { string company; public Employee(string name, int age, string company) : base(name, age) { this.company = company; Console.WriteLine("Employee(string name, int age, string company)"); } } Employee ob'ekti yaratishda:
Employee tom = new Employee("Tom", 22, "Microsoft"); Biz quyidagi konsol natijanini olamiz:
Person(string name) Person(string name, int age) Employee(string name, int age, string company)
class base {
int i, j;
public:
void set(int a, int b) { i=a; j=b; }
void show() { cout << i << " " << j << "\n"; }
};
class derived : public base {
int k;
public:
derived(int x) { k=x; }
void showk() { cout << k << "\n"; }
};
int main()
{
derived ob(3);
ob.set(1, 2); // asos sinf a’zosiga ruxsat ochiq
ob.show(); // asos sinf a’zosiga ruxsat ochiq
ob.showk(); // voris sinf a’zosidan foydalanish
}
class derived : public base {….}
Bunda asos sinfning barcha public a’zolari voris sinfning ham public a’zolari bo’ladi. Asos sinfning barcha himoyalangan (protected) a’zolari voris sinfning protected a’zolari bo’ladi. Lekin, asos sinfning private a’zolari voris sinf uchun ham, boshqa sinflar uchun ham not accessible bo’ladi ya’ni asos sinfning private a’zolariga to’g’ridan to’g’ri murojaat yo’q.
Agar voris sinf asos sinfdan private asosida yaratilsa nima bo’ladi? Voris sinf a’zolari qanday xususiyatga ega bo’ladi? Agar voris sinf private xususiyati orqali yaratilsa, bunda asos sinfning barcha public va protected a’zolari voris sinfning private a’zolari qatoriga o’tadi.