CS253: Software Development with C++

Fall 2021

HW 2

CS253 HW2: Phobic Numbers                

Changes                

Updates to the assignment will be noted here. None yet!                 

Description                

Fear is a necessary part of life, but, alas, it has infected the realm of computer numbers. For this assignment, you will write a class Phobic that behaves much like int, but it avoids certain scary numbers, by going to the next larger integer. Initially, only the number 13 is scary, but the numbers that are regarded as scary can grow over time. Different Phobic objects can fear different sets of numbers—they are independent.                 

Specifically, you will provide Phobic.h (it starts with a capital letter), with the interface of that class, and Phobic.cc, with the implementation. A library, libhw2.a, which will contain the compiled implementation of that class.                 

Methods                

Phobic must have the following public methods:                

Phobic(optional int)
This constructor sets its current value to the argument, or zero if no argument given. Only the number 13 will be regarded as scary by this object.
Copy constructor
Copy all information from another object of the same class, including the value and the scary numbers.
Assignment operator
Copy the current value from the other object of the same class. The collections of scary numbers are merged. That is, for a = b, if a initially fears 13 and 4, and b fears 13, 42, and 65535, then, after assignment, b will be unchanged, but a will fear 13, 4, 42, and 65535.
Destructor
Destroy.
.add(Phobic)
.sub(Phobic)
.mul(Phobic)
.div(Phobic)
Add/subtract/multiply/divide the current object and another, yielding a third object with the expected numeric value, that fears all the numbers feared by either object. These methods do not alter the current object or the argument. Even though the argument is specified as a Phobic, the sample run, below, uses a int as an argument. This works, because Phobic has a ctor that takes an int, which acts as an implicit conversion from int to Phobic.
.add_to(Phobic)
.sub_from(Phobic)
.mul_by(Phobic)
.div_by(Phobic)
Add/subtract/multiply/divide the current object and another, putting the result into the current object. The current object is altered to additionally fear everything that is feared by the argument object, similar to assignment. These methods do not alter the argument.
.eq(Phobic)
Return true iff the value in the current object equals the value in the argument.
.lt(Phobic)
Return true iff the value in the current object is less than the value in the argument.
.get()
Return the int value associated with this object.
.make_scary(int, …)
This method takes from one to ten int arguments. This object should regard those values as additional scary numbers. Any previous scary numbers remain scary.
.is_scary(int)
Return true iff the object fears the argument.

For all methods that create or alter 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, 177777, and 5, then the value must be changed to 15.                 

The types and names in the method descriptions, above, do not determine the C++ declarations of those methods. They only serve to informally describe what sort of arguments a method might take. For example, I expect that you will give a name to the argument of .is_scary(). You might pass certain arguments by reference, use const, declare return types, etc.                 

Const-correctness, for arguments, methods, and operators, 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.                 

You may define other methods or data, public or private, as you see fit. You may define other classes, as you see fit. However, to use the Phobic class, the user need only #include "Phobic.h", not any other header files.                 

Non-Requirements                

Several things are not specified by this assignment. That means that the answer to these questions is “It’s up to you.”

Debugging                

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

    export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a

Libraries                

libhw2.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 libhw2.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 libhw2.a. Particularly, do not put main() in Phobic.h or Phobic.cc. You will also have to create Phobic.h, and put it into hw2.tar. We will test your program by doing something like this:                 

    mkdir a-new-directory
    cd the-new-directory
    tar -x </some/where/else/hw2.tar
    cmake . && make
    cp /some/other/place/test-program.cc .
    g++ -Wall test-program.cc libhw2.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 hw2.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/HW2 fetched by unknown <unknown> with Linux UID 65535 at 2024-11-21T18:17:43 from IP address 3.145.91.111. 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(hw2)

# 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 <iostream>
#include <cassert>

using namespace std;

int main() {
    Phobic a(13);               // a is 13 bumped to 14
    assert(a.get() == 14);      // since 13 is always scarry
    a.make_scary(14);           // bumps a to 15
    assert(a.get() == 15);

    Phobic b = a.add(4);        // b is 15+4 = 19
    assert(a.get() == 15);
    assert(b.get() == 19);

    b.make_scary(35,12,34);     // b now fears 12,13,34,35
    assert(b.is_scary(12));
    assert(b.is_scary(13));
    assert(b.is_scary(35));
    assert(b.is_scary(34));
    assert(!b.is_scary(42));

    a.add_to(b);                // a is 15+19 = 34, bumped to 36
    assert(a.get() == 36);
    assert(b.add(0).get() == 19);

    assert(a.is_scary(12));     // b’s fears should get merged into a.
    assert(a.is_scary(13));     // a now fears 12,13,14,34,35
    assert(a.is_scary(14));
    assert(a.is_scary(34));
    assert(a.is_scary(35));

    a.mul_by(2);                // a is 36*2 = 72
    const Phobic c(72);
    assert(a.eq(c));            // 72 == 72
    assert(b.lt(c));            // 19 < 72
    assert(c.get() == 72);
    assert(c.eq(72));           // 72 == 72
    assert(c.eq(c));            // 72 == 72
    assert(!c.eq(99));          // !(72 == 99)
    assert(c.lt(99));           // 72 < 72
    assert(!c.lt(-12));         // !(72 < -12)
    assert(!c.lt(c));           // !(72 < 72)
    assert(c.add(1).eq(73));

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

Requirements                

  • All copies (copy ctor, assignment operator) are “deep”. Do not share data between copies—that’s not making a copy.

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.                 

Tar file                

    cmake . && make

How to submit your work:                

In Canvas, check in the file hw2.tar to the assignment “HW2”. 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.