Re: member access
Valeriu Catina wrote:
Hi,
consider the Shape class from the FAQ:
class Shape{
public:
Shape();
virtual ~Shape();
virtual void draw() = 0;
};
Now we have several derived shapes:
class Rectangle : public Shape{
public:
Rectangle();
void draw();
private:
double a; // width
double b; // height
};
class Circle{
public:
Circle();
void draw();
private:
double r; // radius
};
// more derived shapes follow
For each shape I would need to access its member variables. For this one
can add member functions in each derived class, for instance,
something like this in the Rectangle class:
double get_width() const
{
return a;
}
For a Shape derived class which would have many member variables, I
find it rather annoying and not very elegant to implement one get/set
member function for each member variable.
Is there any other way to gain access to the derived classes data,
(except making its member variables public)
Greetings,
Vali
Writing accessors is pretty standard. With accessors at least you can
control access to your data... but more importantly, if you find
yourself giving away the internals of your class this usually
signifies maybe a design problem.