숙제를 위해 몇 가지 클래스를 작성 중이며 클래스 멤버 함수를 main에서 호출 할 수 없도록하고 싶습니다. 그럴 경우 프로그램을 종료하고 싶습니다. 멤버 함수가 호출되는 시점을 어떻게 알 수 있습니까? 클래스의 경우 각 객체는 < 100,200,215> 형식의 색상을 나타냅니다. 도와 주셔서 감사합니다!클래스 멤버 함수를 main에서 호출 할 수 없도록하려면 어떻게해야합니까?
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;
}
하지만 다른 기능에서 호출 할 수 있기를 원하십니까? –
예. 나는 단지 그들이 main에서 호출 될 수 없기를 바란다. – user1681673
한 가지 방법은 그것들을 비공개로 만들고'friend's에 접근하고자하는 곳에서 함수를 만드는 것입니다. –