Inherited Code Discussions | C++ | HackerRank

Inherited Code

Sort by

recency

|

223 Discussions

|

  • + 0 comments

    class BadLengthException{ public: int n; BadLengthException(int e){ n=e; } int what() { return n; } };

  • + 0 comments

    If showing error in integer or const something stuffs, just remove the 'const' keyword after as : class BadLengthException : public exception { private: int exceptionname; public: explicit BadLengthException(const int &exceptionexample) { exceptionname = exceptionexample;}

    int what()    // removing 'const' after 'what()' works , not any other
    {       
        return exceptionname;
    }
    
  • + 0 comments

    I cannot see the proper solution where we inherit all methods. So, here it is:

    class BadLengthException : public exception {
    private:
        string message;
    public:
        BadLengthException(const int& msg) : message(to_string(msg)) {}
        const char* what() const noexcept override{
            return message.c_str();
        }
    };
    

    To be syntactically coherent with error messages, we need to return the error as a C-style string, not an integer.

  • + 0 comments

    what defines if something is invalid???

  • + 0 comments

    try c++14 class BadLengthException : public exception{ private: int length; public: BadLengthException(int n){ length=n; } int what(){ return length; } };