For this assignment, you will enhance class Serial
by adding a few
operators.
                
Your class must have the same methods and operators described in HW5. You must also create the following operators:                 
+=
bool
+=
int
+=
short
+=
long
+=
char
+=
std::string
+=
Serial
+
bool
+
int
+
short
+
long
+
char
+
std::string
+
Serial
+
Serial
+
Serial
+
Serial
+
Serial
+
Serial
+
Serial
<<=
Serial
<<=
Serial
<<=
Serial
<<=
Serial
<<=
Serial
<<=
Serial
==
Serial
!=
Serial
Of course, you can’t write code that literally says bool + Serial
.
That means a bool
expression on the left, and a Serial
expression (perhaps a variable) on the right.
                
The above operators may be methods, or free functions, as you wish.                 
Const-correctness, both arguments & methods, is your job. For example,
it must be possible to concatenate two const Serial
objects,
or compare a const Serial
with a non-const Serial
.
                
+=
                Adding to a Serial
via +=
is the same as calling .put
.
                
Serial +=
Serial is a bit different. It appends the
string from the right-hand-side to the left-hand-side object,
altering the left-hand-side object.
The right-hand-side object is unchanged.
                
+
                Concatenating two Serial
objects, or a Serial
and a bool
/
int
/ short
/ long
/ char
/ std::string
and a
Serial
, produces a new Serial
object, which contains the
serialized data from the left-hand-side object, followed by the
serialized data from the right-hand-side object. Neither operand is
modified.
                
<<=
                Extraction via <<=
is similar to the .get
method. It removes the
next datum from the front of the Serial
, assigns it to the given
variable, and throws a descriptive std::string
if something goes
wrong for the same reasons as .get
.
                
I agree that this use of the operator is completely bizarre. We live in crazy times, don’t we?                 
==
and !=
                When comparing two Serial
objects, just compare the accumulated strings.
Compare them in the normal way that std::string
objects are usually
compared.
                
Here is a sample run, where %
is my shell prompt:
                
% cat test.cc #include "Serial.h" #include <iostream> #include <string> #include <cassert> using namespace std; int main() { cout << "Test begins\n"; Serial s; bool b; int i; long l; s += true; s.put(-1); // s = -1 const Serial t(s); // s = t = true -1 assert(t == s); s.get(b); assert(b); // s = -1 s = s + 'x'; // s = -1 'x' assert(s != t); s = (1L<<63) + t; // s = LONG_MIN true -1 assert((s+t).str() == "l\x8f\x80\0\0\0\0\0\0\0ti\x0fti\17"s); bool caught = false; try { s.get(b); } catch (string st) { caught = true; } assert(caught); l <<= s; assert(l == 1L<<63); // true -1 const string now = "2024-11-21T19:25:50.458751"; s += now; // true -1 now s.get(b); // -1 now s.get(i); // now string foo="bar"; s.get(foo); assert(foo == now); cout << "Test ends\n"; return 0; } % g++ -Wall test.o hw6.a % ./a.out Test begins Test ends
If you encounter “STACK FRAME LINK OVERFLOW”, then try this:
export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a
The requirements are the same as those for HW5, plus:
"const char *"
)
must fail to compile (produce an error):
+=
C-string
+
C-string
+
Serial
.put(
C-string)
"theta"
is a const char *
, not a std::string
.
"gamma"s
(with a trailing s
) is a const std::string
, however.
If you have any questions about the requirements, ask. In the real world, your programming tasks will almost always be vague and incompletely specified. Same here.                 
Like HW5,
you will have to write a main()
function to test your code.
Don’t turn it in.
                
We will test your program in much the same manner as for HW5.                 
hw6.tar
*.cc
)
*.h
), including Serial.h
Makefile
, with a capital M
)
Makefile
’s default target must create the library file
hw6.a
.
Makefile
must use at least -Wall
when compiling.
~cs253/bin/checkin HW6 hw6.tar
Turn in someone else’s work.                 
User: Guest