✅ Перевірена відповідь на це питання доступна нижче. Наші рішення, перевірені спільнотою, допомагають краще зрозуміти матеріал.
You will refactor the design from Part (a) so that the formatting behaviour is implemented usingkind string.
Goal: No if/else/switch may be used to select the message-type behaviour.
Required output format rules:
EMAIL to <user>: <text>
SMS to <user>: <text> (truncate text to 20 characters)
PUSH to <user>: <optional URGENT><text>(if priority ≥ 4, add
URGENT before the text)Task: Complete the code template below by writing the missing class hierarchy. You must only fill the TODO sections.
#include <iostream>
#include <string>
#include <memory>
#include <array>
using namespace std;
// =====================
// TODO 1: Base class
// =====================
// Create an abstract class named MessageBase with:
// - a virtual destructor
// - virtual string format() const = 0;
// =====================
// TODO 2: Derived classes
// =====================
// Implement these derived classes (each must override format()):
// - EmailMessage
// - SMSMessage
// - PushMessage
//
// Each derived class should store the data it needs (user, text, priority if needed).
// Do NOT store a "kind" field in the polymorphic design.
int main() {
// Do not change main()
array<unique_ptr<MessageBase>, 3> msgs = {
make_unique<EmailMessage>("u100", "Welcome to the system", 2),
make_unique<SMSMessage>("u101", "Your verification code is 123456", 3),
make_unique<PushMessage>("u102", "Server maintenance tonight", 5)
};
for (const auto& m : msgs) {
cout << m->format() << "\n";
}
}
Expected output:
EMAIL to u100: Welcome to the system
SMS to u101: Your verification code
PUSH to u102: URGENT Server maintenance tonight
Constraints (must follow):
if/else/switch) is allowed to select the message type behavior.
dynamic_cast and no typeid.
array<unique_ptr<MessageBase>> to demonstrate polymorphism.