Klasslardi qollaw. Dostana ha’m virtual funkciyalar Qoyilg’an ma’sele


Download 18.5 Kb.
bet3/4
Sana23.12.2022
Hajmi18.5 Kb.
#1049037
1   2   3   4
Bog'liq
2-lekciya

Klass doslari
C++ qandayda bir klass doslarina usi klasstin’ menshik elementlerine mu’rajat qiliwg’a imkan beredi. Misal ushin to’mendegi keltirilgen book klasi librarian klasin o’zinin’ dostisi dep ja’riyalaydi. Sonin’ ushin librarian klasi obiektleri tuwridan tuwri book, klasinin’ menshik elementlerine mu’rajat mu’mkin:
class book
{
public:
book (char *, char *, char *);
void show_book(void);
friend librarian;
private:
char title [64] ;
char author[64];
char catalog[64];
};
To’mendegi VIEWBOOK.CPP dastu’rinde librarian klasi book klasin o’zinin’ dosti deb jariyalaydi. Dastu’rde librarian klasinin’ change_catalog
Funkciyasinan paydalanilg’an:
#include
#include
class book
{
public:
book (char *, char *, char *);
void show_book(void);
friend librarian;
private:
char title[64] ;
char author[64];
char catalog[64];
};
book::book(char *title, char *author, char •catalog)
{
strcpy(book::title, title);
strcpy(book::author, author) ;
strcpy(book::catalog, catalog);
}
void book::show_book(void)
{
cout << "Nazvanie: " << title << endl;
cout << "Avtor: " << author << endl;
cout << "Katalog: " << catalog << endl;
}
class librarian
{
public:
void change_catalog(book *, char *);
char *get_catalog(book);
};
void librarian::change_catalog(book *this_book, char *new_catalog)
{
strcpy(this_book->catalog, new_catalog);
}
char *librarian: :get__catalog(book this_book)
{
static char catalog[64];
strcpy(catalog, this_book.catalog);
return(catalog) ;
}
void main(void)
{
book programming( "Uchimsya programmirovat' na yazihke C++", "Jamsa", "P101");
librarian library;
programming.show_book();
library.change_catalog(&programming, "Legkiy C++ 101");
programming.show_book();
}

Download 18.5 Kb.

Do'stlaringiz bilan baham:
1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling