Integrating Complex Features in C++ While Preserving Backward Compatibility

Introduction

Welcome to today's lesson, where we will tackle a common challenge in software engineering: incorporating complex features while maintaining backward compatibility. We'll use a Potluck Dinner organization system as our scenario, embarking on an intriguing journey of C++ programming, step-by-step analysis, and strategic thought. Ready to dive in? Let's commence our journey!

Starter Task Review

Initially, our Potluck Dinner organization system allows us to add and remove participants and manage their respective dishes for each round. There are three critical methods:

  • bool add_participant(const std::string& member_id): This method adds a participant. If a participant with the given member_id already exists, it won’t create a new one but will return false. Otherwise, it will add the member and return true.
  • bool remove_participant(const std::string& member_id): This method removes a participant with the given member_id. If the participant exists, the system will remove them and return true. Otherwise, it will return false. When removing a participant, you need to remove their dish if they brought one.
  • bool add_dish(const std::string& member_id, const std::string& dish_name): This method enables each participant to add their dishes for every round. If a participant has already added a dish for this round OR if the member_id isn't valid, the method will return false. Otherwise, it will add the dish for the respective participant's round and return true.

Let's write our C++ code, which implements the functions as per our initial state:

#include <unordered_set>
#include <unordered_map>
#include <string>

class Potluck {
private:
    std::unordered_set<std::string> participants_;
    std::unordered_map<std::string, std::string> dishes_;

public:
    bool add_participant(const std::string& member_id) {
        if (participants_.find(member_id) != participants_.end()) {
            return false;
        } else {
            participants_.insert(member_id);
            return true;
        }
    }

    bool remove_participant(const std::string& member_id) {
        if (participants_.find(member_id) == participants_.end()) {
            return false;
        } else {
            participants_.erase(member_id);
            if (dishes_.find(member_id) != dishes_.end()) {
                dishes_.erase(member_id);
            }
            return true;
        }
    }

    bool add_dish(const std::string& member_id, const std::string& dish_name) {
        if (participants_.find(member_id) == participants_.end() || dishes_.find(member_id) != dishes_.end()) {
            return false;
        } else {
            dishes_[member_id] = dish_name;
            return true;
        }
    }
};

In this code, we utilized std::unordered_set to store unique participant IDs and std::unordered_map to store the participant's ID and their respective dish name. With this foundation laid, let's introduce some advanced functionalities.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal