CS253: Software Development with C++

Fall 2021

HW 4

CS253 HW4: Operations!                

Changes                

To be clear: Phobic + Phobic, Phobic + int, and int + Phobic must all work. Same for -, *, /, and the comparison operators.                 

Similarly, both Phobic += Phobic and Phobic += int must work. Same for -=, *=, and /=.                 

Description                

For this assignment, you will build upon your work in HW2. Your Phobic class will now have operators in place of several methods.                 

Operators                

Phobic must have all the methods & operators from HW2, except that:                 

.add(Phobic) is replaced by +
.sub(Phobic) is replaced by -
.mul(Phobic) is replaced by *
.div(Phobic) is replaced by /
Add/subtract/multiply/divide the left-hand and right-hand objects, yielding a third object with the expected numeric value, that fears all the numbers feared by either object. These do not alter either the left-hand or right-hand objects.
.add_to(Phobic) is replaced by +=
.sub_from(Phobic) is replaced by -=
.mul_by(Phobic) is replaced by *=
.div_by(Phobic) is replaced by /=
Add/subtract/multiply/divide the left-hand object and the right-hand object, putting the result into the left-hand object. The left-hand object is altered to additionally fear everything that is feared by the right-hand object. This method does not alter the right-hand object.
.make_scary(int) is replaced by Phobic << int;
Think of the insertion operator (<<) as inserting fear into the object. Chaining is allowed:
    Phobic p;
    p << 1 << 2 << 3;
will make the object p afraid of 1, 2, 3, and, of course, 13.
.eq(Phobic) is replaced by ==
Return true iff the value in the left-hand object equals the value in the right-hand object.
.lt(Phobic) is replaced by <
Return true iff the value in the left-hand object is less than the value in the right-hand object.

In addition, you must implement the operators !=, <=, >=, and >, which work as expected.                 

It must be an compile-time error or warning to ignore the result of the operators +, -, *, /, ==, >=, <=, <, >, !=, and the methods .get() and .is_scary(). That is, this must generate a compile-time error or warning:

    Phobic castor, pollux;
    castor + pollux;

For everything that creates or alters an object, the value that results must be one that the object does not regard as scary. Scary values are avoided by incrementing the value past them. For example, if the value should be 13, but the current object fears 13, 14, 20, and 5, then the value must be changed to 15.                 

When I say that such-and-such an operator replaces a method, I mean it. The replaced method must not exist. E.g., your class must not have an .add method, or a .lt method.                 

Const-correctness, for arguments, functions, and methods, is your job. For example, it must be possible to call .get() on a const object, or to copy a const object to a non-const object.                 

Exceptions                

The / and /= operators must detect attempted division by zero, and throw a runtime_error containing a descriptive string that mentions the value of the operands. Attempted division by zero must not alter the left-hand operand.                 

Debugging                

If you encounter “STACK FRAME LINK OVERFLOW”, then try this:

    export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a

Libraries                

libhw4.a is a library file. It contains a number of *.o (object) files. It must contain Phobic.o, but it may also contain whatever other *.o files you need. The CMakeLists.txt shown creates libhw4.a. It does not contain main().                 

Testing                

You will have to write a main() function to test your code. Put it in a separate file, and do not make it part of libhw4.a. Particularly, do not put main() in Phobic.h or Phobic.cc. You will also have to create Phobic.h, and put it into hw4.tar. We will test your program by doing something like this:                 

    mkdir a-new-directory
    cd the-new-directory
    tar -x </some/where/else/hw4.tar
    cmake . && make
    cp /some/other/place/test-program.cc .
    g++ -Wall test-program.cc libhw4.a
    ./a.out

We will supply a main program to do the testing that we want. You should do something similar. It’s your choice whether to include your test program in your hw4.tar file. However, cmake . && make must work. If it fails because you didn’t package test.cc, but your CMakeLists.txt requires test.cc, then your build failed, and you get no points. Test your tar file, not just your code.                 

This is the Colorado State University CS253 web page https://cs.colostate.edu/~cs253/Fall21/HW4 fetched by unknown <unknown> with Linux UID 65535 at 2024-06-26T10:00:49 from IP address 18.220.58.179. Registered CSU students are permitted to copy this web page for personal use, but it is forbidden to repost the information from this web page to the internet. Doing so is a violation of the rules in the CS253 syllabus, will be considered cheating, and will get you an F in CS253.

Sample Run                

Here is a sample run, where % is my shell prompt:                 

% cat CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(hw4)

# Are we in the wrong directory?
if (CMAKE_SOURCE_DIR MATCHES "[Hh][Ww]([0-9])$"
   AND NOT PROJECT_NAME MATCHES "${CMAKE_MATCH_1}$")
    message(FATAL_ERROR "Building ${PROJECT_NAME} in ${CMAKE_SOURCE_DIR}")
endif()

# Using -Wall is required:
add_compile_options(-Wall)

# These compile flags are highly recommended, but not required:
add_compile_options(-Wextra -Wpedantic)

# Optional super-strict mode:
add_compile_options(-fmessage-length=80 -fno-diagnostics-show-option
    -fstack-protector-all -g -O3 -std=c++17 -Walloc-zero -Walloca
    -Wctor-dtor-privacy -Wduplicated-cond -Wduplicated-branches
    -Werror -Wextra-semi -Wfatal-errors -Winit-self -Wlogical-op
    -Wold-style-cast -Wshadow -Wunused-const-variable=1
    -Wzero-as-null-pointer-constant)

# add_compile_options must be BEFORE add_executable.

# Create the executable from the source file main.cc:
add_library(${PROJECT_NAME} Phobic.cc)
add_executable(test test.cc)
target_link_libraries(test ${PROJECT_NAME})

# Create a tar file every time:
add_custom_target(${PROJECT_NAME}.tar ALL COMMAND
    tar -cf ${PROJECT_NAME}.tar *.cc *.h CMakeLists.txt)

% cmake . && make
… cmake output appears here …
… make output appears here …
% cat test.cc
#include "Phobic.h"
#include "Phobic.h"     // I meant to do that.
#include <iostream>
#include <cassert>
#include <stdexcept>

using namespace std;

int main() {
    const Phobic three(3), seven(7), two=seven/three, five{seven-two}, ten=10;
    assert(five == 5);
    assert(ten-two == 4*two);
    assert(ten/two == two+two+two/two);

    Phobic x(4); x << 12 << 3;  // fears 3, 12, and 13
    Phobic y; y << 11;          // fears 11 & 13
    Phobic fearful(y/x);
    assert(fearful.is_scary(11));
    assert(fearful.is_scary(12));
    assert(fearful.is_scary(13));
    assert(!x.is_scary(11));
    assert(!y.is_scary(3));
    assert(!y.is_scary(2));

    Phobic a(13);               // 14
    assert(a.get() == 14);
    a << 14;                    // a is bumped to 15
    assert(a.get() == 15);
    a = a;                      // fraught with peril
    assert(a.is_scary(14));     // did it survive?

    assert(2 + a == 17);
    assert(a + 2 == 17);
    assert(a + a == 30);
    assert(a - two == 15);      // 15 - 2 is 13 bumped to 15
    assert(30 == two * a);
    assert(seven == a / two);

    a += ten;                   // 15 ⇒ 25
    assert(a == 25);
    a -= 5;                     // 25 ⇒ 20
    assert(20 == a);
    a *= a;                     // 20 ⇒ 400
    assert(a == 400);
    a /= ten;                   // 400 ⇒ 40
    assert(a == 40);

    assert(two < three);
    assert(3 > two);
    assert(3 <= ten);
    assert(ten >= 5);
    assert(ten == ten);
    assert(-10 != ten);

    try {
        a /= (two/ten);         // attempt division by zero
    }
    catch (const runtime_error &) {  // Hey, no variable!
        a += 3 - two;           // 40 ⇒ 41
    }
    assert(a.get() == 41);      // Did we catch the divide by zero?

    cout << "Success!\n";
    return 0;
}
% ./test
Success!

Hints                

Requirements                

Same requirements as HW2, except as modified above.                 

Tar file                

    cmake . && make

How to submit your work:                

In Canvas, check in the file hw4.tar to the assignment “HW4”. It’s due 10:00:00ᴘᴍ MT Saturday, with a 24-hour late period for a 25% penalty.                 

How to receive negative points:                

Turn in someone else’s work.