Question
Write the declaration for the overloaded operator+= for the color class as a public member function inside the color class definition in Color.h. This declaration
Write the declaration for the overloaded operator+= for the color class as a public member function inside the color class definition in Color.h.
This declaration will be written as: Color& operator+=(const Color& rhs);
The Color& value returned by this function will be a reference to the lhs object (see 5c).
We are overloading the += operator for our color class, which is identified by operator+=.
When adding the color appearing on the right-hand side of the operator+= to that on the left, the left-hand side object will contain the result: lhs+=rhs means that rhs will be added to lhs.
You may wonder why we are not writing this function with two arguments
The reason is that the expression above is equivalent to writing lhs.operator+=(rhs)
This implies that lhs is an implicit argument to the operator+= function call
Which is similar to how any other member function operates: an implicit argument provides the object upon which the function will operate
The argument rhs is labeled as const in the function declaration because we are not modifying it
Unlike when we wrote the operator== overload, this overloaded function will not be labeled as const because it does modify the lhs object calling it
In Color.cpp, locate the comment block for Color& Color::operator+=(const Color& rhs).
Write a definition at this location corresponding to the declaration that youve already written. Remember that adding two colors involves taking the average of the two colors. The autograder will assume that you use integer division.
Although you dont have to, use the getters for the accessing the R, G, and B values of the rhs (rhs.get_R(), rhs.get_B(), and rhs.get_G()).
We are requesting that you do this so that we can illustrate the important point that member functions called on a constant object must be constant as well.
Uncomment the block in Main.cpp, and test your implementation of operator+=. What is printed in main will mirror that below once your implementation is correct:
Color color : (80,125,125) Color color2 : (255,255,255) after color2+=color : Color color : (80,125,125) Color color2 : (167,190,190) Color color3 : (167,190,190) after color3+=color : Color color : (80,125,125) Color color3 : (123,157,157) |
Fill out the description portion of the comment block for Color& Color::operator+=(const Color& rhs).
After successfully implementing your operator+=, I would now like to focus on the implications of passing const Color& rhs with respect to the member functions of rhs that can successfully called.
Understand that rhs is passed as a constant reference, meaning that we should not modify it. Note that your implementation of operator+= makes calls (we requested that it should) to rhs.get_R(), rhs.get_B(), and rhs.get_G().
Remove the const label from the in-class definition of get_R(): int get_R(void) const { return R; } ? int get_R(void) { return R; }
Compile your program. You should now see an error message similar to that below:
In member function 'Color& Color::operator+=(const Color&)': Color.cpp: error: passing 'const Color' as 'this' argument discards qualifiers Color.h: note: in call to 'int Color::get_R()' int get_R(void) { return R; } |
The compiler is complaining about call to the non-const function get_R() because the argument being passed to it (rhs) is a constant reference and the fact that it is constant should be honored.
To tell the compiler that the function get_R() does not modify an object, and can subsequently be called for constant objects, we must add the const label directly after the parameter list of both the function declarations and definitions.
Only functions that are const can be called with constant objects; only such functions promise that they will not modify the object.
Provided that we have defined get_R() inside the class declaration, we only need to add const in one place. Add back the const label after the get_R() parameter list.
////////////////////////////////////////////////////////////////////////////// // main() // ////////////////////////////////////////////////////////////////////////////// int main() { Color color(80,125,125); cout << "Color color : " << color.to_str() << endl; /* Color color2(255,255,255); cout << "Color color2 : " << color2.to_str() << endl; cout << "after color2+=color : " << endl; color2+=color; cout << "\tColor color : " << color.to_str() << endl; cout << "\tColor color2 : " << color2.to_str() << endl; Color color3(167,190,190); cout << "Color color3 : " << color3.to_str() << endl; cout << "after color3+=color : " << endl; color3+=color; cout << "\tColor color : " << color.to_str() << endl; cout << "\tColor color3 : " << color3.to_str() << endl; */ }
#ifndef COLOR_H #define COLOR_H ////////////////////////////////////////////////////////////////////////////// // Dependencies // ////////////////////////////////////////////////////////////////////////////// #include#include ////////////////////////////////////////////////////////////////////////////// // Color Class Definition // ////////////////////////////////////////////////////////////////////////////// class Color_error {}; class Color { /* Public */ public: Color(); Color(int r, int g, int b); std::string to_str() const; bool is_valid_val(int) const; int get_R(void) const { return R; } int get_G(void) const { return G; } int get_B(void) const { return B; } void set_R(int); void set_G(int); void set_B(int); /* Private */ private: int R; int G; int B; }; ////////////////////////////////////////////////////////////////////////////// #endif
////////////////////////////////////////////////////////////////////////////// // Color Class Member Definitions // ////////////////////////////////////////////////////////////////////////////// /***************************************************************************** * Color::Color() * * * * Description ....: Defualt constructor for Color class * * * * Written By : Environment : Mac OS X 10.10.5 * * Date ......: 2017/06/29 Compiler ...: Homebrew GCC 6.3.0_1 * *****************************************************************************/ Color::Color() : R(255), G(255), B(255) { } /***************************************************************************** * Color::Color(int r, int g, int b) * * * * Description ....: Parameterized constructor for Color class * * * * Written By : Environment : Mac OS X 10.10.5 * * Date ......: 2017/06/29 Compiler ...: Homebrew GCC 6.3.0_1 * *****************************************************************************/ Color::Color(int r, int g, int b): R(r), G(g), B(b) { if (!is_valid_val(R) || !is_valid_val(G) || !is_valid_val(B)) { throw Color_error{}; } } /***************************************************************************** * Color& Color::operator+=(const Color& rhs) * * * * Description ....: * * * * Written By : Environment : Mac OS X 10.10.5 * * Date ......: 2017/06/29 Compiler ...: Homebrew GCC 6.3.0_1 * *****************************************************************************/ /***************************************************************************** * std::string Color::to_str() const * * * * Description ....: Creates string "(R,G,B)", where R, G, and B are * * the values stored in a respective object of the class * * * * Written By : Environment : Mac OS X 10.10.5 * * Date ......: 2017/06/29 Compiler ...: Homebrew GCC 6.3.0_1 * *****************************************************************************/ std::string Color::to_str() const { std::stringstream ss; ss << '(' << R << ',' << G << ',' << B << ')'; return ss.str(); } /***************************************************************************** * bool Color::is_valid_val(int v) const * * * * Description ....: Checks whether the actual argument provided to the * * parameter v constitutes a valid color value * * * * Written By : Environment : Mac OS X 10.10.5 * * Date ......: 2017/06/29 Compiler ...: Homebrew GCC 6.3.0_1 * *****************************************************************************/ bool Color::is_valid_val(int v) const { if (v < 0 || v > 255) return false; else return true; } /***************************************************************************** * void Color::set_R(int r) * * * * Description ....: Sets the member R to the actual argument provided * * to the parameter, provided that that argument is a * * valid color value. If it is not, the function * * throws Color_error{} * * * * Written By : Environment : Mac OS X 10.10.5 * * Date ......: 2017/06/29 Compiler ...: Homebrew GCC 6.3.0_1 * *****************************************************************************/ void Color::set_R(int r) { if (!is_valid_val(r)) throw Color_error{}; R = r; } /***************************************************************************** * void Color::set_G(int g) * * * * Description ....: Sets the member G to the actual argument provided * * to the parameter, provided that that argument is a * * valid color value. If it is not, the function * * throws Color_error{} * * * * Written By : Environment : Mac OS X 10.10.5 * * Date ......: 2017/06/29 Compiler ...: Homebrew GCC 6.3.0_1 * *****************************************************************************/ void Color::set_G(int g) { if (!is_valid_val(g)) throw Color_error{}; G = g; } /***************************************************************************** * void Color::set_B(int B) * * * * Description ....: Sets the member B to the actual argument provided * * to the parameter, provided that that argument is a * * valid color value. If it is not, the function * * throws Color_error{} * * * * Written By : Environment : Mac OS X 10.10.5 * * Date ......: 2017/06/29 Compiler ...: Homebrew GCC 6.3.0_1 * *****************************************************************************/ void Color::set_B(int b) { if (!is_valid_val(b)) throw Color_error{}; B = b; } //////////////////////////////////////////////////////////////////////////////
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started