typeid() funksiyasidan foydalanish Ob'ektning o'lchamini qanday topish mumkin, agar bizda bor narsa unga ko'rsatgich bo'lsa? Bunga bitta javob typeid() funksiyasi keltirilgan. Bu funksiyadan sinfini topish uchun foydalanishimiz mumkin ob'ektni yarating va bu sinf nomini sizeof() da foydalaning. typeid() dan foydalanish uchun a ni yoqishingiz kerak bo'lishi mumkin
Run-Time Type Information (RTTI) deb nomlangan kompilyator opsiyasi. (Hozirda shunday Microsoft kompilyatori, C ilovasida tavsiflanganidek, “Microsoft Visual C++.”)
Bizning keyingi misolimiz bu qanday ishlashini ko'rsatadi. Ob'ektning o'lchamini bilganimizdan so'ng, biz undan foydalanishimiz mumkin
ob'ektni diskka yozish uchun write() funksiyasi.
Biz EMPLOY dasturiga oddiy foydalanuvchi interfeysini qo‘shdik va uni a’zolarga xos qilib qo‘ydik
virtual funktsiyalari mavjud, shuning uchun biz ob'ektlarga ko'rsatgichlar majmuasidan foydalanishimiz mumkin. Biz ham ba'zilarini kiritdik
oxirgi bo'limda muhokama qilingan xatolarni aniqlash usullari.
Bu juda ambitsiyali dastur, lekin u bo'lishi mumkin bo'lgan ko'plab texnikalarni namoyish etadi
to'liq miqyosli ma'lumotlar bazasi ilovasida qo'llaniladi. Bundan tashqari, OOP ning haqiqiy kuchini ko'rsatadi. Boshqa qanday qilib
faylga turli o'lchamdagi ob'ektlarni yozish uchun bitta bayonotdan foydalanasizmi? Bu erda ro'yxat
EMPL_IO:
// empl_io.cpp
// performs file I/O on employee objects
// handles different sized objects
#include // fayl oqimi funktsiyalari uchun
#include #include // typeid() uchun
using namespace std;
#include
// chiqish uchun()
const int LEN = 32; // familiyalarning maksimal uzunligi
const int MAXEM = 100; // xodimlarning maksimal soni
enum employee_type {tmanager, tscientist, tlaborer};
class employee // xodimlar sinfi
{
private:
char name[LEN]; // xodim nomi
unsigned long number; // xodim raqami
static int n; // xodimlarning joriy soni
static employee* arrap[]; // ptrs dan empsga massiv
public:
virtual void getdata()
{
cin.ignore(10, ‘\n’);
cout << “ Enter last name: “; cin >> name;
cout << “ Enter number: “; cin >> number;
}
virtual void putdata()
{
cout << “\n Name: “ << name;
cout << “\n Number: “ << number;
}
virtual employee_type get_type(); // turini oling
static void add(); // xodim qo'shing
static void display(); // barcha xodimlarni ko'rsatish
static void read(); // disk faylidan o'qing
static void write(); // disk fayliga yozish
};
// statik o'zgaruvchilar
int employee::n; // xodimlarning joriy soni
employee* employee::arrap[MAXEM]; // ptrs dan empsga massiv
// menejer sinfi
class manager : public employee
{
private:
char title[LEN]; //” vitse-prezidenti” va boshqalar.
double dues; // golf klubi to'lovlari
public:
void getdata()
{
employee::getdata();
cout << “ Enter title: “; cin >> title;
cout << “ Enter golf club dues: “; cin >> dues;
}
void putdata()
{
employee::putdata();
cout << “\n Title: “ << title;
cout << “\n Golf club dues: “ << dues;
}
};
// olimlar sinfi
class scientist : public employee
{
private:
int pubs; // nashrlar soni
public:
void getdata()
{
employee::getdata();
cout << “ Enter number of pubs: “; cin >> pubs;
}
void putdata()
{
employee::putdata();
cout << “\n Number of publications: “ << pubs;
}
};
// ishchilar sinfi
class laborer : public employee
{
};
// Xotiradagi ro'yxatga xodim qo'shing
void employee::add()
{
char ch;
cout << “‘m’ to add a manager”
“\n’s’ to add a scientist”
“\n’l’ to add a laborer”
“\nEnter selection: “;
cin >> ch;
switch(ch)
{ // belgilangan xodim turini yaratish
case ‘m’: arrap[n] = new manager; break;
case ‘s’: arrap[n] = new scientist; break;
case ‘l’: arrap[n] = new laborer; break;
default: cout << “\nUnknown employee type\n”; return;
}
arrap[n++]->getdata(); // foydalanuvchidan xodimlar ma'lumotlarini olish
}
// barcha xodimlarni ko'rsatish
void employee::display()
{
for(int j=0; j{
cout << (j+1); // ko'rsatish raqami
switch( arrap[j]->get_type() ) // ko'rsatish turi
{
case tmanager: cout << “. Type: Manager”; break;
case tscientist: cout << “. Type: Scientist”; break;
case tlaborer: cout << “. Type: Laborer”; break;
default: cout << “. Unknown type”;
}
arrap[j]->putdata(); // xodimlar ma'lumotlarini ko'rsatish
cout << endl;
}
}
// ushbu ob'ektning turini qaytaring
employee_type employee::get_type()
{
if( typeid(*this) == typeid(manager) )
return tmanager;
else if( typeid(*this)==typeid(scientist) )
return tscientist;
else if( typeid(*this)==typeid(laborer) )
return tlaborer;
else
{ cerr << “\nBad employee type”; exit(1); }
return tmanager;
}
// barcha joriy xotira obyektlarini faylga yozish
void employee::write()
{
int size;
cout << “Writing “ << n << “ employees.\n”;
ofstream ouf; //open ofstream in binary
employee_type etype; // har bir xodim ob'ektining turi
ouf.open(“EMPLOY.DAT”, ios::trunc | ios::binary);
if(!ouf)
{ cout << “\nCan’t open file\n”; return; }
for(int j=0; j{
// uning turini oling
etype = arrap[j]->get_type();
// faylga yozing
ouf.write( (char*)&etype, sizeof(etype) );
switch(etype) //find its size
{
case tmanager: size=sizeof(manager); break;
case tscientist: size=sizeof(scientist); break;
case tlaborer: size=sizeof(laborer); break;
} // faylga xodim ob'ektini yozish
ouf.write( (char*)(arrap[j]), size );
if(!ouf)
{ cout << “\nCan’t write to file\n”; return; }
}
}
// barcha xodimlar uchun ma'lumotlarni fayldan xotiraga o'qish
void employee::read()
{
int size; // xodim ob'ektining o'lchami
employee_type etype; // xodimning turi
ifstream inf; // ifstreamni ikkilik rejimda oching
inf.open(“EMPLOY.DAT”, ios::binary);
if(!inf)
{ cout << “\nCan’t open file\n”; return; }
n = 0; // Xotirada hali xodimlar yo'q
while(true)
{
// keyingi xodimning turini o'qing
inf.read( (char*)&etype, sizeof(etype) );
if( inf.eof() ) // eof-dagi tsikldan chiqing
break;
if(!inf) // xato o'qish turi
{
cout << “\nCan’t read type from file\n”; return; }
switch(etype)
{
// yangi xodim yaratish
case tmanager: // to'g'ri turdagi
arrap[n] = new manager;
size=sizeof(manager);
break;
case tscientist:
arrap[n] = new scientist;
size=sizeof(scientist);
break;
case tlaborer:
arrap[n] = new laborer;
size=sizeof(laborer);
break;
default: cout << “\nUnknown type in file\n”; return;
}
// fayldagi ma'lumotlarni o'qing
inf.read( (char*)arrap[n], size );
if(!inf)
// xato, lekin eof emas
{ cout << “\nCan’t read data from file\n”; return; }
n++; // xodimni hisoblash}
// vaqt tugaydi
cout << “Reading “ << n << “ employees\n”;
}
int main()
{
char ch;
while(true)
{
cout << “‘a’ -- add data for an employee”
“\n’d’ -- display data for all employees”
“\n’w’ -- write all employee data to file”
“\n’r’ -- read all employee data from file”
“\n’x’ -- exit”
“\nEnter selection: “;
cin >> ch;
switch(ch)
{
case ‘a’: // ro'yxatga xodim qo'shing
employee::add(); break;
case ‘d’: // barcha xodimlarni ko'rsatish
employee::display(); break;
case ‘w’: // barcha xodimlarni ko'rsatish
employee::write(); break;
case ‘r’: // barcha xodimlarni fayldan o'qing
employee::read(); break;
case ‘x’: exit(0); // dasturdan chiqish
default: cout << “\nUnknown command”;
}
// tugatish tugmasi
}
// vaqt tugaydireturn 0;
} // asosiy yakun