This repo provides skeleton code for an example RGB class. You will use this as the basis for implementing operator overloading.
CMake is required to build this project. If you already have CMake installed, building is easy:
mkdir build && cd build
cmake ..
make && make test
If your system doesn't already have CMake installed, follow the instructions below.
I would be shocked if your Linux system doesn't have CMake by default. No really. Please check before installing, just run cmake --version
. If it somehow doesn't already have CMake, installing should be as easy as asking your package managed to install cmake, e.g. sudo apt-get install cmake
.
Mac doesn't generally include CMake out of the box, but it's often installed by other things so it's worth checking. Again, just run cmake --version
from any command prompt. If you don't have CMake, there are two ways to install it:
You can also use XCode if you wish: just run cmake .. -G XCode
and then open the resulting XCode project.
The easiest way to do this lab on Windows (including the SoC computers) is to use Visual Studio Code. In VSCode:
- Make sure the "C/C++ Extension Pack" is installed. This should include CMake and CMake Tools. You might have to restart after installing the extension pack.
- Git clone this repo (which you can do from inside VSCode) and open the resulting folder.
- Press ctrl-shift-p, and type in "CMake run tests". It may ask you for which compiler to use. Choose the most recent Visual Studio available.
Another option is to install the official CMake binaries and use Visual Studio directly:
- Clone this repo and change to the directory you put it in.
- Make a "build" directory and change directory to that.
- Run
cmake ..
- Open the Visual Studio solution file generated.
Unit tests are in the RGB_test.cpp
file and run automatically with make test
. To add a new unit test, just add something like:
TEST(RGBTest, TestName) {
// write test code here ...
}
Note that test names must be unique.
When you mix two colours, you effectively average each of their components. So if you mix bright red RGB(240, 0, 0) and bright blue RGB(0, 0, 220) you would get something like RGB(120, 0, 110).
You are to work in pairs and follow TDD, aka "ping-pong programming". One partner writes a test case, and then the other partner writes the simplest code to satisfy the test case. Then the second partner writes the test case and the first writes the implementation. Continue until the tasks below are complete.
- Add new method to
RGB
to mix the current colour with a given colour and return the new colour,RGB RGB::mix(const RGB& c)
- Implement colour mixing as an overloaded "+" operator. If
c1
andc2
are colours, thenRGB c3 = c1 + c2;
should result in the mix ofc1
andc2
. - Write a method brighten a colour by a factor given as a real number. A factor of 1.0 means no change, while 2.0 would mean twice as bright (i.e. double each value) or 0.5 would be half as bright (i.e. halve each value).
- Implement the method above as an overloaded "*" operator.
- Implement a
+=
operator that acts as a shortcut forc1 = c1 + c2
- Implement a
*=
operator that acts as a shortcut forc1 = c1 * f
- Implement an equality test operator
==
that returns true if two RGB instances are the same. Can you use this operator to simplify your tests? - Implement a not-equals operator
!=
that returns true if two RGB instances are different. Can you use this operator to simplify any tests?