Qisqa qilib aytganda: Bir obyekt boshqa bir obyektning hususiyat va metodlarini oladi. Yani bir class boshqa classdan nasl oladi. 1 chi classning hamma hususiyat va metodlari 2 chi classga o’tadi.
Baliq sinfi (class Baliq)
1. Suzadi, ikra tashlaydi
2. Ko’l yoki dengiz
Tuna sinfi (class Tuna)
1. Tez suzadi, ikra tashlaydi
2. Dengiz baliqi
Carp sinfi (Class Carp)
1. Sekin suzadi, ikra tashlaydi
2. Ko’l baliqi
Misol
#include
#include
using namespace std;
class Baliq {
public:
bool SuvningTozaligi;
void Suzish() {
if (SuvningTozaligi) {
cout<<"Ko'lda suzadigan baliq"<
}
else cout<<"Dengizda suzadigan baliq"<
}
};
class Tuna:public Baliq {
public:
Tuna() {
SuvningTozaligi=false;
}
};
Misol (davomi…)
class Carp:public Baliq {
public:
Carp() {
SuvningTozaligi=true;
}
};
int main() {
Carp MeningTushligim;
Tuna MeningNonushtam;
cout<<"Mening oziq-ovqatim suzish bo`yicha tanlanadi:";
cout<<"Tushlikka:";
MeningTushligim.Suzish();
cout<<"Nonushtaga:";
MeningNonushtam.Suzish();
getch();
}
Classlardan meros olish Class B class A:public B-a-pub,b-pro,c-private class A:protected B a-protected,b-pro,c-private class A:B - a,b,c-private { public: a; protected: b; private: c; } #include #include #include using namespace std; class Uchburchak { public: float a,b,c,s,p,h; float YuzaniHisobla(float a,float b, float c) { cout<<"1-metod"< p=(a+b+c)/2; s=sqrt(p*(p-a)*(p-b)*(p-c)); return s; }; float BalandlikniHisobla (float a,float b, float c) { h=2*YuzaniHisobla(a,b,c)/c; return h; } }; class TugriUchburchak: Uchburchak { public: float YuzaniHisobla(float a,float b, float c) { cout<<"2-metod"< s=a*b/2; return s; } }; int main () { Uchburchak uchburchak; TugriUchburchak tugriuchburchak; float u=uchburchak.BalandlikniHisobla(3,4,5); cout< float t=tugriuchburchak.YuzaniHisobla(3,4,5); cout< return 0; } Konstruktor va destruktorlarda vorislik Konstruktorlar meros bo’lmagani uchun, xosila sinfni yaratishda undan meros bo’lgan ma’lumot – a’zolari asosiy (bazaviy) sinf konstruktori orqali initsializatsiyalanishi lozim. Asosiy sinf konstruktori avtomatik ravishda chaqiriladi va xosila sinfni konstruktoridan oldin bajariladi. Asosiy (bazaviy) sinfni konstruktorining parametrlari xosila sinfni konstruktorni aniqlashda ko’rsatiladi. Shunday qilib argumentlarni xosila sinfni konstruktoridan asosiy (bazaviy) sinfni konstruktoriga uzatish vazifasi bajariladi. Doira class Aylana classdan meros olganda
Aylana (class Aylana)
1. Radius, uzunlik
2.Konstruktor: Uzunlikni hisoblaydi
Doira sinfi (class Doira)
1. Radius, kontur uzunligi
2. Konstruktor: Uzunlik, Yuzani hisoblaydi
Misol 2
#include
#include
using namespace std;
class Aylana {
protected:
float radius;
float uzunlik;
public:
Aylana (float radius) {
Aylana::radius=radius;
uzunlik=2*3.14*radius;
}
void show () {
cout<
}
};
Misol 2 davomi
class Doira:public Aylana {
float yuza;
public:
Doira(float radius):Aylana(radius) {
yuza=2*3.14*radius*radius;
}
void show() {
cout<
cout<
}
};
int main() {
Aylana aylana(12.6);
Doira doira(12.6);
aylana.show();
doira.show(); getch();
}
Obyekt mavjud bo’lmaganda, public static Sinfning barcha obyektlarida o’zaro moslikda foydalaniladigan, elementi static sifatida e’lon qilinishi tushinarli bo’ldi, lekin, shunday holat bo’lishi mumkin: hech qanday obyekt yaratilmagan, ammo, ushbu elementdan foydalanish zarur. Dasturda bu elementdan foydalanish uchun, uni public hamda static deb e’lon qilish zarur. Quyidagi dasturda huddi shu holatga e’tibor qaratilgan. Misol 3
#include //strcpy() uchun
#include //printf() uchun
#include //_getch() uchun
using namespace std;
class book_series {
book_series();
public:
static void show_book(void);
//funksiyaning statik elementini chop etish uchun, ushbu atribut qo`shiladi
static int page_count;
private:
char title [64];
char author[64];
float price;
};
Misol 3 davomi…
int book_series::page_count;
//O`zgaruvchini global o`zgaruvchi sifatida e'lon qilish
void book_series::show_book (void)
{
printf("Sahifalar soni=%d\n",page_count);
}
int main(void)
{
book_series::page_count = 256; /*o`zgaruvchiga 256 qiymati o`zlashtirilgan*/
book_series::show_book();
return 0;
}
Sinfni to‘liqmas eloni class Sinf2; // синфнинг тўлиқмас эълони class Sinf1 { int x; Sinf2 * sinf2; // sinf2 синфига кўрсаткич public: Sinf1(int _x) {x=_x;} }; int main(){ //… return 0; } class Sinf2 { // Sinf2 синфининг тўлиқ эълони int a; public: Sinf2(); }; Merosxo‘rlikning turlari - Yagona merosxo'rlik ( Engl. Single Inheritance)
- Ko'p merosxo'rlik ( Engl. Multiple Inheritance)
- Ierarxik merosxo'rlik ( Engl. Hierarchical Inheritance)
- Ko'p qatlamli merosxo'rlik ( Engl. Multilevel Inheritance)
- Gibrid merosxo'rlik ( Engl. Hybrid Inheritance)
Yagona merosxo'rlik ( Engl. Single Inheritance)
Classlarni bir biridan meros olishida public, private, va protected kalit so‘zlarini ishlatilishiga qarab undagi hususiyat va metodlarga murojatlar turlicha bo‘lishi mumkin
Yagona merosxo'rlik ( Engl. Single Inheritance) |
Avlod Class
|
Avlod Class
|
Avlod Class
|
Ajdod class
|
Public Mode
|
Private Mode
|
Protected Mode
|
Private
|
Not Inherited
|
Not Inherited
|
Not Inherited
|
Protected
|
Protected
|
Private
|
Protected
|
Public
|
Public
|
Private
|
Protected
| Ko'p merosxo'rlik ( Engl. Multiple Inheritance) Ko'p merosxo'rlik ( Engl. Multiple Inheritance) Ierarxik merosxo'rlik ( Engl. Hierarchical Inheritance) Ierarxik merosxo'rlik ( Engl. Hierarchical Inheritance) Ierarxik merosxo'rlik ( Engl. Hierarchical Inheritance) Ko'p qatlamli merosxo'rlik ( Engl. Multilevel Inheritance) Ko'p qatlamli merosxo'rlik ( Engl. Multilevel Inheritance) Gibrid merosxo'rlik ( Engl. Hybrid Inheritance) Gibrid merosxo'rlik ( Engl. Hybrid Inheritance) Gibrid merosxo'rlik ( Engl. Hybrid Inheritance) Gibrid merosxo'rlik ( Engl. Hybrid Inheritance) Gibrid merosxo'rlik ( Engl. Hybrid Inheritance) Tamom
Do'stlaringiz bilan baham: |