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
VulpesPosted Jan 29, 2014, 9:50 AM
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.
SUNIL GUTTAPosted Jan 29, 2014, 2:38 PM
Have a nice day
George PorterPosted Jan 29, 2014, 9:56 AM
George PorterPosted Jan 29, 2014, 9:12 AM
George PorterPosted Jan 29, 2014, 8:28 AM
VulpesPosted Jan 29, 2014, 8:00 AM
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: