Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the Main.cpp file, change the following assignments: color.R = 80; to color.R

ID: 3911870 • Letter: I

Question

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).

We’ll see how to initialize them properly when we work with constructors in a later labwork. Until then, we’ll 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. We’ll see that later.

Let’s 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.

Explanation / Answer

#include <iostream>

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;

//change the value of R,G and B to 29,511,98 respctly

color.R = 29;

color.G = 511;

color.B = 98;

color.cout_rgb();

}

/*output after changing valuers of R,G and B in the main

Color Values: (29,511,98)

*/

----------------------------------------------------

//After making required changes for making variables private with values assigned to 255 for R,G and B //and using getter function to get the values,program and output is as below

#include <iostream>

using namespace std;

class Color {

/* Private */

private:

int R = 255;

int G = 255;

int B = 255;

/* Public */

public:

void cout_rgb(void) {

cout << "Color Values: (" << R << ',' << G <<

',' << B << ')' << endl;

}

int get_R()

{

return R;

}

int get_G()

{

return G;

}

int get_B()

{

return B;

}

};

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;

}

/*output

Color Values: (255,255,255)

color.R = 255

color.G = 255

color.B = 255

*/

--------------------------------------------------------------------------------------

//Now define the setter function to set values of R , G and B in class Color, program and output after //these changes is given below

#include <iostream>

using namespace std;

class Color {

/* Private */

private:

int R = 255;

int G = 255;

int B = 255;

/* Public */

public:

void cout_rgb(void) {

cout << "Color Values: (" << R << ',' << G <<

',' << B << ')' << endl;

}

//getter functions

int get_R()

{

return R;

}

int get_G()

{

return G;

}

int get_B()

{

return B;

}

//setter functions

void set_R(int r)

{

R = r;

}

void set_G(int g)

{

G = g;

}

void set_B(int b)

{

B = b;

}

};

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();

}

/*output

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
Color Values: (1,2,3)

*/

------------------------------------------------------------------

//program after trying to catch the error when values enterd for R , G and B are not in range

#include <iostream>

using namespace std;

class Color {

/* Private */

private:

int R = 255;

int G = 255;

int B = 255;

/* Public */

public:

void cout_rgb(void) {

cout << "Color Values: (" << R << ',' << G <<

',' << B << ')' << endl;

}

//getter functions

int get_R()

{

return R;

}

int get_G()

{

return G;

}

int get_B()

{

return B;

}

//setter functions

void set_R(int r)

{

R = r;

}

void set_G(int g)

{

G = g;

}

void set_B(int b)

{

B = b;

}

};

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;

try

{

if ((R < 0 || G < 0 || B < 0) || (R > 255 || G > 255 || B > 255))

{

throw 1;

}

color.set_R(R); color.set_G(G); color.set_B(B);

color.cout_rgb();

}

catch (int e)

{

if( e == 1)

cout << "the value is out of range"<<endl;

}

}

/*output1

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

the value is out of range

*/

/*output 2

Color Values: (255,255,255)
color.R = 255
color.G = 255
color.B = 255
Please enter integer values for R, G, and B: 256 2 3
the value is out of range

*/