#include
class phone
{
public:
void dial(char *number) { cout << "Nabor nomera " << number << endl; }
void answer(void) { cout << "Ojidanie otveta" << endl; }
void hangup(void) { cout << "Zvonok vihpolnen - povesit' trubku" << endl; }
void ring(void) { cout << "Zvonok, zvonok, zvonok" << endl; }
phone(char *number) { strcpy(phone::number, number); };
private:
char number[13];
};
void main(void)
{
phone telephone("555-1212");
telephone.dial("212-555-1212");
}
Agar tugmali va diskli telefon yaratish lozim bo’lsa va qo’ng’iroq qilish uchun 25 tsent to’lash lozim bo’lsa vorislik yordamida touch_tone va pay_phone schinflarini yaratish mumkin:
class touch_tone : phone
{
public:
void dial(char * number) { cout << "Pik pik Nabor nomera " << number << endl; }
touch_tone(char *number) : phone(number) { }
};
class pay_phone : phone
{
public:
void dial(char * number)
{
cout << "Pojaluysta, oplatite " << amount << " tsentov" << endl;
cout << "Nabor nomera " << number << endl;
}
pay_phone(char *number, int amount) : phone(number) { pay_phone::amount = amount; }
private:
int amount;
};
Yangi touch_tone va pay__phone sinflari hususiy dial usulidan foydalanadi. Qo’yidagi NEWPHONE.CPP dasturida shu sinflardan foydalanilgan
#include
#include
class phone
{
public:
void dial(char *number) { cout << "Nabor nomera " << number << endl; }
void answer(void) { cout << "Ojidanie otveta" << endl; }
void hangup(void) { cout << "Zvonok vihpolnen - povesit' trubku" << endl; }
void ring(void) { cout << "Zvonok, zvonok, zvonok" << endl; }
phone(char *number) { strcpy(phone::number, number); };
protected:
char number[13];
);
class touch_tone : phone
{
public:
void dial(char *number) { cout << "Pik pik Nabor nomera " << number << endl; }
touch_tone(char *number) : phone(number) { }
};
class pay_phone : phone
{
public:
void dial(char * number) { cout << "Pojaluysta, oplatite " << amount << " tsentov" << endl; cout << "Nabor nomera " << number << endl; }
pay_phone(char * number, int amount) : phone(number) { pay_phone::amount = amount; }
private:
int amount ;
};
void main (void)
{
phone rotary("303-555-1212");
rotary.dial("602-555-1212");
touch_tone telephone("555-1212");
telephone.dial("212-555-1212");
pay_phone city_phone("555-1111", 25);
city_phone.dial("212-555-1212");
}
Dastur bajarilganda ekranga qo’yidagi ma'lumotlar chiqadi:
S:\> NEWPHONE
Nabor nomera 602-555-1212
Pik pik Nabor nomera 212-555-1212
Pojaluysta, oplatite 25 tsentov
Nabor nomera 212-555-1212
Bu misolda polimorf ob'ektlarpdan foydalanilmagan.
Do'stlaringiz bilan baham: