This is to e a C++ program. I repeat C++ program. Must compile in VS 2015 Create
ID: 3865369 • Letter: T
Question
This is to e a C++ program. I repeat C++ program. Must compile in VS 2015
Create a class named Colortype.
It contains three private integer variables: red, green, blue.
It also has a few public member functions as described below.
A parameterized constructor that initializes the above member variables
A mutator named setColor. It has three integer parameters r, g, and b, used to set the data varables red, green, and blue, respectively.
A function named whichColor. It has three integer parameters r, g, and b. The function will return a color name (string type) based on (r, g, b). To be specific, (0, 0, 0) represents Black color; (1, 0, 0) represents Red color; (0, 0, 1) represents Blue color; (0, 1, 0) represents Green color; (1, 1, 0) represents Yellow color; (1, 0, 1) represents Magenta color; (0, 1, 1) represents Cyan color; (1, 1, 1) represents White color.
A function named PlayWithColor. It has one parameter color2, an object of class Colortype. The function returns a color name (string type) based on the result of mixing the object itself and color2. To be specific, we do the following binary calculation:
resultColor.red = red + color2.red;
resultColor.green = green + color2.green;
resultColor.blue = blue + color2.blue;
If resultColor.red is 2, we convert it to 0. We do the same for resultColor.green and resultColor.blue.
Below we give two examples.
Example 1: if we have a Colortype object whose color is Red and color2 is Blue, based on the representations described in the function whichColor, since Red is (1, 0, 0), so the object’s red is 1, green is 0, and blue is 0; since Blue is (0, 0, 1), so color2.red is 0, color2.green is 0, and color2.blue is 1.
The result of mixing the object’s color and color2 is
resultColor.red = 1+0 = 1;
resultColor.green = 0+0 = 0;
reslutColor.blue = 0+1= 1;
So we get (1, 0, 1) which represents Magenta, then the function returns a string "Magenta".
Example 2: if the object’s color is Yellow and color2 is White, i.e. the object’s red is 1, green is 1, and blue is 0; color2.red is 1, color2.green is 1, and color2.blue is 1.
The result of mixing color1 and color2 is
resultColor.red = 1+1 = 2, converted to 0;
resultColor.green = 1+1 = 2, converted to 0;
reslutColor.blue = 0 + 1 = 1;
So we get (0, 0, 1) which represents Blue, then the function returns a string "Blue".
In the function PlayWithColor, you can call function whichColor to find resultColor is which color based on the value of its red, green and blue.
You can add other methods in the class definition if necessary.
Write a program named ColorGame.cpp. It contains the main function where you create two objects of Colortype. Ask the user to input two sets of values, each set contains three numbers and each number is either 1 or 0. That is, each set represents a color. Use the user’s input as arguments to invoke the parameterized constructor. Call the function PlayWithColor to find out and display the result of mixing two colors.
Explanation / Answer
#include <iostream>
using namespace std;
//class declaration
class Colortype
{
//private variables
private:
int red;
int green;
int blue;
//function declaration
public:
//default constructor
Colortype()
{
}
//parametrized constructor
Colortype(int r, int g,int b)
{
red=r;
green=g;
blue=b;
}
//function to set color
void setColor(int r,int g,int b)
{
red=r;
green=g;
blue=b;
}
//function which returns color based on r,g,b params
string whichColor(int r,int g,int b)
{
if(r==0)
{
if(g==0)
{
if(b==0)
{
return "Black";
}
else
if(b==1)
{
return "Blue";
}
}
else if(g==1)
{
if(b==0)
{
return "Green";
}
else
if(b==1)
{
return "Cyan";
}
}
}
else if(r==1)
{
if(g==0)
{
if(b==0)
{
return "Red";
}
else
if(b==1)
{
return "Magenta";
}
}
else if(g==1)
{
if(b==0)
{
return "Yellow";
}
else
if(b==1)
{
return "White";
}
}
}
}
//fucntion to return color name after mixing two colors
string PlayWithColor(Colortype color2)
{
Colortype resultColor;
resultColor.red = red + color2.red;
resultColor.green = green + color2.green;
resultColor.blue = blue + color2.blue;
return whichColor(resultColor.red,resultColor.green,resultColor.blue);
}
};
//m,ain function
int main()
{
int r,g,b;
bool flag=false;
Colortype color[2];
for(int i=0;i<2;i++)
{
flag=false;
do{
cout<<" Enter red color(0 or 1): ";
cin>>r;
if(r==0 || r==1)
flag=true;
}while(!flag);
flag=false;
do{
cout<<" Enter green color(0 or 1): ";
cin>>g;
if(g==0 || g==1)
flag=true;
}while(!flag);
flag=false;
do{
cout<<" Enter blue color(0 or 1): ";
cin>>b;
if(b==0 || b==1)
flag=true;
}while(!flag);
color[i]=Colortype(r,g,b);
}
cout<<" Mixing the given colors result in " <<color[0].PlayWithColor(color[1]);
return 0;
}
Output:
Enter red color(0 or 1): 1
Enter green color(0 or 1): 1
Enter blue color(0 or 1): 1
Enter red color(0 or 1): 0
Enter green color(0 or 1): 0
Enter blue color(0 or 1): 0
Mixing the given colors result in White
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.