Search code examples
c++inheritancesubclasssuperclass

C++ Inheritance : Base attributes over various derived objects


I'm currently learning about C++ Inheritance, so if this question is a dumb one, I apologize in advance.

Implementing this scenario:

  1. A super class has a color attribute, which can be any color (let's assume color is represented by an integer).

  2. Let's assume I have an initialization of this super class, with the color red.

  3. I am also going to initialize different objects of a sub-class which also share the color red.

My question is, is there any way that I can initialize this attribute color to red (or any color for that matter) and it would be automatically inherited by objects of it's sub-class, instead of setting the attribute to red every-time I initialize one of these objects?

Again apologies if I'm missing a basic concept here, but I can't seem to find anything online on this.

Pseudo-Code per request:

Super-class code:

class Shape {

    int color;

    Shape::Shape(int c) : color(c) { }  //constructor

}

Sub-Class code:

class Square {

    int length, width;

    Square::Square(int l, int w, int c)
    :    length(l),
         width(w),
         color(c)
    { }
}

class Circle {

    int radius;

    Square::Square(int r, int c)
    :    radius(r),
         color(c)
    { }
}

What I'm trying to say is that both square and circle need to have the same color, is there anyway (maybe from the super class? ) to declare this color (ex. red), and both shapes would have this color set the same?


Solution

  • You could accomplish what you want with a static default_color that gets used when no color is explicitly specified, and gets set whenever a color is specified.

    struct Shape {
        static int default_color;
        int color;
        Shape(int c) : color(c)
        {
            default_color = c;
        }
        Shape() : color(default_color) {}
    };
    
    Shape::default_color = BLACK;
    
    struct Square : public Shape {
        int length, width;
        Square(int l, int w, int c)
        : Shape(c),
          length(l),
          width(w),
        { }
    
        Square(int l, int w)
        : length(l),
          width(w)
        { }
    }
    
    struct Circle : public Shape {
        int radius;
        Circle(int r, int c)
        : Shape(c),
          radius(r)
        { }
    
        Circle(int r)
        : radius(r)
        { }
    }
    
    int main()
    {
        Square sq(2, 3, RED);
        Circle cir(10); // Automatically red, since that's
                           the last color explicitly specified
    }
    

    I would say this is a poor design though. Global state like this makes it very easy to make mistakes. You now have to think about the state of the entire program whenever you create a shape. It would be better to simply create the Circle as Circle cir(10, sq.color);. That makes it explicit what color your Circle is, and reduces cognitive load on the programmer.