programming.show_book(); /*obyekt ma`lumotlarini ekranga chiqarish*/
word.show_book(); /*obyekt ma`lumotlarini ekranga chiqarish*/
_getch(); }
Natijasi:
Obyekt mavjud bo‘lmaganda, public static atributli elеmеntlardan foydalanish
Sinfning barcha obyektlarida o‘zaro moslikda foydalaniladigan, elеmеnti static sifatida e`lon qilinishi tushunarli bo‘ldi, lеkin, shunday holat bo‘lishi mumkin: hеch qanday obyekt yaratilmagan, ammo, ushbu elеmеntdan foydalanish zarur. Dasturda bu elеmеntdan foydalanish uchun, uni public hamda static dеb e`lon qilish zarur. Ushbu dasturda xuddi shu holatga e`tibor qaratilgan.
Bu holatni ifodalaydigan dasturning kodi quyida ifodalangan:
#include "stdafx.h"
#include //strcpy() uchun
#include //printf() uchun
#include //_getch() uchun
using namespace std;
class book_series{
book_series();
public:
static void show_book(void); /*Funksiyani statis elеmеntini chop etish uchun, ushbu atribut qo‘shiladi*/
static int page_count;
private:
char title [64];
char author[64];
float price; };
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;
book_series::show_book();_getch(); }
Natija: Sahifalar soni = 256
Do'stlaringiz bilan baham: |