Thursday 17 August 2017

Recall the STRCONV example from Chapter 8. The String class in this example has a flaw: It does not protect itself if its objects are initialized to have too many characters. (The SZ constant has the value 80.) For example, the definition String s = “This string will surely exceed the width of the “ “screen, which is what the SZ constant represents.”; will cause the str array in s to overflow, with unpredictable consequences, such as crashing the system. With String as a base class, derive a class Pstring (for “protected string”) that prevents buffer overflow when too long a string constant is used in a definition. A new constructor in the derived class should copy only SZ–1 characters into str if the string constant is longer, but copy the entire constant if it’s shorter. Write a main() program to test different lengths of strings.




SOLUTION:

#include "stdafx.h"
#include "iostream"
#include "string.h"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////
class String
{
            protected:
                        enum { SZ = 80 };
                        char str[SZ];
            public:
                        String( )
                        {
                                    str[0] = '\0';
                         }
                        String( char s[ ] )
                        {
                                     strcpy_s(str, s);
                         }
                        void display( ) const
                        {
             cout << str << endl;
 }
                        operator char*( )
                        {
                                     return str;
                         }
};

class Pstring : public String
{
            private:
                        int len;
                        char ss[SZ];
            public:
                        Pstring( )
                        {
                                    String::String( );
                         }
                        Pstring(char s[ ])
                        {
                                    len = strlen(s);
                                    if(len >= SZ)
                                    {
                                                for(int i = 0; i < SZ-1; i++)
                                                            ss[i] = s[i];
                                                ss[SZ-1] = '\0';
                                                strcpy_s(str, ss);
                                    }
                                    else
                                                strcpy_s(str, s);
                        }
                        void display( ) const
                        {
                                    String::display( );
                        }
};
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
            Pstring ps1;
            char xstr[] = "Joyeux Noel! ";
            ps1 = xstr;
            ps1.display( );
            Pstring ps2 = "SZ-1 characters into str if the string constant is longer, but copy the entire"
          " constant if it’s shorter. Write a main() program to test different lengths of strings.";
            cout << static_cast<char*>(ps2);
            cout << endl;
            system("pause");
}

OUTPUT:



0 comments: