Use the
String
class
that we developed in lecture to solve the following assignment.
We've already seen how buffers are used to alleviate speed discrepancies between slow devices (memory, disks, screens, etc.) and the blazing fast CPU. With networking, not only is speed a concern (although much less so), but also throughput/bandwidth is crucial. It is more efficient to send one packet of information over the network than to send 10 smaller ones. There are many reasons for this. Perhaps the most pertinent is that each packet sent over the network has a certain amount of extra information added to it in addition to the application data. This extra data is concerned with addressing (sender/recipient), content type and length, etc. It is maintenance information, essentially. But, if you have, say, 15 bytes of maintenance info per packet and need to send 100 bytes of information, how is that going to work out? The above scenario said 1 packet vs 10. That'd be:
15+100 = 115 vs (15+10)*10 = 250
The first one seems like it will use up less bandwidth -- less than half, in fact.
So, looking at how the streams did it, we see a lovely Cstring buffer data member in those classes. But we already know that managing String objects is much easier than managing a Cstring array -- they take care of most of the details by themselves.
To make future endeavors even easier, we can add some buffer-like capabilities to our String class:
Noting how similar our translation functions (for
extraction from
a String
object) are to the extraction functions for
streams (inputting data from a stream), you can't help but wonder
if we maybe shouldn't overload operator<<
for
insertion to a String
object? (Just like you use
operator<< to print something on an output stream.)
It would be similar
to our current operator+=
implementations, except
it would be for any of our data types (including the other 5 built-in
types, too).
In other words, it would be nice to be able to build up a
String
object by means such as:
String str_obj; str_obj << "She has " << apples << " apples.\n";
Where apples
is of a built-in data type. (Even if
it wasn't, a programmer could overload operator<< to work
between the String class and their class/enumeration once we've
finished this lab exercise!)
Code appropriate operator<<'s for our String
class
.
Don't forget to test your new insertion operations (perhaps modifing
the String
test application) to show your code works properly.
Note: Ignore formatting for now. Just translate the values you are given in some default manner analogous to how an output stream would if you gave it no formatting information. It would be nice if your default for decimals was a fixed notation with decimal places always shown -- even if 0. You can choose how many places to show.
cout << x << y;might give you an idea...)
This assignment is (Level 4).