Classes

I have this assignment but I am not sure how to do it.

voidStringC::operator*=(constinti)
{
...
}

Implement the “in-place”multiplication operator above. The original contents of the string should be duplicated “i”times.Be careful about memory allocation and de-allocation.

Please help
You're overloading and I assume with strings? If you're using std::string, then it's as simple as making a for loop and appending:

1
2
3
4
for(int j = 0; j < i; j++)
{
     this->string.append(temp);
}


Like this:

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
class test
{
public:
	std::string t;
	void set(std::string temp)
	{
		t = temp;
	}

	void operator*=(const int i)
	{
		std::string temp = this->t;
		for (int j = 0; j < i; j++)
		{
			this->t.append(temp);
		}
	}
};

int main()
{
	test Test;

	Test.set("asd");
	Test *= 5;

	std::cout << Test.t;
}
> the string should be duplicated “i”times
¿so "a" * 3 will give you "aaaaaaaa"?
Topic archived. No new replies allowed.