Question: URGENT PLEASE in C++ Given the following class definition: #include using namespace std; class Color { private: int red, green, blue; // The function should

URGENT PLEASE in C++

Given the following class definition:

#include

using namespace std;

class Color

{

private:

int red, green, blue;

// The function should return x if 0<=x<=255, 0 if x < 0 and 255 if x > 255

int checkAndTruncate(int x)

public:

// Add a default constructor

// Add a constructor with parameters

void setRed(int r){ red = r; }

void setGreen(int g){ green = g; };

void setBlue(int b){ blue = b; }

int getRed(){ return red; }

int getGreen(){ return green; }

int getBlue(){ return blue; }

};

Color addition or subtracting is done by adding/subtracting each component of the first color with/from the corresponding component of the second color, red with red, green with green and blue with blue.

Since the color components ranges from 0 to 255, if you got a result greater than 255 then you have to truncate it to 255, if the result is less than 0 then you have to truncate it back to 0.

  1. Implement the default and non-default constructors.

  1. Implement the checkAndTruncate function.

  1. Add three non-member but friend functions, one for addition, one for subtraction and one for checking the equality and do the tasks 1-4 given in the main() function.

A sample output is given below:

Enter the Color 1 ( red, green and blue ) : 120 120 120

Enter the Color 2 ( red, green and blue ) : 25 25 11

Color 1 + Color 2 = red[145] Green[145] Blue[131]

Color 1 - Color 2 = red[95] Green[95] Blue[109]

Color 1 == Color 2 = 0

  1. Overload the +, - and == operators as friend functions and do the tasks 1-4 in the given main().

A sample output is given below:

Enter the Color 1 ( red, green and blue ) : 120 120 120

Enter the Color 2 ( red, green and blue ) : 25 25 11

Color 1 + Color 2 = red[145] Green[145] Blue[131]

Color 1 - Color 2 = red[95] Green[95] Blue[109]

Color 1 == Color 2 = 0

  1. Overload the +, - and == operators as member functions and do the tasks 1-4 in the given main().

void main()

{

/**********************************************************

Task 1: create two objects of type Color name them col1 and col2

Read their values from the user

**********************************************************/

/**********************************************************

Task 2: add col1 and col2 and print the result

***********************************************************/

/**********************************************************

Task 3: subtract col1 from col2 and print the result

***********************************************************/

/**********************************************************

Task 4: check if col1 is equal to col2 and print the result

***********************************************************/

}

Note: you are supposed to write 3 different programs (a full program for each b, c and d).

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!