I've been asked to make a program which has a base class, two derived children classes from it, and then another one which is derived from both children. The assignment asks me to inherit half the attributes from one child and the rest from the other one. (Hope this makes sense to you). These attributes are not specific of the children, but of the base class. This is my code:
# include <iostream>
class Base {
    protected:
        int _var1;
        int _var2;
        int _var3;
        int _var4;
    public:
        Base() : _var1(10), _var2(20), _var3(30), _var4(40) {}
        virtual ~Base() {};
        int getVar1() const { return this->_var1; }
        int getVar2() const { return this->_var2; }
        int getVar3() const { return this->_var3; }
        int getVar4() const { return this->_var4; }
};
class Child1 : public virtual Base {
    public:
        Child1() { _var1 = 100; _var2 = 200; _var3 = 300; _var4 = 400;}
        virtual ~Child1() {}
};
class Child2 : public virtual Base {
    public:
        Child2() { _var1 = 500; _var2 = 600; _var3 = 700; _var4 = 800;}
        virtual ~Child2() {}
};
class GrandChild : public Child1, public Child2 {
    public:
        GrandChild() {}
        virtual ~GrandChild() {}
};
int main() {
    Child1      c;
    Child2      d;
    GrandChild  g;
    std::cout << c.getVar1() << std::endl;
    std::cout << d.getVar1() << std::endl;
    std::cout << g.getVar1() << std::endl;
}
With this code, g instance automatically inherits the attributes from Child2, but I want to pick which attributes it inherits from which class, e.g. _var1 from Child1 and _var3 from Child2. Is that even possible? Thank you so much in advance :)
                        
I stripped the unrelated parts off your code (every thing public, and no need to declare the virtual destructor again):
It produces the same output:
This is because, the
Grandchildconstructor is equivalent to:A virtual base is always initialized by the most derived class. Then the
Child1andChild2subobjects are initialized by calling their constructor. They are called in this order, because thats the order herestruct GrandChild : Child1, Child2.Thats why you see
500on the last line of output. First, theBaseconstructor initializesvar1to10. Next theChild1constructor assigns100. And eventually theChild2constructor assigns500.You cannot selectively inherit only some members. I suppose what you want is to let
Child1assign100tovar1andChild2assign600tovar2. Note that initialization of this members already happened whenGrandChildcalled the constructor ofBase. BecauseBaseis a virtual base class andGrandChildis the most derived class, neitherChild1norChild2have any buisness in initializing the membersvar1andvar2ofGrandChild.You can however, write constructors that assign only some of the inherited members:
Output: