Question: Given the following class definition: #include iostream > using namespace std ; class Color { private : int red, green, blue; int checkAndTruncate ( int
Given the following class definition:
#includeiostream>
using namespace std;
class Color
{
private:
int red, green, blue;
int checkAndTruncate(int x);
public:
Color() { red = 0; green = 0; blue = 0; }
Color(int x) { red = green = blue = x; }
void read(istream &in) { in >> red >> green >> blue; }
void print(ostream &out) { out "[" red ","green "," blue "]"; }
void setRed(int r) { red = checkAndTruncate(r); }
void setGreen(int g) { green = checkAndTruncate(g); };
void setBlue(int b) { blue = checkAndTruncate(b); }
int getRed() { return red; }
int getGreen() { return green; }
int getBlue() { return blue; }
};
int Color::checkAndTruncate(int x)
{
if (x >= 0 && x
return x;
else if (x
{
return 0;
}
else
{
return 255;
}
}
Add + (binary) ++ (pre-increment) and (unary) operators so that the following main will generate the following output:
You MUST develop the operators in 3 different ways; 1-Member functions, 2- Non-Member functions and 3- Friend functions. So you need to submit 3 different programs.
Note: you are not allowed to change anything in the main
int main()
{
Color col1;
Color col2;
cout "Please enter the first color (red green blue):";
col1.read(cin);
cout "Please enter the second color (red green blue):";
col2.read(cin);
Color col3 = col1 + col2;
//printing the result of col1+col2 = col3;
col1.print(cout); cout " + "; col2.print(cout); cout " = "; col3.print(cout);cout endl;
// printing the result of col1++ = col4
col1.print(cout);
cout "++ =";
Color col4 = ++col1;
col4.print(cout); cout endl;
Color col5 = -col1;
//printing the result of -col1 = col5
cout "-"; col1.print(cout); cout " = "; col5.print(cout); cout endl;
return 0;
}
Note: The (unary) operator for colors is 255-color component for all components (red, green and blue).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
