In computer systems color is represented by RGB format (Red. Green and Blue colo
ID: 3804707 • Letter: I
Question
In computer systems color is represented by RGB format (Red. Green and Blue color components) where each component can hold a value from 0 to 255. Implement all functions and operators given in the following header file: # include using namespace std; class public://Default constructor//Constructor with parameters//Copy constructor//Getters//Setters//a function that increment all components//a print function to print all components//a function to add two private: int red; int green; int blue; Test your class with an appropriate main ()Explanation / Answer
/*
* File: Color.cpp
* Author: Sam
*
* Created on 26 March, 2017, 3:50 PM
*/
#include <iostream>
using namespace::std;
class Color {
private:
int red,green,blue;
public:
int GetBlue() const { //Getter
return blue;
}
void SetBlue(int B) { //Setter
this->blue = B;
}
int GetGreen() const { //Getter
return green;
}
void SetGreen(int G) { //Setter
this->green = G;
}
int GetRed() const { //Getter
return red;
}
void SetRed(int R) { //Setter
this->red = R;
}
Color(int R, int G, int B) : //param constructor
blue(B), green(G), red(R) {
}
Color() { //default constructor
blue = red = green = 0;
}
Color(const Color& other) : //Copy constructor
red(other.red), green(other.green), blue(other.blue) {
}
void Print() { //to print
cout<<"R = "<<red<<endl;
cout<<"G = "<<green<<endl;
cout<<"B = "<<blue<<endl;
}
void IncrementAll() {
red++; //Increase R G B
green++;
blue++;
if(red>255) //If they cross their limit, set to max
red=255;
if(green>255)
green=255;
if(blue>255)
blue=255;
}
void addColor(const Color& other){ //Add another color
red = red+other.red;
green = green+other.green;
blue = blue+other.blue;
if(red>255) //If threshold is crossed set to max values
red=255;
if(green>255)
green=255;
if(blue>255)
blue=255;
}
};
int main() {
Color c1;
c1.Print();
cout<<endl;
Color c2 = Color(10,20,100);
c2.Print();
c2.addColor(Color(200,200,200));
c2.Print();
return 0;
}
Here you go champ, I commented the code as far as possible. But still if you can't understand, feel free to comment below
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.