有一家公司想要開發一個通知系統,用來通知顧客他們的最新資訊。他們通知顧客的管道有兩種:透過 Line 或透過 Email。
接下來,我們會給你一個叫做 Notification
的 abstract class
,你需要使用「繼承」(Inheritance) 的方式來實作兩個通知系統:LineNotification
跟 EmailNotification
。
當然,因為我們還沒學過怎麼接 API,所以我們現在會直接用 cout 來取代真正寄通知給顧客。
Notification
這個 abstract class 如下:
class Notification {
protected:
string recipient;
string message;
public:
Notification(const string& recipient, const string& message);
virtual void send() = 0; // abstract method (pure virtual)
virtual ~Notification();
};
你要實作的 class 有以下兩個:
LineNotification
Notification
abstract classlineId
(line 的 l 是小寫 l,Id 的 I 是大寫 I)void setLineId(const string &lineId)
:將 private 的 lineId 變數設為參數給定的 lineIdNotification
的 void send()
函式:輸出以下字串:"[Line] Sending message to <recipient>
(<lineId>
): <message>
",其中 <recipient>
, <lineId>
, <message>
要分別用 LineNotification
裡面的變數取代EmailNotification
Notification
abstract classemail
void setEmail(const string &email)
:將 private 的 email 變數設為參數給定的 emailNotification
的 void send()
函式:輸出以下字串:"[Email] Sending message to <recipient>
(<email>
): <message>
",其中 <recipient>
, <email>
, <message>
要分別用 EmailNotification
裡面的變數取代請複製以下程式碼,並且將這兩個 class 定義在指定的區塊。請注意,請勿修改任何已經寫好的程式碼,你只能額外定義這兩個 class。
#include <iostream> #include <vector> #include <string> #include "lib1048.h" /* TODO: WRITE YOUR CODE HERE */ // IMPORTANT: DO NOTE MODIFY ANY CODE BELOW! void sendNotification(Notification& notification) { notification.send(); } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { string type, recipient, message; cin >> type >> recipient; cin.ignore(); // discard whitespace getline(cin, message); if (type == "Email") { string email; cin >> email; EmailNotification emailNotification = EmailNotification(recipient, message); emailNotification.setEmail(email); sendNotification(emailNotification); } else if (type == "Line") { string lineId; cin >> lineId; LineNotification lineNotification = LineNotification(recipient, message); lineNotification.setLineId(lineId); sendNotification(lineNotification); } } }
Note: 你不需要處理任何輸入,只需要按照題目說明實作兩個 class
輸入格式:
第一行有一個正整數 $n$,代表有幾筆測資。
接下來 $n$ 筆測資,每筆測資有三行。
第一行有兩個用空白隔開的字串,分別代表 type, recipient
第二行代表 message
第三行代表有一個字串代表 lineId 或 email (根據 type 決定)
如果 type 是 Line,輸出 [Line] Sending message to <recipient>
(<lineId>
): <message>
如果 type 是 Email,輸出 [Email] Sending message to <recipient>
(<email>
): <message>
No. | Testdata Range | Score |
---|---|---|
1 | 0~4 | 100 |