CS253: Software Development with C++

Spring 2018

HW 6

CS253 HW6: Operators!                


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:                 

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.                 

Appending via +=                

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.                 

Concatenation via +                

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 <<=                

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?                 

Comparison via == and !=                

When comparing two Serial objects, just compare the accumulated strings. Compare them in the normal way that std::string objects are usually compared.                 

Sample Run                

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 {
        catch (string st) {
            caught = true;

        l <<= s; assert(l == 1L<<63);   // true -1

        const string now = "2018-04-25T21:57:26.393215";
        s += now;			// true -1 now
        s.get(b);			// -1 now
        s.get(i);			// now
        string foo="bar";
        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:



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.                 

Tar file                

How to submit your homework:                

    ~cs253/bin/checkin HW6 hw6.tar

How to receive negative points:                

Turn in someone else’s work.                 

User: Guest                 

Edit History Source

Modified: 2018-04-04T19:15                 

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building