C++ Kirish Assalomu alaykum hurmatli dasturchi. Bugun siz bilan C++
Download 1.28 Mb.
|
C
- Bu sahifa navigatsiya:
- "Meros" dan qachon va qanday vaqtda foydalanish kerak
- Kop meros.
- Meros Huquqi.
- C++ Polimorfizm. (Sinf/Class)
Meros olishderived class (bola) - boshqa sinfdan meros qolgan sinf. base class (ota-ona) - meros bo'lib o'tgan sinf. Sinfdan meros olish uchun :belgidan foydaniladi. Quyidagi misolda, Carsinf (bola) atributlar va usullarni Vehicle sinfdan (ota-onadan) meros qilib oladi : #include #include using namespace std; // Base class class Vehicle { public: string brand = "Ford"; void honk() { cout << "Tuut, tuut! \n" ; } }; // Derived class class Car: public Vehicle { public: string model = "Mustang"; }; int main() { Car myCar; myCar.honk(); cout << myCar.brand + " " + myCar.model; return 0; } Tuut, tuut! Ford Mustang "Meros" dan qachon va qanday vaqtda foydalanish kerak?"Meros" - bu odatda bitta class dan qayta qayta foydalanish imkoniyatini beradi. Yangi sinf yaratishda sinf attributlaridan foydalanish imkoniyati. Ko'p darajali meros#include using namespace std; class MyClass { public: void myFunction() { cout << "Hello World. MasterSherkulov." ; } }; class MyChild: public MyClass { }; class MyGrandChild: public MyChild { }; int main() { MyGrandChild myObj; myObj.myFunction(); return 0; } Hello World. MasterSherkulov. Ko'p meros.Sinf shuningdek, vergul bilan ajratilgan ro'yxat yordamida bir nechta asosiy sinflardan olinishi mumkin : #include using namespace std; class MyClass { public: void myFunction() { cout << "Hello World. MasterSherkulov.\n" ; } }; class MyOtherClass { public: void myOtherFunction() { cout << "Hello World. MasterSherkulov.\n" ; } }; class MyChildClass: public MyClass, public MyOtherClass { }; int main() { MyChildClass myObj; myObj.myFunction(); myObj.myOtherFunction(); return 0; } Hello World. MasterSherkulov. Hello World. MasterSherkulov. Meros Huquqi.Siz Kirish xususiyatlarini aniqlash bo'limidan C ++ da uchta aniqlovchi mavjudligini bilib oldingiz . Hozirgacha biz faqat foydalanganmiz public(sinf a'zolariga sinf tashqarisidan kirish mumkin) va private (a'zolarga faqat sinf ichida kirish mumkin). Uchinchi spetsifikatorga protected o'xshash private, ammo unga meros qilib olingan klassda kirish mumkin : #include using namespace std; class Employee { protected: // Protected access specifier int salary; }; class Programmer: public Employee { public: int bonus; void setSalary(int s) { salary = s; } int getSalary() { return salary; } }; int main() { Programmer myObj; myObj.setSalary(50000); myObj.bonus = 15000; cout << "Salary: " << myObj.getSalary() << "\n"; cout << "Bonus: " << myObj.bonus << "\n"; return 0; } Salary: 50000 Bonus: 15000 C++ Polimorfizm. (Sinf/Class)Polimorfizm bu "ko'p shakllar" degan ma'noni anglatadi. Biz Meros orqali bir-biri bilan bog'liq bo'lgan ko'plab sinflarga ega bo'lganimizda paydo bo'ladi. Polimorfizm turli xil vazifalarni bitta harakatda turli yo'llarni bajarishimiz imkoniyatiga ega bo'lamiz. Obyektga abstrakt darajada qarash xususiyati. Masalan, turli xil oynalar mavjud: deraza oynasi, eshik oynasi, mashina oynasi, telefon oynasi. Bularning barchasi bir biridan ishlatilish sohasi, tuzulishi, shakli bilan farq qiladi. Lekin barchasini umumiy qilib oyna deb qarash mumkin. Polimorfizm turli xil obyektlar bilan bir xil uniformada ishlash imkoniyatini beradi. Polimorfizm orqali bir jarayonni turli yo'llar bilan tashkillashtirishimiz mumkin. Polimorfizm so'zi yunoncha ikki so'zning birikmasidan tashkil topgan «poly» — Ko'p va «morphs» — formalar. Polimorfizm ham ko'p formalar degan ma'noni anglatadi. #include #include using namespace std; // Base class class Animal { public: void animalSound() { cout << "The animal makes a sound \n" ; } }; // Derived class class Pig : public Animal { public: void animalSound() { cout << "The pig says: wee wee \n" ; } }; // Derived class class Dog : public Animal { public: void animalSound() { cout << "The dog says: bow wow \n" ; } }; int main() { Animal myAnimal; Pig myPig; Dog myDog; myAnimal.animalSound(); myPig.animalSound(); myDog.animalSound(); return 0; } Download 1.28 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling