// Program to inherit a class from String and implement some restrictions. // Assignment-26 // pstrclsiht.cpp, Author - Krishna Kumar Khatri. // Uncomment ONLY the following line if you are using Turbo/Borland compiler. // #define TC #ifdef TC #include #include #else #include #include using namespace std; #endif #include // For getch. const int MAXLEN = 25; class String { protected: char str[MAXLEN]; }; class Pstring : public String { public: Pstring(const char[]); friend ostream& operator<<(ostream&, Pstring&); }; Pstring::Pstring(const char s[]) { int i; if (strlen(s) > MAXLEN - 1) { cout << endl << "Buffer overflow!"; for (i = 0; i <= MAXLEN; i++) str[i] = s[i]; str[i] = '\0'; } else strcpy(str, s); } ostream& operator<<(ostream& os, Pstring& ps) { os << ps.str; return os; } int main() { #ifdef TC clrscr(); #endif cout << "MAXLEN = " << MAXLEN << endl << endl; cout << "The following string's length is within the limit."; Pstring str1("This is going."); cout << endl << str1; cout << endl << endl << "The following string's length is not within the limit."; Pstring str2("This is going to wrench me like a twister."); cout << endl << str2; cout << endl << endl << "Program over. Press any key to exit..."; getch(); return 0; // Successful termination. }