6.8 Algebra on real numbers 169
void algebra::print() const
{
cout << x <<""<<color << endl;
}
//---
void algebra::shift(double y)
{
color = setcolor(x+y);
x = x+y;
}
Following is a main program that uses the algebra class:
int main()
{
string chroma;
algebra A = algebra();
A.print();
cout << A.get(chroma) << " " << chroma << endl;
algebra B = algebra(-0.9);
B.print();
B.shift(2.1);
B.print();
return 0;
}
Running this program produces on the screen:
0 white
0 white
-0.9 red
1.2 black
Two features are worth emphasizing:
- Thegetfunction returns the value ofxthrough the functionreturn
and passes the color through an argument endowed with the reference
declarator (&). - Because the functionsetcolorhas been declared private, it cannot be
called from the main program.