Question
Problem A. Overloading Operators for the Color Class (20 points): Recall the color class, from your last homework assignment (HW 8, Problem B), specified below.
Problem A. Overloading Operators for the Color Class (20 points):
Recall the color class, from your last homework assignment (HW 8, Problem B), specified below. You will reuse it for this part of homework assignment 9 by overloading operators for the class and using ALL of the operators you overload in a program
Member Variables: The class should have three private member variables, r, g, b that contain the red, green, and blue components of the light color. Constructors: The class should have the following two constructors or one constructor that functions as specified in item i. or ii. below. i. The default constructor should initialize all the components to 0.
ii. A constructor that takes three int arguments representing the red, green, and blue components, respectively. This should check that all three components are between 0 and 255, inclusive. If they are, then it should assign the argument values to the corresponding member variables. If one or more is not, then the constructor should print out a message, and set all member variables to 0. Member Functions: The class should have the following public member functions: get_r() takes no arguments and should return the red component.
get_g() takes no arguments and should return the green component.
get_b() takes no arguments and should return the blue component.
set(int new r, int new g, int new b) sets the value of the components to the corresponding arguments. However, if any of the arguments is outside the range 0 to 255, then the program should print a message and not change any of the component values
. invert() changes the components to the values 255 - r, 255 - g, and 255 b, respectively. So, for example, if the current color is 100, 110, 120, the new red value would be 255 100 = 155, the new green would be 255 110 = 145, and the new blue would be 255 120 = 135.
scale(double s) multiplies each component by the scale factor s. For example, if the current color is 100, 115, 130, and s is .5, then then new red component would be 100 .5 = 50, the new green is 115 .5 = 57 (the result, being an int, should not include any fractional part), and the new blue is 130 .5 = 65. However, if scaling would result in any component being outside the range 0 to 255, then this function should print a message and leave the components as they were. Any function specified above that does not change the value of its member variables should be a const function. This is good programming practice, and should be followed not only here, but on all classes you write. Next, add the declarations and definitions to the color class necessary to overload the operators specified on the next page. Less than: < Specification: The less than operator will check to see if the sum of the red, green and blue components on the left side of the < operator is less than the sum of the red, green, and blue components on the right side of the operator, and return true if that is the case, and false otherwise. Greater than: > Specification: The greater than operator will check to see if the sum of the red, green and blue components on the left side of the > operator is greater than the sum of the red, green, and blue components on the right side of the operator and return true if that is the case, and false otherwise. Addition: + Specification: Adds the red, green and blue components of each color object together and returns the resulting color. Note that the value of any color component cannot be greater than 255, so if the result of adding any two color components is greater than 255, the resulting color component should be set to 255. Subtraction: - Specification: Subtracts the red, green and blue components of each color object from each other and returns the resulting color. Note that the value of any color component cannot be less than zero (0), so if the result of subtracting any two components is less than zero, the resulting color component should be set to 0. Input Stream: >> Specification: Reads the red, green and blue components of a color in from any input stream (cin or a file). The colors should be input in the form (r, g, b), followed by a newline. Output Stream: << Specification: Writes the red, green and blue components of a color out to any output stream (cout or a file). The color should be written in the format (r, g, b) followed by a newline. NOTE, we have eliminated the print function from the color class. You will replace it the << operator, as specified above. Put your Color interface (i.e., declaration) in a file named
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