?? fax.cpp
字號:
//這個程序在本書所帶軟盤中。文件名為FAX.CPP
//這個程序在基類中忽略了關鍵字virtual,使程序不再具有多態性。
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
class Fax {
public:
Fax(int); //構造函數用來對機器中的紙數進行初始化
void process(int); //關鍵字virtual被忽略,程序不再具有多態性
protected:
char phone_num[32];
int num_paper;
};
Fax::Fax(int paper)
{
num_paper = paper;
}
void Fax::process(int pages)
{
cout << "總紙數: " << pages << endl;
cout << "這是虛擬處理.." << endl;
}
class Fax_machine: public Fax{
public:
Fax_machine(char* phone, int paper) : Fax(paper)
{
strcpy(phone_num, phone);
}
void process(int);
};
void Fax_machine::process(int paper)
{
if (paper > num_paper)
{
cout << "不能完成傳真!" << endl;
cout << "紙張已用完。還需要至少加入 " << (paper-num_paper)
<< " 頁紙才可完成傳真。謝謝!" << endl;
exit(1);
}
else {
cout <<"完成傳真! " << endl;
cout << "傳真發至: " << phone_num << endl;
cout << "傳真發出頁數: " << paper << endl;
}
}
class Printer: public Fax{
public:
Printer(int paper) : Fax(paper) { };
void process(int);
};
void Printer::process(int paper)
{
if (paper > num_paper)
{
cout << "不能完成打印!" << endl;
cout << "紙張已用完! 請至少加入 " << (paper-num_paper)
<< " 頁紙才可完成打印。 謝謝!" << endl;
exit(1);
}
else {
cout << "完成打印! " << endl;
cout << "打印總頁數為: " << paper << endl;
}
}
void main(void)
{
Fax_machine fax("324-8890", 100);
Printer printer(30);
Fax* use;
use = &fax;
use->process(9);
cout << endl;
use = &printer;
use->process(40);
}
/*這個程序運行后將顯示如下輸出結果:
總紙數: 9
這是虛擬處理..
總紙數: 40
這是虛擬處理..
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -