Mavzu: Obyektga yo’naltirilgan dasturlash asoslari Obyekt tushunchasi Struktura tushunchasi Class tushunchasi Konstruktor Desturktor
Class e’lon qilish class class_nomi {
Download 1.78 Mb.
|
1.C tilida OOP
Class e’lon qilishclass class_nomi {private: vars & methodspublic: vars & methodsprotected: vars & methods} obyekt nomlari;Sinf ob’ekti (sinf nusxasi) ni ta’riflash uchun quyidagi konstruksiyadan foydalaniladi: Ob’ekt orqali maydonlarga va usullarga quyidagicha murojjat qilish mumkin: < ob’ekt_nomi >. < ob’ekt_nomi >. Misolclass Car {private:int sum;int quantity_n;public:void quantity(int n);void buy();};void Car::quantity(int n) { quantity_n=n; }void Car::buy() { cout<}Misol#include using namespace std;… -> classint main(){Car a;a.quantity(40);a.buy();return 0;}Function overloading
Misolclass Aylanaclass Aylana{private:double radius;double PI;public:Aylana();Aylana(double r);void setPI();void setPI(double PI);void setRadius(double r);double getDiameter();double getLength();};Hususiyat Konstruktor Metod Konstruktor
class Aylana { public: Aylana(); Aylana(double r); }; KonstruktorAylana::Aylana(){cout << “Aylana yaratildi!”;}Aylana::Aylana(double r){radius = r;cout << “Aylana yaratildi!”;}Misol#include using namespace std;class Aylana{private:double radius;double PI;public:Aylana();Aylana(double r);void setPI();void setPI(double PI);void setRadius(double r);double getDiameter();double getLength();};Aylana::Aylana(){ cout << "Aylana yaratildi!" << endl; }; Aylana::Aylana(double r){ radius = r; cout << "Radiusi " << r << " bulgan aylana yaratildi!\n"; }; int main() { Aylana A1; Aylana A2(10); Aylana *A3; A3 = new Aylana(5); return 0; } Destruktor
Destruktorclass Aylana{public:Aylana();Aylana(double r);~Aylana();};DestruktorMisolNatijaa1 Aylana uzunligi:31.4286a2 Aylana uzunligi:31.4159Misol#include #include #include using namespace std;class timer{clock_t start;public:timer(); // konstruktor~timer(); // destruktor};timer::timer() { start=clock(); } timer::~timer() { clock_t end=clock(); cout << "\nKetgan vaqt: " << (end-start) / CLOCKS_PER_SEC <<"\n"; } //CLOCKS_PER_SEC = 1000 int main() { timer ob; cout << "Biror tugmani bosing!"; getch(); } E’tiboringiz uchun rahmat Savollar ?Download 1.78 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling