2

If we have one parents class and 2 child class. Child1 inherit from parents and child2 inherit from child1. In child2 class how do we call child1 and parents overloaded constructor in child2.

class parents{
        int x;
        public:
        parents(int a)
        {
           x=a;
        }
        };
class child1 : public parents{
        int y;
        pulic:
        child1(int b)
        {
          y=b;
        }
        };
class child2 :public child1{
          int z;
          public:
          child2(int c)
          {
              z=c;
            // how we call above overloaded constructor in this overloaded cnstructor 
          }
          }; 
abhiarora
  • 9,743
  • 5
  • 32
  • 57
  • Does this answer your question? [What are the rules for calling the superclass constructor?](https://stackoverflow.com/questions/120876/what-are-the-rules-for-calling-the-superclass-constructor) – DaszuOne Apr 17 '20 at 20:38

1 Answers1

3

You do this in the member initializer-list of the constructor of the subclass. Because parent is not a direct base of child2. You have to give a constructor for your direct bases (child) to child2 and may be in contructor of child1, you can initialize your base class parent.

Thereby, you will have to propagate initialization in a base class all way through the hierarchy but could directly address the "very base" constructor (parent) in member initializer list of child2.

Try this:

class parents {
    int x;
public:
    parents(int a)
    {
      x = a;
    }
};
class child1: public parents {
    int y;
public:
    child1(int b): parents(b)
    {
      y = b;
    }
};

class child2: public child1{
    int z;
public:
    child2(int c): child1(c)
    {
      z = c;
    }
};

Or

You can use virutal inheritance. Virtual bases are the exception to the above statement.

They are always initialized in leaf classes, otherwise potentially you get multiple constructor calls for the same base. So if you make base virtual, not only can you initialize it in child2, you must.

Furthermore, you will actually be enforced to call it in any "Grandchild"-class, so you can't forget it by accident:

class parents {
    int x;
public:
    parents(int a)
    {
      x = a;
    }
};
class child1: public virtual parents {
    int y;
public:
    child1(int b): parents(b)
    {
      y = b;
    }
};

class child2: public virtual child1{
    int z;
public:
    child2(int c): child1(0), parents(1)
    {
      z = c;
    }
};
abhiarora
  • 9,743
  • 5
  • 32
  • 57
  • 1
    In this way only one overloaded constructor called i want to call above 2 overloaded constructor in third class – Muhammad Irfan Shahzad Apr 17 '20 at 20:39
  • 1
    There are two options and i have mentioned both of them. See my latest edit – abhiarora Apr 17 '20 at 20:52
  • 1
    i would also recommend putting the child class assignment in the initializer list as well. There can be performance implications and its the best practice. https://www.geeksforgeeks.org/when-do-we-use-initializer-list-in-c/ – Michael Apr 17 '20 at 20:55
  • 1
    but virtual inheritance use to function overriding but the above code no function have same name. – Muhammad Irfan Shahzad Apr 18 '20 at 09:05
  • 1
    if the above two classes parents and child1 have one parameter in overloaded we solve the problem but if parents overloaded constructor have 1 parameter child1 overloaded constructor have 2 parameter and child3 have overloaded constructor have 3 parameter then how to deal with them – Muhammad Irfan Shahzad Apr 18 '20 at 09:12
  • You can then call that constructor in member initializer list. Like this: `child2(int a): child1(2,3,4,5), parent(5,6,7,8)` – abhiarora Apr 18 '20 at 09:22