How to Implement Operator+= for Custom String Class?

  • MHB
  • Thread starter linag96
  • Start date
In summary, the conversation discusses the creation of two operator functions in a my_string.cpp and .h file. These functions are used to append a string to a character and a character to a string. The private member functions involved in this process are capacity, size, and a pointer to a character. The two operator functions are implemented using a conditional statement to check the capacity, and dynamically allocating memory to store the appended string or character. They also involve a for loop to iterate through the size of the string and copy the contents to the new pointer.
  • #1
linag96
4
0
I have a my_string.cpp and .h file. I am required to append string q to char c and append char c to string q
these are the following functions:
Code:
my_string my_string::operator+=(const my_string& q)
my_string my_string::operator+=(char c)
Private member functions:
Code:
int capacity;
int size;
char *ptr;
Please help on writing the 2 operator functions
 
Technology news on Phys.org
  • #2
My_string& My_string::eek:perator+=(const my_string& q) { if (capacity < (size + q.size)) { capacity = size + q.size; char* newptr = new char[capacity]; for (int i = 0; i < size; i++) newptr = ptr; delete[] ptr; ptr = newptr; } for (int i = 0; i < q.size; i++) { ptr[size + i] = q.ptr; } size += q.size; return *this;}My_string& My_string::eek:perator+=(char c) { if (capacity < (size + 1)) { capacity = size + 1; char* newptr = new char[capacity]; for (int i = 0; i < size; i++) newptr = ptr; delete[] ptr; ptr = newptr; } ptr[size] = c; size++; return *this;}
 

FAQ: How to Implement Operator+= for Custom String Class?

1. What is the purpose of the "Operator+=(const my_string& q)" function?

The purpose of this function is to overload the "plus equals" operator, also known as the compound assignment operator, for the "my_string" class. This allows for concatenation of two my_string objects, with the resulting string being stored in the original object on the left side of the operator.

2. How does the "Operator+=(const my_string& q)" function work?

The function takes in a constant reference to a my_string object, denoted by "q". It then appends the content of "q" to the end of the original string object, using the existing functions and operators within the class. This allows for a more efficient and concise way of concatenating strings.

3. Can the "Operator+=(const my_string& q)" function be used with other data types?

No, this function is specifically designed to work with the my_string class. However, other operators can be overloaded to work with other data types, allowing for similar functionality.

4. Is there a limit to the length of the strings that can be concatenated using "Operator+=(const my_string& q)"?

The length of the strings is limited by the amount of memory available. As long as there is enough memory, the strings can be concatenated without any limitations.

5. Can the "Operator+=(const my_string& q)" function modify the original string object?

Yes, as this is a compound assignment operator, the original string object on the left side of the operator will be modified. This means that the resulting string will be stored in the original object, rather than creating a new object.

Similar threads

Replies
89
Views
5K
Replies
89
Views
5K
Replies
17
Views
1K
Replies
75
Views
5K
Replies
5
Views
4K
Replies
9
Views
1K
Replies
2
Views
1K
Back
Top