我正在为家庭作业编写一些课程,并且我希望我的课程成员函数不可能在 main 中被调用。如果是,我希望程序退出。我怎么知道我的成员函数何时被调用?至于类,每个对象代表一种颜色,格式为<100,200,215>。谢谢您的帮助!
class Color{
public:
Color( unsigned red = 0, unsigned green = 0, unsigned blue = 0 ); // ctor
unsigned getRed() const; // accessor
unsigned getGreen() const; // accessor
unsigned getBlue() const; // accessor
Color & setRed( unsigned red ); // mutator
Color & setGreen( unsigned green ); // mutator
Color & setBlue( unsigned blue ); // mutator
const Color & output() const;
private:
unsigned myRed;
unsigned myGreen;
unsigned myBlue;
static unsigned okColor(unsigned color);
}; //Class Color
int main(int argc, const char * argv[])
{
}
Color::Color( unsigned red, unsigned green, unsigned blue):
myRed(okColor(red)),myGreen(okColor(green)),myBlue(okColor(blue))
{
//initialization list here...
}
//accessors
unsigned Color::getRed() const {return myRed;}
unsigned Color::getGreen() const {return myGreen;}
unsigned Color::getBlue() const {return myBlue;}
//mutators
Color & Color::setRed(unsigned red){
myRed = okColor(red);
return *this;
}
Color & Color::setGreen(unsigned green){
myGreen = okColor(green);
return *this;
}
Color & Color::setBlue(unsigned blue){
myBlue = okColor(blue);
return *this;
}
//output
const Color & Color::output() const{
cout << "<" << myRed << "," << myGreen << "," << myBlue << ">" << endl;
return *this;
}
//checkers
unsigned Color::okColor(unsigned myColor){
if (myColor > 255) {
die("Color intensity is out of range!");
}
return myColor;
}
bool die(const string & msg){
cerr << endl << "Fatal error: " << msg <<endl;
exit(EXIT_FAILURE);
}
Color mixture( const Color & color0, const Color & color1, double weight){
double color1Multiplier = 0;
Color mixture;
unsigned mixtureRed;
unsigned mixtureBlue;
unsigned mixtureGreen;
color1Multiplier = 1 - weight;
mixtureRed = (color0.getRed() * weight) + (color1.getRed() * color1Multiplier);
mixtureBlue = (color0.getBlue() * weight) + (color1.getBlue() * color1Multiplier);
mixtureGreen = (color0.getGreen() * weight) + (color1.getGreen() * color1Multiplier);
mixture.setRed(mixtureRed);
mixture.setBlue(mixtureBlue);
mixture.setGreen(mixtureGreen);
return mixture;
}