#include <iostream>
#include <vector>
#include <memory>
Using namespace std;
Class Subscription {
Protected:
String name;
Double price;
Public:
Subscription(string n, double p) : name(move(n)), price(p) {}
Virtual void display() const {
Cout << “Subscription: “ << name << “ | Price: $” << price << endl;
Virtual ~Subscription() = default;
};
Class MonthlySubscription : public Subscription {
Int duration;
Public:
MonthlySubscription(string n, double p, int d) : Subscription(move(n), p), duration(d) {}
Void display() const override {
Cout << “Monthly Subscription: “ << name << “ | Price: $” << price
<< “ per month | Duration: “ << duration << “ months” << endl;
}
};
Class YearlySubscription : public Subscription {
Double discount;
Public:
YearlySubscription(string n, double p, double d) : Subscription(move(n), p), discount(d) {}
Void display() const override {
Cout << “Yearly Subscription: “ << name << “ | Price: $” << price
<< “ per year | Discount: “ << discount << “%” << endl;
Friend void comparePrices(const YearlySubscription &y1, const YearlySubscription &y2);
};
Void comparePrices(const YearlySubscription &y1, const YearlySubscription &y2) {
Cout << “Comparing “ << y1.name << “ and “ << y2.name << “…” << endl;
If (y1.price < y2.price)
Cout << y1.name << “ is cheaper than “ << y2.name << endl;
Else if (y1.price > y2.price)
Cout << y2.name << “ is cheaper than “ << y1.name << endl;
Else
Cout << “Both subscriptions have the same price.” << endl;
}
Void manageSubscriptions() {
Vector<unique_ptr<Subscription>> subscriptions;
Int choice;
Do {
Cout << “\nSubscription Management System” << endl;
Cout << “1. Add Monthly Subscription” << endl;
Cout << “2. Add Yearly Subscription” << endl;
Cout << “3. Display All Subscriptions” << endl;
Cout << “4. Compare Two Yearly Subscriptions” << endl;
Cout << “5. Exit” << endl;
Cout << “Enter your choice: “;
Cin >> choice;
Switch (choice) {
Case 1: {
String name;
Double price;
Int duration;
Cout << “Enter Name, Price, Duration (months): “;
Cin >> name >> price >> duration;
If (price <= 0 || duration <= 0) {
Cout << “Invalid input! Please enter positive values.” << endl;
Break;
}
Subscriptions.push_back(make_unique<MonthlySubscription>(name, price,
duration));
Break;
Case 2: {
String name;
Double price, discount;
Cout << “Enter Name, Price, Discount (%): “;
Cin >> name >> price >> discount;
If (price <= 0 || discount < 0) {
Cout << “Invalid input! Please enter positive values.” << endl;
Break;
Subscriptions.push_back(make_unique<YearlySubscription>(name, price,
discount));
Break;
Case 3: {
If (subscriptions.empty()) {
Cout << “No subscriptions available!” << endl;
Break;
Int index = 1; // Start index from 1
For (const auto &sub : subscriptions) {
Cout << “[“ << index++ << “] “;
Sub->display();
}
Break;
Case 4: {
Int I, j;
Cout << “Enter indices of two yearly subscriptions to compare: “;
Cin >> I >> j;
If (I < 1 || j < 1 || I > subscriptions.size() || j > subscriptions.size() || I == j) {
Cout << “Invalid indices!” << endl;
Break;
Auto *y1 = dynamic_cast<YearlySubscription *>(subscriptions[I – 1].get());
Auto *y2 = dynamic_cast<YearlySubscription *>(subscriptions[j – 1].get());
If (y1 && y2)
comparePrices(*y1, *y2);
else
cout << “Invalid selections! Ensure you are selecting Yearly Subscriptions.” << endl;
break;
Case 5:
Cout << “Exiting…” << endl;
Break;
Default:
Cout << “Invalid choice!” << endl;
} while (choice != 5);
}
Int main() {
manageSubscriptions();
return 0;