If a class have a requirement that two values must always be in sync, the
'setter' method may then automatically adjust the two values on change.
Since it isn't possible to just write-protect a value, the class must then
also have a 'getter' method.
class Pythagoras {
protected:
double a,b,c,area;
Pythagoras() {
a = b = c = area = 0;
}
void SetA(double v) {
a = v;
Evaluate();
}
void SetB(double v) {
b = v;
Evaluate();
}
double GetA() const {
return a;
}
double GetB() const {
return b;
}
double GetHyp() const {
return c;
}
double GetArea() const {
return area;
}
protected:
void Evaluate() {
c = sqrt(a*a + b*b);
area = a*b/2;
}
};
With the above class, it is impossible for a user to mess up the triangle
area without doing stupid low-level tricks with pointers etc.
/Per W
On Tue, 20 May 2003 Rus...@ao... wrote:
> I understand the basics of Classes, but what determines which
> members/functions should be public/private?
> The Accessor functions (is that the correct terminology?) seem to take a
> public member of a class and attach some argument/value/data to a private member
> of the same class. What is the logic behind this? I can understand what is
> being done, but I don't understand the reasoning.
>
>
> Regards,
> Russ
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: ObjectStore.
> If flattening out C++ or Java code to make your application fit in a
> relational database is painful, don't do it! Check out ObjectStore.
> Now part of Progress Software. http://www.objectstore.net/sourceforge
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|