Messages Order

Sort by

recency

|

94 Discussions

|

  • + 0 comments

    Here is Messages Order solution in c++ - https://programmingoneonone.com/hackerrank-messages-order-solution-in-cpp.html

  • + 0 comments

    Message's id assignment should be handled by the factory.

    class Message {
    public: 
        Message() {}
        Message(string text, int id):msg_body(text), msg_id(id) {}
        const string& get_text() {
            return msg_body;
        }
        bool operator<(const Message& other) const {
            return this->msg_id < other.msg_id;
        }
        
    private:
        string msg_body;
        int msg_id;
    };
    
    class MessageFactory {
    public:
        MessageFactory() {}
        Message create_message(const string& text) {
            msg_counter += 1;
            return Message(text, msg_counter);
        }
    private:
        int msg_counter = -1;
    };
    
  • + 0 comments
    class Message {
    private:
        string text;
        static int id;
        int current_id;
    public:
        Message() { current_id = ++id; }
        Message(string t){ current_id = ++id; text=t; }
        const string& get_text() {
            return text;
        }
        bool operator < (const Message& M2) {
            if(current_id < M2.current_id)
                return true;
            else
                return false;
        }
    };
    int Message::id = 0;
    class MessageFactory {
    public:
        MessageFactory() {}
        Message create_message(const string& text) {
            Message m = Message(text);
            return m;
        }
    };
    
  • + 1 comment

    The key is the static variable.

  • + 0 comments
    class Message {
    public: 
        Message() {}
        
        Message (const string& m) : m_Id(s_Id++), m_Message(m) {}
        
        const string& get_text() {
            return m_Message;
        }
        
        bool operator<(const Message& toCompare)
        {
            return m_Id < toCompare.m_Id;
        }
        
        static int s_Id;
        private:
        int m_Id;
        string m_Message;
        
    };
    
    int Message::s_Id = 0;
    
    class MessageFactory {
    public:
        MessageFactory() {}
        Message create_message(const string& text) {
            return Message(text);
        }
    };``