Hi, I have a problem to solve and I really don't know where to start. Here is the problem:
Implement a class that does string manipulation by overloading the following operators: <<, >>, = and ==. For example consider the following code:
StrShift example;
example = "John";
printf("\"example << 2\" results in %s\n", example << 2);
In the above code the output would be "hnJo" which shows the last two characters of the string "John" rotated to the left of the string. Please note that state is maintained so two calls of example << 1 would give the same end result as calling example << 2 once.
Loading

VulpesRank #55 · 29.2K reputationPosted Jan 29, 2014, 4:50 AM EST
I've also expanded the examples to include testing of the >> and != operator overloads.
Incidentally, one thing I forgot to mention previously is that when the question says that 'state should be maintained' I've taken it that the instance of StrShift being operated on should be mutated so that its underlying string represents the result of the operation.
This isn't something that would normally be done in C# because strings themselves are immutable though it is possible to achieve.
VulpesRank #55 · 29.2K reputationPosted Jan 29, 2014, 3:00 AM EST
Notice also that if you overload '==' then you have to overload '!=' as well. It's also good practice to overload the GetHashCode and Equals methods which StrShift will inherit from System.Object at the same time.
Finally, note that the C function printf is not supported in C# - you have to use Console.Write(Line) instead. Incidentally, operator overloading is not permitted in C at all though it is permitted in C++.
Anyway here's some C# code to do all this:
George Porter7 reputationPosted Jan 29, 2014, 3:28 AM EST
George Porter7 reputationPosted Jan 29, 2014, 4:12 AM EST
George Porter7 reputationPosted Jan 29, 2014, 4:56 AM EST
SUNIL GUTTARank #1,831 · 810 reputationPosted Jan 29, 2014, 9:38 AM EST
Have a nice day