computer graphics, a light can be represented as a mixture of the three colors,
ID: 3700654 • Letter: C
Question
computer graphics, a light can be represented as a mixture of the three colors, red, green Light will be 10 and blue. We will use a colors in a Light. The level of each of the three colors may vary between o 255, and a of the form: (r, g.b). Wnite complete interface and implementation code for the class Light 1. The Light is represented by three values: the value for red, green and blue 2. Three constructors. A default constructor that takes no arguments. A constructor that takes three triple of three integer values to represent the level of each of these three values with 2 default parameters. A copy constructor 3. An assignment operator- function. 4. A friend binary addition operator+ function. To add two Lights add red to red, blue to blue and green to green 5. An insertion operatorce function. 6. A member function operator- retuns true if two Lights are equal, false otherwise 7. A member function calfled White that returns a Boolean, true if the Light is white, false otherwise. A Light is white if the three values are: whito (ss, 255, 255) Pass parameters as const and reference when appropriate. You must decide what built-in type or types you will need as private data members. Do not use any more space than is needed to store the needed values. If a function may be declared as const, do so. Do not implement within the Your implementation of this class must ensure that objects of type Light are always in a correct state. classExplanation / Answer
#include<iostream>
using namespace std;
// Class Light definition
class Light
{
private:
// Private data member to store data
int red;
int green;
int blue;
public:
// Prototype of member function
Light();
// Default value for green and blue is 255
Light(int, int = 255, int = 255);
Light(Light &);
void operator =(const Light&);
friend Light operator +(Light, Light);
friend ostream & operator<<(ostream &, Light&);
bool operator ==(Light);
bool White();
};// End of class
// Default constructor definition
Light::Light()
{
// Initializes to zero
red = green = blue = 0;
}// End of default constructor
// Parameterized constructor definition
Light::Light(int r, int g, int b)
{
// Assigns parameter value
red = r;
blue = b;
green = g;
}// End of parameterized constructor
Light::Light(Light &l)
{
red = l.red;
green = l.green;
blue = l.blue;
}// End of copy construct
// Overloading assignment operator using member function
void Light::operator = (const Light &l)
{
// Assigns parameter object values to data member
red = l.red;
green = l.green;
blue = l.blue;
}// End of function
// Overloading addition operator using friend function
Light operator +(Light l1, Light l2)
{
// Creates a temporary object using default constructor
Light l3;
// Adds the member of object l1 and l2 and stores the result in object l3
l3.red = l1.red + l2.red;
l3.green = l1.green + l2.green;
l3.blue = l1.blue + l2.blue;
// Returns the result object l3
return l3;
}// End of friend function
// Overloads << operator
ostream & operator<<(ostream &os, Light &l)
{
// Displays information
cout<<" Red = "<<l.red<<" Green = "<<l.green<<" Blue = "<<l.blue;
// Returns ostream object
return os;
}// End of friend function
// Overloads equals to operator using member function
// Returns true if object which calls the method is equals to the parameter object, otherwise returns false
bool Light::operator ==(Light l)
{
// Checks if current object red, green and blue value is equals to parameter object red, green and blue then return true
if(red == l.red && green == l.green && blue == l.blue)
return true;
// Otherwise return false
else
return false;
}// End of function
// Member function to return true if all the red, green and blue is having 255 value otherwise, false
bool Light::White()
{
// Checks if the current object red, green and blue value is 255 then return true
if(red == 255 && green == 255 && blue == 255)
return true;
// Otherwise return false
else
return false;
}// End of member function
int main()
{
// Creates object using default constructor, parameterized constructor with default argument, parameterized constructor will all the argument
Light light1, light2(0), light3(255, 0, 0);
// Displays object values using << operator overloading
cout<<" Light 1"<<light1<<endl;
cout<<" Light 2"<<light2<<endl;
cout<<" Light 3"<<light3<<endl;
// Creates a object using copy constructor
Light light4 = light2;
cout<<" Using copy constructor Light 4 = Light 2. Now Light 4 = "<<light4<<endl;
// Using = operator overloading
light1 = light3;
cout<<" Light 1 = Light 3. Now Light 1"<<light1<<endl;
// Using = and + operator overloading
light4 = (light2 + light3);
cout<<" Light 4 = Light 1 + Light 3: "<<light4<<endl;
// Using == operator overloading compares light1 is equals to light3 or not
if(light1 == light3)
cout<<" Light 1 = Light 3";
else
cout<<" Light 1 != Light 3";
// Using == operator overloading compares light1 is equals to light4 or not
if(light1 == light4)
cout<<" Light 1 = Light 4";
else
cout<<" Light 1 != Light 4";
// Calls the function to check whether light1 is white or not
if(light1.White())
cout<<" Light 1 is white.";
else
cout<<" Light 1 is not white.";
// Calls the function to check whether light4 is white or not
if(light4.White())
cout<<" Light 4 is white.";
else
cout<<" Light 4 is not white.";
}// End of main function
Sample Output:
Light 1
Red = 0 Green = 0 Blue = 0
Light 2
Red = 0 Green = 255 Blue = 255
Light 3
Red = 255 Green = 0 Blue = 0
Using copy constructor Light 4 = Light 2. Now Light 4 =
Red = 0 Green = 255 Blue = 255
Light 1 = Light 3. Now Light 1
Red = 255 Green = 0 Blue = 0
Light 4 = Light 1 + Light 3:
Red = 255 Green = 255 Blue = 255
Light 1 = Light 3
Light 1 != Light 4
Light 1 is not white.
Light 4 is white.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.