Hello, help please, I am stumped. I need to create a custom class String inherited from the parent in which the at() function is implemented and the operator[ ] operator is overloaded.

In the String class, I overload several operators for my lines: +,>, <, ==,! =

My goal is to implement a default constructor that would help me write a character array and then output it. My problem is exactly this, help push on the idea how to do it ...

Parent class:

 class TCharArray { protected: static const size_t LIMIT = 100; char str[LIMIT]; public: TCharArray(); ~TCharArray(); char & at(size_t n) { if (n >= LIMIT) throw std::out_of_range("Invalid index"); return str[n]; } char& operator[ ](size_t n) { if (n < 0 || n >= LIMIT) { std::cout << "Invalid index" << std::endl; exit(1); } return str[n]; } }; 

String class:

 #include "TCharArray.h" #include <iostream> #include <string.h> typedef unsigned un ; class String : public TCharArray { public: /*String() { strcpy_s(str, ""); }*/ String() //Именно в этом конструкторе мне нужно сделать реализацию { char s[LIMIT]; try { //вылавливаю исключения, если нет то.. std::cout << "Enter smth: " << std::endl;//прошу ввести строку std::cin >> s[LIMIT];//ввожу for (un i = 0; i < LIMIT; i++) { at(i); } } catch (std::out_of_range const& exc) { std::cout << exc.what() << '\n'; } for (un j = 0; j < LIMIT; j++) {//вывожу std::cout << at(j)<<std::endl; } } String::~String() { std::cout << "Exit" << std::endl; } bool operator==(String ss)const { return (strcmp(str, ss.str) == 0) ? true : false; } String operator+(String ss) const { String temp; if (strlen(str) + strlen(ss.str) < LIMIT) { strcpy_s(temp.str, str); strcat_s(temp.str, ss.str); } else { std::cout << "over!" << std::endl; exit(1); } return temp; } friend bool operator!= (const String &a,const String &b ) { return !(a == b); } friend bool operator>(const String& a, const String& b) { return (strlen(a.str) > strlen(b.str)) ? true : false; } friend bool operator<(const String& a, const String& b) { return (strlen(a.str) < strlen(b.str)) ? true : false; } }; 
  • Offtopic: do not forget classes with such common names to hide in any of your namespace. - vp_arth
  • @vp_arth Understood you while in development, so I’m not hiding - Nikita Gusev
  • What exactly does not work for you in the current implementation? Apparently it should be at (i) = s [i]; - Unick
  • @Unick at(i)=s[i]; I change in the following for s [j]. The entered word is recorded as needed, but now the error is that, after my written word, it displays incomprehensible characters to the console, I suspect that there are about 100 of them. That is, My LIMIT not properly attracted. I strive for the fact that I will record 7 records, and ask 7 times to enter a record and display it. It seems to me that the 2nd for should be up to 8, and my LIMIT and try watch overflow, understand correctly? - Nikita Gusev
  • @ nikita-gusev Yes, you have to remove garbage, apparently your cycles should not be before LIMIT, but before strlen (s). - Unick

0