Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To support this assignment the primary reading in the text is... Although the C+

ID: 3822671 • Letter: T

Question

To support this assignment the primary reading in the text is...

Although the C++ standard library contains a string class, it is instructive to write your own. In this assignment you will create a basic string class that allows strings to grow dynamically to any size (similar to std::string).

The attached file, String.hpp, contains a definition of a very minimal string class. A few of the methods are already implemented inline. For this assignment you are to implement the remaining methods in a String.cpp file.

In a manner similar to BigInteger these strings are intended to hold a pointer to a dynamically allocated array of characters containing the text of the string. Copying and assigning string objects should copy this array as needed. When characters are added to the string via the insertmethod, the array may need to be reallocated to increase its size.

Reallocating the internal array for every insert operation is inefficient. For this assignment, you should maintain a larger array than necessary and only reallocate when the "extra" space fills up. You will need to track the size of the array and the size of the string separately since not all positions in the array are really being used by the string. Also when doing a reallocation you should double the size of the array. This approach provides amortized constant time appends and is standard (you can be sure std::string is doing something similar).

Submit String.hpp and String.cpp in a zip archive. Be sure to include your name in both files. You should submit the header file because you will likely need to make changes to the class's private section. I encourage you to write a demo/test program to exercise your class, but you don't need to submit it

The attached file, String.hpp,

#ifndef STRING_HPP

#define STRING_HPP

#include <cstddef>

#include <iosfwd>

namespace vtc {

    class String {

    public:

        // Default construct a String to an empty value.

        String( );

        // Destroy a String.

       ~String( );

        // Copy constructor.

        String( const String &other );

        // Copy assignment operator.

        String &operator=( const String &other );

        // Copies a C-style string into this String.

        String( const char *other );

        // Move constructor.

        String( String &&other );

        // Move assignment operator.

        String &operator=( String &&other );

        // Return the length of this String.

        std::size_t length( ) const

            { return size; }

        // Give read only access to the text of the string on a per-character basis.

        const char &operator[]( std::size_t index ) const

            { return buffer[index]; }

        // Give read/write access to the text of the string on a per-character basis.

        char &operator[]( std::size_t index )

            { return buffer[index]; }

        // Inserts 'ch' into the string at 'position'. If position is just off the end of the

        // string, this method appends ch onto the string. If position is "far" off the end of

        // the string, there is no effect (no exception is thrown and the program continues

        // normally).

        void insert( std::size_t position, char ch );

    private:

        // INVARIANT: ? One question to ask yourself: should the string include a trailing null

        // character?

       

        // You may add or change the private section as you desire (but be sure the inline

        // methods above still produce the correct results).

       

        char *buffer;

        std::size_t size;

    };

    //! Output a String to the given ostream.

    std::ostream &operator<<( std::ostream &output, const String &s );

}

#endif

   

((((In a manner similar to BigInteger)))

#endif

Explanation / Answer

#include #include #include using namespace std; #ifndef _TSTRING_H // only include once in a compilation unit #define _TSTRING_H class TString { // Prefix with 'T' for uniqueness public: TString(const char *pText = 0); // default ctor . // We need to include the default values here and not at cpp file. //TString(const char *pText); // default ctor TString(const TString& SObject) ; //Copy Constructor ~TString(); //Destructor int length() const; //since length() is not changing class object, it is defined as const and same for others. char *asChar() const; void assign(const TString& SObject); // since assign() is changing class object, it is not defined as const. void assign(const char *pText); //append(string SObject); void append(const TString& SObject); //equals(string SObject); bool equals(const TString& SObject) const; //equalsIgnoreCase(string SObject); bool equalsIgnoreCase(const TString& SObject) const; //indexOf(char CSearch); int indexOf(const char CSearch) const; private: int mLength; // length of char data (not including null byte) char *mpText; // pointer to dynamic char data in heap }; #endif // _TSTRING_H
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote