ТА_Методички / Lec2_10
.pdfДаний приклад пояснює принцип передачі значень параметрів з допомогою конструктора.
#include <iostream.h> #include <string.h> #include <conio.h>
// Global'ni zminni const int IN = 1;
const int CHECKED_OUT = 0;
// Opys clasu class book
{
char author[40]; char title[40]; int status;
public:
//Zadannya (prototyp) constructora book book (char *n, char *t, int s);
//Zadannya i opys funkciji status
int get_status() {return status;} void set_status(int s) {status = s;}
31/33
// Zadannya (prototyp) funkciji pokazaty (vyvesty) void show();
};
// Opys konstructora
book::book(char *n, char *t, int s)
{
strcpy(author, n); strcpy(title, t); status = s;
}
// Opys funkciji pokazaty (vyvesty) void book::show()
{
cout << author << ". " << title << " "; cout << " - znahodyt'sya ";
if(status==IN) cout << "u biblioteci. \n"; else cout << "u chytacha. \n";
}
int main ()
{
clrscr ();
32/33
//Oholoshennya dvoh zminnyh klasu book b1 i b2
//z zadannyam znachen za dopomogoyu konstructora book b1("M. Tven", "Tom Soyer", IN);
book b2("A. Melvill","Mobi Dick", CHECKED_OUT); b1.show(); cout << "\n";
b2.show(); getch (); return 0;
}
Результат
M. Tven. Tom Soyer - znahodyt'sya u biblioteci.
A. Melvill. Mobi Dick - znahodyt'sya u chytacha.
33/33
