Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 _Color.hpp and your implementation (i.e., definition) in a file named _Color.cpp. Then write a main function to test the correct operation of your class and overloaded operators. Your main program should prompt the user for two colors, and read each of them in using the overloaded << operator. Your program should then perform each of the overloaded operations specified above on the two colors, and print out the result (using the overloaded >> operator). Your program should provide a continuation loop. An example of a run of your program should be similar to the following (note that user input is in bold font): Welcome to the color compare program! Enter Color 1: (128, 32, 224) Enter Color 2: (64, 224, 192) Color 1 is less than Color 2 Color 1 is NOT greater than Color 2 Color 1 + Color 2 = (192, 255, 255) Color 1 Color 2 = (64, 0, 32) Would you like to continue (Enter y or n): y Enter Color 1: (64, 224, 192) Enter Color 2: (128, 32, 224) Color 1 is NOT less than Color 2 Color 1 is greater than Color 2 Color 1 + Color 2 = (192, 255, 255); Color 1 Color 2 = (0, 192, 0) Would you like to continue (Enter y or n): n Constraints: Use all of the operators you have overloaded to implement the main program and the produce the output specified above. Test and revise your program until you are sure it is correct. Note that, for this program, is acceptable (though unrealistic) to assume that the user will enter the colors correctly. When you are done, name the main source code file _9A.cpp (where you replace with your U of M email address). Remember to follow the naming conventions diligently; Also remember to submit three files: your main function (_9A.cpp), _Color.hpp, and _Color.cpp. Note: there are three separate Moodle links, one for each file; make sure you submit each file using the correct link.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Genetic Databases

Authors: Martin J. Bishop

1st Edition

ISBN: 0121016250, 978-0121016258

More Books

Students also viewed these Databases questions