Question: #include using namespace std; class Color { /* Public */ public: void cout_rgb (void) { cout < < Color Values: ( < < R <

#include  using namespace std; class Color { /* Public */ public: void cout_rgb (void) { cout << "Color Values: (" << R << ',' << G << ',' << B << ')' << endl;} int R; int G; int B; /* Private */ private: }; int main() { Color color; color.R = 80; color.G = 0; color.B = 0; color.cout_rgb(); }

In the Main.cpp file, change the following assignments:

color.R = 80; to color.R = 29;

color.G = 0; to color.G = 511;

color.B = 0; to color.B = 98;

Compile and run the program; the output should be a single line:

Color Values: (29,511,98)

Notice that the values of the members of the Color class (i.e. R, G, and B) can be changed directly. This does not allow us prevent invalid values being assigned such as the value for G, which is not between 0 and 255.

In Main.cpp, place the declarations for int R, int G, and int B under private:

/* Public */ public: void cout_rgb (void) { cout << "Color Values: (" << R << ',' << G << ',' << B << ')' << endl;

} /* Private */ private: int R; int G; int B;

Compile the program. You should see multiple errors. The problem is that we are trying make an assignment to a private variable outside of the class; this is not allowed.

Fix the errors by removing the assignment statements color.R = 29;, color.G = 511;, and color.B = 98; from Main.cpp.

Compile and run the program. The class members R, G, and B have not been initialized, so the values output are whatever those memory location currently hold (you may see zero on Visual Studio).

Well see how to initialize them properly when we work with constructors in a later labwork. Until then, well use in class initialization. I.e. change int R; to int R=255;

And similarly for G and B. Note: this is not the preferred method. Well see that later.

Lets set things up to be able to access the color values. In the Color class, create three public getter methods (get_R, get_G, and get_B) that return the values of R, G, and B respectively.

Update the main function to the following:

int main() { Color color; color.cout_rgb(); cout << "color.R = " << color.get_R() << endl; cout << "color.G = " << color.get_G() << endl; cout << "color.B = " << color.get_B() << endl; }

Compile and run your program. The output should be very similar to:

Color Values: (255,255,255) color.R = 255 color.G = 255 color.B = 255

If you've made it to this point, you've successfully implemented getter methods to return the values of private variables that cannot be directly accessed by non-member functions (i.e. public).

Now, update the Color class by creating three public setter methods set_R, set_G, and set_B to set the values of R, G, and B respectively.

Recall, that these values should only be between 0 and 255, so enforce that in your method. Throw an exception (use runtime_error) if the value passed into the function is not valid.

Update Main.cpp to the following for testing:

int main() { Color color; color.cout_rgb(); cout << "color.R = " << color.get_R() << endl; cout << "color.G = " << color.get_G() << endl; cout << "color.B = " << color.get_B() << endl; cout << "Please enter integer values for R, G, and B: "; int R, G, B; cin >> R >> G >> B; color.set_R(R); color.set_G(G); color.set_B(B); color.cout_rgb(); }

Compile and run the program. The output of your code should be similar to that below (although your input values for R, G, and B may differ depending on what you decide to input):

Color Values: (255,255,255) color.R = 255 color.G = 255 color.B = 255 Please enter integer values for R, G, and B: 1 2 3 (1,2,3)

Try running the program and entering a negative value or a value greater than 255.

Gracefully handle the exception thrown in 18 with a try/catch block in main(). Inform the user that the value is out of range before exiting the program. Do not get a corrected value from the user.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!