How to implement class dynamic_string, based on the interface

I have such interface and need to implement dynamic_string. But I am not a C++ pro and don't know how to do this.
Please, help. Thank you in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
template<typename T = char>
    class dynamic_string
    {
    public:
        dynamic_string();
        dynamic_string(const T* value);

        dynamic_string(const dynamic_string<T>& right);
        dynamic_string(dynamic_string<T>&& right);

        dynamic_string<T>& operator=(const dynamic_string<T>& right);
        dynamic_string<T>& operator=(dynamic_string<T>&& right);

        T& operator[](size_t idx);
        const T& operator[](size_t idx) const;

        size_t size();
        size_t capacity();

        dynamic_string<T> substr(size_t pos, size_t size);
        dynamic_array<dynamic_string<T>> split(T separator);

        dynamic_string<T>& operator+(const dynamic_string<T>& right);
        void operator+=(const dynamic_string<T>& right);
        bool operator==(const dynamic_string<T>& right);

        size_t size() const;
        size_t capacity() const;

        void resize(size_t newSize);
        void reserve(size_t newCapacity);
    };

Last edited on
You probably should start by making a dynamically allocated data member that stores a stores a string of the specified value, like a char array.
Well, this tells me nothing :-( I rarely use C++. But have such task
Follow this code:

class Interface
{
public:
Interface(){}
virtual ~Interface(){}
virtual void method1() = 0; // "= 0" part makes this method pure virtual, and
// also makes this class abstract.
virtual void method2() = 0;
};

class Concrete : public Interface
{
private:
int myMember;

public:
Concrete(){}
~Concrete(){}
void method1();
void method2();
};

// Provide implementation for the first method
void Concrete::method1()
{
// Your implementation
}

// Provide implementation for the second method
void Concrete::method2()
{
// Your implementation
}

int main(void)
{
Interface *f = new Concrete();

f->method1();
f->method2();

delete f;

return 0;
}

A dynamically allocated array can be made using the new keyword. The result is a pointer pointing to an allocated block of the specified number of that type on the heap.

the syntax is
 
new TYPE[ NUMBER_OF_SPACES ]; // returns a TYPE* pointer. 

Once you have allocated an array in such way, and wish to be done with it, just delete it using the syntax
 
delete[] PTR;


Alternatively, you can use a smart pointer so that you never have to worry about deleting the block yourself. They are a bit stiff though...

Last edited on
Thank you but I have no problem with syntax. just with implementation logic.
What exactly in the implementation has you stuck?
All these functions. Because I don't know how dynamic_string works.
Um... ok... let’s see....



I’m going to number the functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
template<typename T = char>
    class dynamic_string
    {
    public:
        dynamic_string(); // 1
        dynamic_string(const T* value); // 2

        dynamic_string(const dynamic_string<T>& right); // 3
        dynamic_string(dynamic_string<T>&& right); // 4

        dynamic_string<T>& operator=(const dynamic_string<T>& right); // 5
        dynamic_string<T>& operator=(dynamic_string<T>&& right); // 6

        T& operator[](size_t idx); // 7
        const T& operator[](size_t idx) const; // 8

        size_t size(); // 9
        size_t capacity(); // 10

        dynamic_string<T> substr(size_t pos, size_t size); // 11
        dynamic_array<dynamic_string<T>> split(T separator); // 12

        dynamic_string<T>& operator+(const dynamic_string<T>& right); // 13
        void operator+=(const dynamic_string<T>& right); // 14
        bool operator==(const dynamic_string<T>& right); // 15

        size_t size() const; // 16
        size_t capacity() const; // 17

        void resize(size_t newSize); // 18 
        void reserve(size_t newCapacity); // 19
    };



Now can you tell me which one is the most confusing? We’ll start front there..
13, 14, 15
13 is an 'operator overload'. it says that when you use the + with this class, do this code. so later you can say
dynamic_string a,b,c;
a = c+b; //you defined what this 'addition' means.

14 is an extension of it, defining what to do in the case of +=, which is often a call to the + operator with a little juggling of where to put the result.

15 overloads the == "is this equal" operator, so you can now say if(a==b) and get the desired behavior.

many simplified, less powerful languages block operator overloading. I don't think java allows it, for example.
it can be abused; you can make the + operator do division or factorials for your own numerical type, for example.


It can also be beautiful. no one wants to see this:
my_result = a.add(b);
instead of
my_result = a+b;
especially in the middle of a 2-3 line long math equation.

is this a learning exercise? C++ <string> type already does all this stuff and is already dynamic. You can extend it to do more things, but you don't have to re-create all the guts if you are doing something useful and not just learning.
Last edited on
13) this function produces a new string by concatenating the dynamic string right to the original string. This does not effect the original string, it just includes it as one of it’s arguments.

So let’s look at this.
Step 1) make a new string.
Step 2) make the new string big enough to hold both of the strings.
Step 3) insert the original string into our new string
Step 4) insert our right string after the original string in our new string.
Step 5) return the new string!


keep in mind reserve() can change the size of a string ;)

14) this function does the same thing as above, but instead of generating an entire new string, it modifies the original string, so our process is only slightly changed.

Step 1) make a new string. <- we already have a string.
Step 2) make the original string big enough to hold both of the strings.
Step 3) insert the original string into our new string <- our string already has the original string.
Step 4) insert our right string after the original string in our new string.
Step 5) return the new string! <- the function is void.


15) this function tests whether or not to string contain the exact same content. It returns a bool and does not effect either string. It’s really easy to make. Just loop through all the characters in the strings and make sure they are equal, if they all are, then your good, if some of them aren’t, then welp they aren’t equal. 👌


Any and all questions are welcome, if I didn’t explain something well, do tell.
Last edited on
How can I measure size and capacity?
size is just the length of characters in your string, whereas capacity is the actual length of your entire allocated block, which can differ. Generally I’m pretty sure those are recorded as a variable of type size_t which they edit whenever a += op or an = op or something happens.
Thank you very much!
You are very welcome! :)
Topic archived. No new replies allowed.