Ma'lumot elementlardan birgalikda foydalanish.
Agar bir sinf ob'ektlari umumiy ma'lumotlardan(statik elementlardan foydalanish zarur bo’lishi mumkin. Bu hollarda elementlarni static hizmatchi so’zi yordamida ta'riflash lozimdir:
private:
static int shared_value;
Sinfni ta'riflagandan so’ng elementni sinf tashqarisida global o’zgaruvchi sifatida ta'riflashingiz lozim:
int class_name::shared_value;
Kuyidagi SHARE_IT.CPP dasturida book_series, sinfi aniqlangan bo’lib, bu sinfga tegishli ob'ektlar uchun bir hil bo’lgan page_count, elementidan foydalanilgandir. Agar dasturda bu element qiymati o’zgartirilsa hamma ob'ektlarda ko’rinadi:
#include
#include
class book_series
{
public:
book_series(char *, char *, float);
void show_book(void);
void set_pages(int) ;
private:
static int page_count;
char title[64];
char author[ 64 ];
float price;
};
int book_series::page__count;
void book_series::set_pages(int pages)
{
page_count = pages;
}
book_series::book_series(char *title, char *author, float price)
{
strcpy(book_series::title, title);
strcpy(book_series::author, author);
book_series::price = price;
}
void book_series:: show_book (void)
{
cout << "Zagolovok: " << title << endl;
cout << "Avtor: " << author << endl;
cout << "Tsena: " << price << endl;
cout << "Stranitsih: " << page_count << endl;
}
void main(void)
{
book_series programming( "Uchimsya programmirovat' na C++", "Jamsa", 22.95);
book_series word( "Uchimsya rabotat' s Word dlya Windows", "Wyatt", 19.95);
word.set_pages(256);
programming.show_book ();
word.show_book() ;
cout << endl << "Izmenenie page_count " << endl;
programming.set_pages(512);
programming.show_book();
word.show_book();
}
Bu dasturda sinf page_count elementini static int. shaklda ta'riflaydi. Sinf ta'rifidan so’ng page_count elementi global uzgaruvchi sifatida ta'riflanadi. Dastur page_count, elementi qiymatini o’zgartirganda, bu o’zgarish book_series sinfining hamma ob'ektlarida ko’rinadi.
Ob'ektlar mavjud bo’lmaganda public static tipidagi elementlardan foydalanish Statik elementlardan ob'ekt hali yaratilmasdan foydalanish mumkindir. Buning uchun bu elementni public va static sifatida ta'riflash lozimdir. Quyidagi misolda USE_MBR.CPP dasturi book_series sinfiga tegishli page_count elementini thu ob'ektga tegishli ob'ektlar mavjud bo’lmasa ham ishlatadi:
#include
#include
class book_series
{
public:
static int page_count;
private:
char title [64];
char author[64];
float price;
};
int book_series::page_count;
void main(void)
{
book_series::page_count = 256;
cout << "Tekuthee znachenie page_count ravno " << book_series::page_count << endl;
}
Bu misolda page_count elementi public sifatida ta'riflangani uchun, book_series sinfi ob'ektlari mavjud bo’lmagan holda ham dastur bu elemenetga murojaat qilishi mumkin.
Statik funktsiya elementlardan foydalanish Dasturda statik elementlardan tashqari statik funktsiyaldaridan ham foydalanish mumkindir. Bu holda shu sinfga tegishli ob'ekt yaratilmasa ham dastur statik funktsiyaga murojaat qilishi mumkindir. Quyida menu sinfi ta'riflangan bo’lib ANSI drayvera esc- ketma ketligi yordamida ekranni tozalash uchun ishlatadi. Buts sinfning clear_screen usuli statik usul sifatida ta'riflangani uchun menu tipidagi ob'ektlar mavjud bo’lmasa ham bu usuldan foydalanish mumkindir. Quyidagi
CLR_SCR.CPP dasturda clear_screen usulidan foydalanish ko’rsatilgan:
#include
class menu
{
public:
static void clear_screen(void);
// Zdes' doljnih biht' drugie metodih
private:
int number_of_menu_options;
};
void menu::clear_screen(void)
{
cout << '\033' << "[2J";
}
void main(void)
{
menu::clear_screen();
}
Dasturda clear_screen elementi statik , element sifatida ta'riflangani uchun, menu tipidagi ob'ektlar mavjud bo’lmasa ham bu funktsiyadan ekranni tozalash uchun foydalanish mumkindir.
Dostları ilə paylaş: |