   Simple Inheritance Q's
  HelloI have a question or 2 about inheritance. I want to set an alter some variables in a child class BUT I declared these variables in the parent class as private (because these variables will be universal to all children). But now I have discovered that I cant set or alter these variables within the child class because they are private. What is the conventional practice to do concerning this; should I declare these variables a public or protected?Here is my simple example that should explain what I want to do easily:class Object{public:   Object(); virtual DrawSelf(); private:// Should I make these protected or public so I can alter & // set them within child classes?// I want to be able to alter these from within the child classPOINT pos;unsigned int width;unsigned int height;};class Circle : public Object{ public:  Circle( POINT nPos, unsigned int nWidth, unsigned int nHeight ) { // I cant do the below because they are private members of// the parent object.    // Should I:    //   - declare these variables as private within this class?    //   - declare these vars.. as public or protected in parent object?    pos    = nPos;    width  = nWidth;    height = nHeight; } };class RoundRect : public Object{ public:  RoundRect( POINT nPos, unsigned int nWidth, unsigned int nHeight,  unsigned int CornerRadius) {    pos    = nPos;    width  = nWidth;    height = nHeight;    CornerRad = CornerRadius; }  private:unsigned int CornerRad;};
  Don't make them public. Whether to make them protected is worth a discussion. If heirs should be able to set and get the variable when they wish, and especially if setting the variable is free of any side effect, and if all heirs are under your control (i.e. we are not speaking of a public library class), then I don't see why to not make it protected. If, on the other hand, some aspects mentioned before may not be true, you have to deliberate. However, you are always free to implement also the simple setter/getter as (perhaps inlined) functions, reducing the impact of changing the behaviour of the setter/getter later on to a minimum.EDIT: Forgotten to write that the setter/getter are to be made protected if the variable itself is made private.[Edited by - haegarr on May 25, 2010 4:55:36 AM];  Short answer: use Protected.If only the base class and derived class are the ones you want modifying these variables, then protected is the convention ("data-hiding"). Don't let other objects modify the variables by making them Public.;  The child should call the parent and have the parent modify the variable.... instead of sneaking in through the basement window after midnigt....   ;Quote:Original post by grettyI want to set an alter some variables in a child class BUT I declared these variables in the parent class as privateWhy are these variables declared in the parent class to begin with? If a parent can't do anything sensible with them, put them in the child where they belong.   ;  Or add this constructor to Object class, :Object(POINT p, unsigned int nWidth, unsigned int nHeight): pos(p), width(nWidth), height(nHeight){}Also you can add getters and setters in the object class.   ;Quote:Original post by grettyshould I declare these variables a public or protected?You probably shouldn't declare them as protected. (And public is definitely out of the question!) The guy who proposed protected data members for C++ regretted his proposal five years later, and Stroustrup admits that adding support for protected data members in the language was a mistake. See Barbara Liskov's 1987 OOPSLA keynote for details.;  Thanks for the repliesI thought that because every child object is going to have the variables pos, width & height that I should declare them in the parent object because: - to not repeat code - each child object created will get these variables created inside them when it is declared ( but they will have different values).But to sum up, are you guys saying that it is better practice to declare & place these variables within the childs private class rather than inside the parent?;Quote:Original post by grettyI thought that because every child object is going to have the variables pos, width & height that I should declare them in the parent objectIt's extremely hard to predict what every child will have. Good OO systems are open. If you place protected variables in a parent and you later discover that not every child needs them, you're screwed.I suppose what you want to express is that every child LOGICALLY has a width and a height. In OO, this is expressed with interfaces, not with implementation details. This suggests to put two pure virtual methods getWidth and getHeight in the parent class and have the children implement those.For example, a Circle does not need separate width and height variables, only a radius variable. The getWidth and getHeight methods would return the current value of that variable.Quote:Original post by grettyBut to sum up, are you guys saying that it is better practice to declare & place these variables within the childs private class rather than inside the parent?Depends on how purist your view on OO is. In your example, protected variables may work just fine, but you will be stuck with your design choices if you ever discover that not every child really needs those two variables.In your own small toy examples, that is not a real problem, because you can simply remove the variables from the parent. In production code, you are not allowed to do that once you publish your APIs, because you have no clue if someone "out there" relies on those two variables being in the parent.   