# E1 一种颜色，三个分量

E1 一种颜色，三个分量

//************* begin *****************
#include <iostream>
using namespace std;
class Color
{
public:
Color(int r=0,int g=0,int b=0);  //构造函数
int getValue( );                 //求得颜色值
private:
unsigned char red;   //用无符号char，表示数的范围是0-255，三基色占用3字节
unsigned char green;
unsigned char blue;
};
Color::Color(int r=0,int g=0,int b=0): red(r),green(g),blue(b) { }  //(1)
int getValue( )  //(2)
{
return(red*0x10000+green*0x100+blue);
}
//************* end *****************
int main( )
{
int r, g, b;
cin>>r;
Color c1(r);
cout<<c1.getValue()<<endl;
cin>>r>>g;
Color c2(r,g);
cout<<c2.getValue()<<endl;
cin>>r>>g>>b;
Color c3(r);
cout<<c3.getValue()<<endl;
return 0;
}

``````84
128 58
32 225 48``````

``````5505024
8403456
2097152``````

``````#include <iostream>
using namespace std;
class Color
{
public:
Color(int r=0,int g=0,int b=0);  //构造函数
int getValue( );                 //求得颜色值
private:
unsigned char red;   //用无符号char，表示数的范围是0-255，三基色占用3字节
unsigned char green;
unsigned char blue;
};    //没有写分号

Color::Color(int r,int g,int b): red(r),green(g),blue(b) { }  //默认参数只写在声明中

int Color::getValue( )  //成员函数在类外定义，要使用域运算符
{
return(red*0x10000+green*0x100+blue);
}

int main( )
{
int r, g, b;
cin>>r;
Color c1(r);
cout<<c1.getValue()<<endl;
cin>>r>>g;
Color c2(r,g);
cout<<c2.getValue()<<endl;
cin>>r>>g>>b;
Color c3(r);
cout<<c3.getValue()<<endl;
return 0;
}``````

``````#include <iostream>
using namespace std;
class Color
{
public:
Color(int r=0,int g=0,int b=0);  //构造函数
int getValue( );                 //求得颜色值
private:
unsigned char red;   //用无符号char，表示数的范围是0-255，三基色占用3字节
unsigned char green;
unsigned char blue;
};    //没有写分号

Color::Color(int r,int g,int b): red(r),green(g),blue(b) { }  //默认参数只写在声明中

int Color::getValue( )  //成员函数在类外定义，要使用域运算符
{
return(red*0x10000+green*0x100+blue);
}
int main( )
{
int r, g, b;
cin>>r;
Color c1(r);
cout<<c1.getValue()<<endl;
cin>>r>>g;
Color c2(r,g);
cout<<c2.getValue()<<endl;
cin>>r>>g>>b;
Color c3(r);
cout<<c3.getValue()<<endl;
return 0;
}``````