Inherited Code in C++ hackerrank solution

Problem statement:


You are given a partially implemented class BadLengthException and a main function.

Your task is to write the class implementation so that it throws an exception when the length of a string is less than 5.

The main function takes an integer T as input and iterates T times.

In each iteration, it reads a string and calls the calculateLength() function to calculate the length of the string.

If the length is less than 5, it should throw a BadLengthException and print "Too short: " followed by the length of the string. Otherwise, it should print the length of the string.

      

Sample Input:

The input consists of an integer T on the first line, which represents the number of test cases. This is followed by T lines, each containing a string.
4 abcd xyz abcdxyz abcdef
      

Sample Output:

For each test case, print either "Too short: " followed by the length of the string (if the length is less than 5) or the length of the string itself.
Too short: 4 Too short: 3 Too short: 7 6

Explanation:

In this example, we have four test cases:

The length of "abcd" is less than 5, so it prints "Too short: 4".

The length of "xyz" is less than 5, so it prints "Too short: 3".

The length of "abcdxyz" is 7, so it prints "Too short: 7".

The length of "abcdef" is 6, so it prints the length itself, which is 6.

      
  #include <iostream>
  #include <string>
  #include <sstream>
  #include <exception>
  
  using namespace std;
  
  class BadLengthException : public exception {
  private:
      int length;
  
  public:
      BadLengthException(int len) : length(len) {}
  
      const char* what() const noexcept override {
          stringstream ss;
          ss << "Too short: " << length;
          return ss.str().c_str();
      }
  };
  
  int calculateLength(string s) {
      if (s.length() < 5) {
          throw BadLengthException(s.length());
      }
      return s.length();
  }
  
  int main() {
      int T;
      cin >> T;
  
      while (T--) {
          string s;
          cin >> s;
  
          try {
              int length = calculateLength(s);
              cout << length << endl;
          } catch (BadLengthException& e) {
              cout << e.what() << endl;
          }
      }
  
      return 0;
  }
  

Code Explanation


We start by including the necessary header files, including for input/output operations, for string manipulation, for creating the exception message, and for defining and handling exceptions.

The BadLengthException class is defined as a derived class of the exception class. It has a private data member length to store the length of the string that caused the exception.

The class provides a constructor that takes the length as an argument and initializes the length data member.

The what() function is overridden to provide a custom exception message using a stringstream. It appends "Too short: " followed by the length of the string and returns the formatted message as a C-style string.

The calculateLength() function takes a string as input and calculates its length. If the length is less than 5, it throws a BadLengthException with the length as the argument.

In the main() function, we read the number of test cases (T) using cin.

We then iterate T times and in each iteration, read a string (s) using cin.

Inside a try-catch block, we call the calculateLength() function and store the length in a variable.

If an exception of type BadLengthException is thrown, the catch block is executed, and the exception message is printed using e.what().

If no exception is thrown, the length of the string is printed.

Finally, we return 0 to indicate successful execution.

The solution demonstrates the usage of exception handling in C++, specifically throwing and catching custom exceptions. It also handles input based on the given format and provides the expected output.

I hope this solution meets your requirements and provides a clear understanding of the problem and its solution!